mirror of
https://github.com/embarklabs/swarmgw.git
synced 2025-02-20 12:38:06 +00:00
Better API
Allow changing gateway and transport protocol
This commit is contained in:
parent
cc57e156a7
commit
9a428a241c
@ -1,13 +1,13 @@
|
||||
# swarmgw
|
||||
|
||||
This library can be used to upload/download files to Swarm via https://swarm-gateways.net/
|
||||
This library can be used to upload/download files to Swarm via https://swarm-gateways.net/ (or an optionally provided gateway).
|
||||
|
||||
**Note that while this is a convenient feature as of today, it may not be present indefinitely.**
|
||||
|
||||
## Library usage
|
||||
|
||||
```js
|
||||
const swarmgw = require('swarmgw')
|
||||
const swarmgw = require('swarmgw')(/* opts */)
|
||||
|
||||
// This should output the hash: 931cc5a6bd57724ffd1adefc0ea6b4f0235497fca9e4f9ae4029476bcb51a8c6
|
||||
swarmgw.put('Hello from swarmgw!', function (err, ret) {
|
||||
@ -28,6 +28,10 @@ swarmgw.get('bzz-raw://931cc5a6bd57724ffd1adefc0ea6b4f0235497fca9e4f9ae4029476bc
|
||||
})
|
||||
```
|
||||
|
||||
The `opts` above is a map of options:
|
||||
- `gateway`: supply your own gateway URL, if not provided, it will use "swarm-gateways.net"
|
||||
- `mode`: can be `http` or `https` (default is `https`), ignore if `gateway` is provided
|
||||
|
||||
## CLI usage
|
||||
|
||||
It can also be used via the command line if installed globally (`npm install -g swarmgw`). To see the help: `swarmgw --help`.
|
||||
|
25
index.js
25
index.js
@ -4,7 +4,7 @@ function isValidHash (hash) {
|
||||
return /^[0-9a-f]{64}$/.test(hash)
|
||||
}
|
||||
|
||||
function getFile (url, cb) {
|
||||
function getFile (gateway, url, cb) {
|
||||
request('https://swarm-gateways.net/' + url, function (error, response, body) {
|
||||
if (error) {
|
||||
cb(error)
|
||||
@ -16,7 +16,7 @@ function getFile (url, cb) {
|
||||
})
|
||||
}
|
||||
|
||||
function putFile (content, cb) {
|
||||
function putFile (gateway, content, cb) {
|
||||
request({
|
||||
method: 'POST',
|
||||
uri: 'https://swarm-gateways.net/bzz-raw:/',
|
||||
@ -34,7 +34,22 @@ function putFile (content, cb) {
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get: getFile,
|
||||
put: putFile
|
||||
module.exports = function (opts) {
|
||||
opts = opts || {}
|
||||
var gateway
|
||||
if (opts.gateway) {
|
||||
gateway = opts.gateway
|
||||
} else if (opts.mode === 'http') {
|
||||
gateway = 'http://swarm-gateways.net'
|
||||
} else {
|
||||
gateway = 'https://swarm-gateways.net'
|
||||
}
|
||||
return {
|
||||
get: function (url, cb) {
|
||||
return getFile(gateway, url, cb)
|
||||
},
|
||||
put: function (content, cb) {
|
||||
return putFile(gateway, content, cb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user