Constructor refactor, clean up
Linting, remove swarmjs.min, format spaces Refactored constructor of SwarmJS to remove `mode` option. By default, if no `gateway` option is passed in, the default gateway will be `https://swarm-gateways.net`. Removed the `else if` and only list `swarm-gateways.net` once.
This commit is contained in:
parent
99a8b38875
commit
830040021d
|
@ -33,7 +33,7 @@ package
|
|||
.vscode
|
||||
.eslintrc.json
|
||||
|
||||
swarm.min.js
|
||||
swarmjs.min.js
|
||||
swarmjs-*.tgz
|
||||
NOTES
|
||||
npm-debug.log
|
||||
|
|
|
@ -27,10 +27,9 @@ 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` |
|
||||
| `gateway` | URL of the Swarm gateway, ie `http://localhost:8500`. | `https://swarm-gateways.net` |
|
||||
|
||||
NOTE: If no options are provided, the default gateway URL will be `https://swarm-gateways.net`. This means you don't necessarily need to [run your own Swarm node](https://swarm-guide.readthedocs.io/en/latest/gettingstarted.html), however there is an upload limit of ~2.5MB and no guarantees about permanence. It is recommended to run your own Swarm node.
|
||||
NOTE: If no options are provided, the default gateway URL will be `https://swarm-gateways.net`. This means you don't necessarily need to [run your own Swarm node](https://swarm-guide.readthedocs.io/en/latest/gettingstarted.html), however there is an upload limit of ~2.5MB and no guarantees regarding permanence. *It is recommended to run your own Swarm node.*
|
||||
##### Check gateway availability
|
||||
```
|
||||
// Check gateway availability
|
||||
|
|
26
bin/swarmjs
26
bin/swarmjs
|
@ -29,12 +29,12 @@ const yargs = require('yargs')
|
|||
.version()
|
||||
.showHelpOnFail(false, 'Specify --help for available options')
|
||||
.help()
|
||||
.demand(1, 'Must provide a command')
|
||||
.demand(1, 'Must provide a command');
|
||||
|
||||
const argv = yargs.argv;
|
||||
const gateway = argv.gateway;
|
||||
const command = argv._[0];
|
||||
const swarmjs = new SwarmJS({ gateway });
|
||||
const swarmjs = new SwarmJS({gateway});
|
||||
|
||||
function abort(msg) {
|
||||
console.log(msg || 'Error occured');
|
||||
|
@ -45,22 +45,24 @@ switch (command) {
|
|||
case 'get':
|
||||
swarmjs.downloadRaw(argv.hash, function (err, ret) {
|
||||
if (err) {
|
||||
abort('Failed to download: ' + err)
|
||||
abort('Failed to download: ' + err);
|
||||
} else {
|
||||
console.log(ret)
|
||||
console.log(ret);
|
||||
}
|
||||
})
|
||||
break
|
||||
});
|
||||
break;
|
||||
case 'put':
|
||||
swarmjs.uploadRaw(fs.readFileSync(argv.filename), function (err, ret) {
|
||||
if (err) {
|
||||
console.log('Failed to upload: ' + err)
|
||||
console.log('Failed to upload: ' + err);
|
||||
} else {
|
||||
console.log('Swarm hash: ' + ret)
|
||||
console.log('Swarm hash: ' + ret);
|
||||
}
|
||||
})
|
||||
break
|
||||
});
|
||||
break;
|
||||
case 'calculate':
|
||||
console.log('Swarm hash: ' + swarmhash(fs.readFileSync(argv.filename)).toString('hex'))
|
||||
break
|
||||
console.log('Swarm hash: ' + swarmhash(fs.readFileSync(argv.filename)).toString('hex'));
|
||||
break;
|
||||
default:
|
||||
console.log('Invalid arguments. Type \'swarmjs --help\' for valid options');
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
export {default} from './shared';
|
||||
export {default} from './shared';
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* eslint no-useless-constructor: "off" */
|
||||
const resolve = require('path').resolve;
|
||||
import _SwarmJS from './shared';
|
||||
import fs from 'fs';
|
||||
|
@ -15,9 +16,9 @@ class SwarmJS extends _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)
|
||||
|
|
|
@ -4,39 +4,43 @@ class _SwarmJS {
|
|||
|
||||
constructor(opts) {
|
||||
this.opts = opts || {};
|
||||
this.gateway = 'https://swarm-gateways.net';
|
||||
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'
|
||||
this.gateway = opts.gateway;
|
||||
}
|
||||
}
|
||||
|
||||
_isValidHash(hash) {
|
||||
return /^[0-9a-f]{64}$/.test(hash)
|
||||
return (/^[0-9a-f]{64}$/).test(hash);
|
||||
}
|
||||
|
||||
_contentResponse(error, response, body, cb) {
|
||||
if (error) {
|
||||
cb(error);
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body);
|
||||
} else {
|
||||
cb(null, body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
_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)
|
||||
}
|
||||
this._contentResponse(error, response, body, (err, body) => {
|
||||
if (err) return cb(err);
|
||||
if (!this._isValidHash(body)) return cb('Invalid hash');
|
||||
return cb(null, body);
|
||||
});
|
||||
}
|
||||
|
||||
download(url, cb) {
|
||||
request(`${this.gateway}/${url}`, (error, response, body) => {
|
||||
if (error) {
|
||||
cb(error)
|
||||
cb(error);
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body)
|
||||
cb(body);
|
||||
} else {
|
||||
cb(null, body)
|
||||
cb(null, body);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -49,7 +53,7 @@ class _SwarmJS {
|
|||
request.post({
|
||||
url: `${this.gateway}/${url}`,
|
||||
body: content
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb))
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb));
|
||||
}
|
||||
|
||||
uploadRaw(content, cb) {
|
||||
|
@ -60,7 +64,7 @@ class _SwarmJS {
|
|||
let postObj = {
|
||||
url: `${this.gateway}/bzz:/`,
|
||||
formData: formData,
|
||||
qs: { defaultpath: defaultPath }
|
||||
qs: {defaultpath: defaultPath}
|
||||
};
|
||||
|
||||
request.post(postObj, (error, response, body) => this._hashResponse(error, response, body, cb));
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,26 +1,24 @@
|
|||
const path = require('path');
|
||||
|
||||
const standalone = {
|
||||
entry: path.join(__dirname, 'dist/browser', 'browser.js'),
|
||||
mode: 'production',
|
||||
// optimization: {
|
||||
// minimize: false
|
||||
// },
|
||||
output: {
|
||||
filename: 'swarmjs.min.js',
|
||||
globalObject: 'typeof self !== \'undefined\' ? self : this',
|
||||
library: 'SwarmJS',
|
||||
libraryTarget: 'umd',
|
||||
libraryExport: 'default',
|
||||
path: __dirname,
|
||||
umdNamedDefine: true,
|
||||
},
|
||||
target: 'web',
|
||||
node: {
|
||||
fs: 'empty'
|
||||
}
|
||||
entry: path.join(__dirname, 'dist/browser', 'browser.js'),
|
||||
mode: 'production',
|
||||
// optimization: {
|
||||
// minimize: false
|
||||
// },
|
||||
output: {
|
||||
filename: 'swarmjs.min.js',
|
||||
globalObject: 'typeof self !== \'undefined\' ? self : this',
|
||||
library: 'SwarmJS',
|
||||
libraryTarget: 'umd',
|
||||
libraryExport: 'default',
|
||||
path: __dirname,
|
||||
umdNamedDefine: true
|
||||
},
|
||||
target: 'web',
|
||||
node: {
|
||||
fs: 'empty'
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = [
|
||||
standalone
|
||||
];
|
||||
module.exports = [standalone];
|
||||
|
|
Loading…
Reference in New Issue