Split modules working
Fixed issues with split modules not working Updated readme Clean up
This commit is contained in:
parent
a9fe1ff313
commit
16f587ad65
8
.babelrc
8
.babelrc
|
@ -5,24 +5,28 @@
|
|||
"browser": {
|
||||
"ignore": [
|
||||
"src/swarmjs.js",
|
||||
"src/index.js",
|
||||
"src/node/index.js",
|
||||
"src/standalone/index.js",
|
||||
"bin/swarmjs"
|
||||
],
|
||||
"plugins": [
|
||||
["@babel/plugin-transform-runtime", {
|
||||
"corejs": 2
|
||||
"corejs": 2,
|
||||
"useESModules": true
|
||||
}]
|
||||
],
|
||||
"presets": [
|
||||
["@babel/env", {
|
||||
"targets": {"node": "8.11.3"}
|
||||
"modules": false,
|
||||
"targets": {"browsers": ["last 1 version", "not dead", "> 0.2%"]}
|
||||
}]
|
||||
]
|
||||
},
|
||||
"node": {
|
||||
"ignore": [
|
||||
"src/browser.js",
|
||||
"src/standalone/index.js",
|
||||
"bin/swarmjs"
|
||||
],
|
||||
"plugins": [
|
||||
|
|
115
README.md
115
README.md
|
@ -1,43 +1,78 @@
|
|||
# Documentation needs to be updated. Please completely disregard for now.
|
||||
# SwarmJS
|
||||
A javascript library for interacting with [Swarm](https://swarm-guide.readthedocs.io/en/latest/), a decentralised and distributed storage platform.
|
||||
|
||||
# swarmjs
|
||||
|
||||
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 swarmjs = require('swarmjs')(/* opts */)
|
||||
|
||||
// This should output the hash: 931cc5a6bd57724ffd1adefc0ea6b4f0235497fca9e4f9ae4029476bcb51a8c6
|
||||
swarmjs.put('Hello from swarmjs!', function (err, ret) {
|
||||
if (err) {
|
||||
console.log('Failed to upload: ' + err)
|
||||
} else {
|
||||
console.log('Swarm hash: ' + ret)
|
||||
}
|
||||
})
|
||||
|
||||
// This should output the content: Hello from swarmjs!
|
||||
swarmjs.get('bzz-raw://931cc5a6bd57724ffd1adefc0ea6b4f0235497fca9e4f9ae4029476bcb51a8c6', function (err, ret) {
|
||||
if (err) {
|
||||
abort('Failed to download: ' + err)
|
||||
} else {
|
||||
console.log(ret)
|
||||
}
|
||||
})
|
||||
## Installation
|
||||
```
|
||||
npm install swarmjs --save
|
||||
```
|
||||
|
||||
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 swarmjs`). To see the help: `swarmjs --help`.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
## Basic usage
|
||||
#### First, import SwarmJS
|
||||
Using **CommonJS**:
|
||||
```
|
||||
const SwarmJS = require('swarmjs');
|
||||
```
|
||||
Or, with **ES6**:
|
||||
```
|
||||
import SwarmJS from 'swarmjs';
|
||||
```
|
||||
Then instantiate SwarmJS
|
||||
```
|
||||
// instantiate SwarmJS
|
||||
const swarmjs = new SwarmJS({ gateway: 'http://localhost:8500' });
|
||||
```
|
||||
Available options:
|
||||
| Option | Description | Default |
|
||||
| -----| ------------| ------- |
|
||||
| `gateway` | URL of the Swarm gateway, ie `http://localhost:8500`. | `swarm-gateways.net` |
|
||||
| `mode` | Protocol of the default gateway URL. If `gateway` is provided, this has no effect. | `https` |
|
||||
NOTE: if no options are provided, the default gateway URL will be `https://swarm-gateways.net`.
|
||||
##### Check gateway availability
|
||||
```
|
||||
// Check gateway availability
|
||||
swarmjs.isAvailable((err, isAvailable) => {
|
||||
if(err) return console.error('Error checking Swarm availability', err);
|
||||
console.log(`Gateway at 'http://localhost:8500' is ${isAvailable ? '' : 'un'}available`);
|
||||
});
|
||||
// > Gateway at 'http://localhost:8500' is available
|
||||
```
|
||||
##### Upload of raw content
|
||||
```
|
||||
// Upload of raw content
|
||||
let testHash;
|
||||
swarmjs.uploadRaw('test', (err, hash) => {
|
||||
if(err) return console.error('Error uploading contents', err);
|
||||
testHash = hash;
|
||||
console.log(`test can now be accessed from 'http://localhost:8500/bzz-raw:/${hash}'`);
|
||||
});
|
||||
// > test can now be accessed from 'http://localhost:8500/bzz-raw:/6de1faa7d29b1931b4ba3d44befcf7a5e43e947cd0bf2db154172bac5ecac3a6'
|
||||
```
|
||||
##### Upload file
|
||||
```
|
||||
// If you want to upload a file, first read the file's contents, then upload as the raw contents
|
||||
const fs = require('fs');
|
||||
fs.readFile('./index.html', (err, data) => {
|
||||
if (err) throw err;
|
||||
swarm.uploadRaw(data, (err, hash) => {
|
||||
if (err) return console.error('Error uploading file contents', err);
|
||||
console.log(`file contents can now be accessed from 'http://localhost:8500/bzz-raw:/${hash}'`);
|
||||
});
|
||||
});
|
||||
// > file contents can now be accessed from 'http://localhost:8500/bzz-raw:/178739cbbd084e90ae0cef3f95e4b92baa85e83edb1a52d28dc370277db9d457'
|
||||
```
|
||||
##### Upload directory (only available in NodeJS, not available in the browser!)
|
||||
```
|
||||
swarmjs.uploadDirectory('dist/', (err, hash) => {
|
||||
if(err) return console.error('Error uploading directory', err);
|
||||
console.log(``);
|
||||
});
|
||||
```
|
||||
##### Download content
|
||||
```
|
||||
// Download content via hash
|
||||
swarmjs.downloadRaw(testHash, (err, content) => {
|
||||
if(err) return console.error(err);
|
||||
console.log(`contents of our test: ${content}`);
|
||||
});
|
||||
// > contents of our test: test
|
||||
```
|
|
@ -15,6 +15,11 @@
|
|||
"not dead",
|
||||
"> 0.2%"
|
||||
],
|
||||
"files": [
|
||||
"dist",
|
||||
"swarmjs.min.js",
|
||||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"lint": "./node_modules/.bin/eslint src/",
|
||||
"babel:browser": "cross-env BABEL_ENV=browser babel --out-dir dist/browser src",
|
||||
|
|
127
src/browser.js
127
src/browser.js
|
@ -1,126 +1 @@
|
|||
const request = require('request');
|
||||
|
||||
//class SwarmJS {
|
||||
|
||||
// constructor(opts) {
|
||||
// this.opts = opts || {};
|
||||
// if (this.opts.gateway) {
|
||||
// this.gateway = opts.gateway
|
||||
// } else if (this.opts.mode === 'http') {
|
||||
// this.gateway = 'http://swarm-gateways.net'
|
||||
// } else {
|
||||
// this.gateway = 'https://swarm-gateways.net'
|
||||
// }
|
||||
// }
|
||||
|
||||
function SwarmJS(opts){
|
||||
this.opts = opts || {};
|
||||
if (this.opts.gateway) {
|
||||
this.gateway = opts.gateway
|
||||
} else if (this.opts.mode === 'http') {
|
||||
this.gateway = 'http://swarm-gateways.net'
|
||||
} else {
|
||||
this.gateway = 'https://swarm-gateways.net'
|
||||
}
|
||||
}
|
||||
|
||||
SwarmJS.prototype._isValidHash = (hash) => {
|
||||
return /^[0-9a-f]{64}$/.test(hash)
|
||||
}
|
||||
|
||||
SwarmJS.prototype._hashResponse = (error, response, body, cb) => {
|
||||
if (error) {
|
||||
cb(error)
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body)
|
||||
} else if (!this._isValidHash(body)) {
|
||||
cb('Invalid hash')
|
||||
} else {
|
||||
cb(null, body)
|
||||
}
|
||||
}
|
||||
|
||||
SwarmJS.prototype.download = (url, cb) => {
|
||||
request(`${this.gateway}/${url}`, (error, response, body) => {
|
||||
if (error) {
|
||||
cb(error)
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body)
|
||||
} else {
|
||||
cb(null, body)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
SwarmJS.prototype.downloadRaw = (hash, cb) => {
|
||||
this.download(`bzz-raw:/${hash}`, cb);
|
||||
}
|
||||
|
||||
SwarmJS.prototype.upload = (url, content, cb) => {
|
||||
request.post({
|
||||
url: `${this.gateway}/${url}`,
|
||||
body: content
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb))
|
||||
}
|
||||
|
||||
SwarmJS.prototype.uploadRaw = (content, cb) => {
|
||||
this.upload('bzz-raw:', content, cb);
|
||||
}
|
||||
|
||||
SwarmJS.prototype.uploadForm = (formData, cb) => {
|
||||
request.post({
|
||||
url: `${this.gateway}/bzz:/`,
|
||||
formData: formData
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
SwarmJS.prototype.isAvailable = (cb) => {
|
||||
const testContent = "test";
|
||||
const testHash = "6de1faa7d29b1931b4ba3d44befcf7a5e43e947cd0bf2db154172bac5ecac3a6";
|
||||
try {
|
||||
this.uploadRaw(testContent, (err, hash) => {
|
||||
if (err) return cb(err);
|
||||
cb(null, hash === testHash);
|
||||
});
|
||||
} catch (e) {
|
||||
cb(e);
|
||||
}
|
||||
}
|
||||
//}
|
||||
|
||||
module.exports = SwarmJS;
|
||||
|
||||
// (opts) => {
|
||||
// opts = opts || {};
|
||||
// if (!opts.gateway) {
|
||||
// if (opts.mode === 'http') {
|
||||
// opts.gateway = 'http://swarm-gateways.net';
|
||||
// } else {
|
||||
// opts.gateway = 'https://swarm-gateways.net';
|
||||
// }
|
||||
// }
|
||||
// //const swarmjs = new SwarmJS(opts);
|
||||
// // return {
|
||||
// // download: swarmjs.download,
|
||||
// // downloadRaw: swarmjs.downloadRaw,
|
||||
// // upload: swarmjs.upload,
|
||||
// // uploadRaw: swarmjs.uploadRaw,
|
||||
// // uploadForm: swarmjs.uploadForm,
|
||||
// // gateway: swarmjs.gateway,
|
||||
// // isAvailable: swarmjs.isAvailable,
|
||||
// // _hashResponse: swarmjs._hashResponse
|
||||
// // }
|
||||
// return {
|
||||
// download: download,
|
||||
// downloadRaw: downloadRaw,
|
||||
// upload: upload,
|
||||
// uploadRaw: uploadRaw,
|
||||
// uploadForm: uploadForm,
|
||||
// gateway: opts.gateway,
|
||||
// isAvailable: isAvailable,
|
||||
// _hashResponse: _hashResponse
|
||||
// }
|
||||
// }
|
||||
export {default} from './shared';
|
98
src/index.js
98
src/index.js
|
@ -1,24 +1,13 @@
|
|||
const request = require('request');
|
||||
const fs = require('fs');
|
||||
const klaw = require('klaw');
|
||||
const through2 = require('through2');
|
||||
const resolve = require('path').resolve;
|
||||
import _SwarmJS from './shared';
|
||||
import fs from 'fs';
|
||||
import klaw from 'klaw';
|
||||
import through2 from 'through2';
|
||||
|
||||
class SwarmJS {
|
||||
class SwarmJS extends _SwarmJS {
|
||||
|
||||
constructor(opts) {
|
||||
this.opts = opts || {};
|
||||
if (this.opts.gateway) {
|
||||
this.gateway = opts.gateway;
|
||||
} else if (this.opts.mode === 'http') {
|
||||
this.gateway = 'http://swarm-gateways.net';
|
||||
} else {
|
||||
this.gateway = 'https://swarm-gateways.net';
|
||||
}
|
||||
}
|
||||
|
||||
static _isValidHash(hash) {
|
||||
return (/^[0-9a-f]{64}$/).test(hash);
|
||||
constructor(opts){
|
||||
super(opts);
|
||||
}
|
||||
|
||||
_getDirectoryTreeReadable(directory, cb) {
|
||||
|
@ -26,9 +15,9 @@ class SwarmJS {
|
|||
let errors = [];
|
||||
|
||||
const excludeDirFilter = through2.obj(function (item, enc, next) {
|
||||
if (!item.stats.isDirectory()) this.push(item);
|
||||
next();
|
||||
});
|
||||
if (!item.stats.isDirectory()) this.push(item)
|
||||
next()
|
||||
})
|
||||
|
||||
klaw(directory)
|
||||
.pipe(excludeDirFilter)
|
||||
|
@ -47,53 +36,7 @@ class SwarmJS {
|
|||
.on('end', () => cb(errors, readables)); // => [ ... array of files]
|
||||
}
|
||||
|
||||
_hashResponse(error, response, body, cb) {
|
||||
if (error) {
|
||||
cb(error);
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body);
|
||||
} else if (!SwarmJS._isValidHash(body)) {
|
||||
cb('Invalid hash');
|
||||
} else {
|
||||
cb(null, body);
|
||||
}
|
||||
}
|
||||
|
||||
download(url, cb) {
|
||||
request(`${this.gateway}/${url}`, (error, response, body) => {
|
||||
if (error) {
|
||||
cb(error);
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body);
|
||||
} else {
|
||||
cb(null, body);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
downloadRaw(hash, cb) {
|
||||
this.download(`bzz-raw:/${hash}`, cb);
|
||||
}
|
||||
|
||||
upload(url, content, cb) {
|
||||
request.post({
|
||||
url: `${this.gateway}/${url}`,
|
||||
body: content
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb));
|
||||
}
|
||||
|
||||
uploadRaw(content, cb) {
|
||||
this.upload('bzz-raw:', content, cb);
|
||||
}
|
||||
|
||||
uploadForm(formData, cb){
|
||||
request.post({
|
||||
url: `${this.gateway}/bzz:/`,
|
||||
formData: formData
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb));
|
||||
}
|
||||
|
||||
uploadDirectory(path, cb) {
|
||||
uploadDirectory(path, defaultPath, cb) {
|
||||
this._getDirectoryTreeReadable(`${resolve(path)}/`, (errors, readables) => {
|
||||
const hasReadables = Boolean(Object.keys(readables).length);
|
||||
if (errors.length && !hasReadables) {
|
||||
|
@ -103,24 +46,11 @@ class SwarmJS {
|
|||
console.trace(errors.join('\n'));
|
||||
}
|
||||
if (hasReadables) {
|
||||
return this.uploadForm(readables, cb);
|
||||
return this.uploadForm(readables, defaultPath, cb);
|
||||
}
|
||||
cb('No files to upload');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
isAvailable(cb) {
|
||||
const testContent = "test";
|
||||
const testHash = "6de1faa7d29b1931b4ba3d44befcf7a5e43e947cd0bf2db154172bac5ecac3a6";
|
||||
try {
|
||||
this.uploadRaw(testContent, (err, hash) => {
|
||||
if (err) return cb(err);
|
||||
cb(null, hash === testHash);
|
||||
});
|
||||
} catch (e) {
|
||||
cb(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports = SwarmJS;
|
||||
|
||||
export default SwarmJS;
|
||||
|
|
|
@ -1 +1 @@
|
|||
module.exports = require('../swarmjs').default;
|
||||
module.exports = require('../index').default;
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
const request = require('request');
|
||||
|
||||
class _SwarmJS {
|
||||
|
||||
constructor(opts) {
|
||||
this.opts = opts || {};
|
||||
if (this.opts.gateway) {
|
||||
this.gateway = opts.gateway
|
||||
} else if (this.opts.mode === 'http') {
|
||||
this.gateway = 'http://swarm-gateways.net'
|
||||
} else {
|
||||
this.gateway = 'https://swarm-gateways.net'
|
||||
}
|
||||
}
|
||||
|
||||
_isValidHash(hash) {
|
||||
return /^[0-9a-f]{64}$/.test(hash)
|
||||
}
|
||||
|
||||
_hashResponse(error, response, body, cb) {
|
||||
if (error) {
|
||||
cb(error)
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body)
|
||||
} else if (!this._isValidHash(body)) {
|
||||
cb('Invalid hash')
|
||||
} else {
|
||||
cb(null, body)
|
||||
}
|
||||
}
|
||||
|
||||
download(url, cb) {
|
||||
request(`${this.gateway}/${url}`, (error, response, body) => {
|
||||
if (error) {
|
||||
cb(error)
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body)
|
||||
} else {
|
||||
cb(null, body)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
downloadRaw(hash, cb) {
|
||||
this.download(`bzz-raw:/${hash}`, cb);
|
||||
}
|
||||
|
||||
upload(url, content, cb) {
|
||||
request.post({
|
||||
url: `${this.gateway}/${url}`,
|
||||
body: content
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb))
|
||||
}
|
||||
|
||||
uploadRaw(content, cb) {
|
||||
this.upload('bzz-raw:/', content, cb);
|
||||
}
|
||||
|
||||
uploadForm(formData, defaultPath = 'index.html', cb) {
|
||||
let postObj = {
|
||||
url: `${this.gateway}/bzz:/`,
|
||||
formData: formData,
|
||||
qs: { defaultpath: defaultPath }
|
||||
};
|
||||
|
||||
request.post(postObj, (error, response, body) => this._hashResponse(error, response, body, cb));
|
||||
}
|
||||
|
||||
isAvailable(cb) {
|
||||
const testContent = "test";
|
||||
const testHash = "6de1faa7d29b1931b4ba3d44befcf7a5e43e947cd0bf2db154172bac5ecac3a6";
|
||||
try {
|
||||
this.uploadRaw(testContent, (err, hash) => {
|
||||
if (err) return cb(err);
|
||||
cb(null, hash === testHash);
|
||||
});
|
||||
} catch (e) {
|
||||
cb(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
export default _SwarmJS;
|
|
@ -1 +0,0 @@
|
|||
export {default} from './index';
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue