2018-05-30 06:34:36 +00:00
|
|
|
const UploadSwarm = require('./upload.js');
|
|
|
|
const utils = require('../../utils/utils.js');
|
|
|
|
const fs = require('../../core/fs.js');
|
|
|
|
const Web3Bzz = require('web3-bzz');
|
2018-05-31 10:18:25 +00:00
|
|
|
const _ = require('underscore');
|
2017-12-27 00:55:42 +00:00
|
|
|
|
|
|
|
class Swarm {
|
|
|
|
|
|
|
|
constructor(embark, options) {
|
|
|
|
this.logger = embark.logger;
|
2018-04-30 05:56:43 +00:00
|
|
|
this.events = embark.events;
|
|
|
|
this.buildDir = options.buildDir;
|
2018-05-25 07:13:57 +00:00
|
|
|
this.storageConfig = options.storageConfig;
|
2018-04-30 05:56:43 +00:00
|
|
|
this.addCheck = options.addCheck;
|
|
|
|
this.embark = embark;
|
2018-05-30 06:34:36 +00:00
|
|
|
|
|
|
|
let host = options.host || options.storageConfig.upload.host;
|
|
|
|
let port = options.port || options.storageConfig.upload.port;
|
|
|
|
if(port) port = ':' + port;
|
|
|
|
else port = '';
|
|
|
|
let protocol = options.protocol || options.storageConfig.upload.protocol || 'http';
|
|
|
|
this.providerUrl = `${protocol}://${host}${port}`;
|
2018-05-31 10:18:25 +00:00
|
|
|
this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/';
|
2018-05-30 06:34:36 +00:00
|
|
|
|
|
|
|
this.bzz = new Web3Bzz(this.providerUrl);
|
2018-05-25 07:13:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
commandlineDeploy() {
|
2017-12-27 00:55:42 +00:00
|
|
|
this.upload_swarm = new UploadSwarm({
|
2018-04-30 05:56:43 +00:00
|
|
|
buildDir: this.buildDir || 'dist/',
|
|
|
|
storageConfig: this.storageConfig,
|
2018-05-30 06:34:36 +00:00
|
|
|
connectUrl: this.providerUrl,
|
|
|
|
getUrl: this.getUrl,
|
2018-04-30 05:56:43 +00:00
|
|
|
bzz: this.bzz
|
2017-12-27 00:55:42 +00:00
|
|
|
});
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
this.embark.registerUploadCommand('swarm', this.upload_swarm.deploy.bind(this.upload_swarm));
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
|
|
|
|
setServiceCheck() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
let storageConfig = this.storageConfig;
|
|
|
|
|
|
|
|
if (!storageConfig.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-31 10:18:25 +00:00
|
|
|
if (_.findWhere(this.storageConfig.dappConnection, {'provider': 'swarm'}) === undefined && (storageConfig.upload.provider !== 'swarm' || storageConfig.available_providers.indexOf("swarm") < 0)) {
|
2018-04-30 05:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.events.on('check:backOnline:Swarm', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('Swarm node detected...'));
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.events.on('check:wentOffline:Swarm', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('Swarm node is offline...'));
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!this.addCheck) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add check for console
|
|
|
|
this.addCheck('Swarm', function(cb){
|
|
|
|
self.logger.trace("Checking Swarm availability...");
|
|
|
|
self.bzz.isAvailable().then(result => {
|
|
|
|
return cb({name: "Swarm ", status: result ? 'on':'off'});
|
|
|
|
}).catch(err => {
|
|
|
|
self.logger.trace("Check Swarm availability error: " + err);
|
|
|
|
return cb({name: "Swarm ", status: 'off'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
addProviderToEmbarkJS() {
|
2018-05-25 07:13:57 +00:00
|
|
|
let self = this;
|
2018-04-30 05:56:43 +00:00
|
|
|
// TODO: make this a shouldAdd condition
|
|
|
|
if (this.storageConfig === {}) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-31 10:18:25 +00:00
|
|
|
if (this.storageConfig.available_providers.indexOf('swarm') < 0 || _.findWhere(this.storageConfig.dappConnection, {'provider': 'swarm'}) === undefined || this.storageConfig.enabled !== true) {
|
2018-04-30 05:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:13:57 +00:00
|
|
|
this.events.request("version:get:p-iteration", function(pIterationVersion) {
|
|
|
|
let currentPIterationVersion = require('../../../package.json').dependencies["p-iteration"];
|
|
|
|
if (pIterationVersion !== currentPIterationVersion) {
|
|
|
|
self.events.request("version:getPackageLocation", "p-iteration", pIterationVersion, function(err, location) {
|
|
|
|
self.embark.registerImportFile("p-iteration", fs.dappPath(location));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
let code = "";
|
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
|
|
|
code += "\nEmbarkJS.Storage.registerProvider('swarm', __embarkSwarm);";
|
|
|
|
|
|
|
|
this.embark.addCodeToEmbarkJS(code);
|
|
|
|
}
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Swarm;
|
|
|
|
|