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-06-01 15:58:11 +00:00
|
|
|
this.storageConfig = embark.config.storageConfig;
|
|
|
|
this.host = options.host || this.storageConfig.host;
|
|
|
|
this.port = options.port || this.storageConfig.port;
|
2018-04-30 05:56:43 +00:00
|
|
|
this.embark = embark;
|
2018-06-01 15:58:11 +00:00
|
|
|
|
2018-06-01 03:12:17 +00:00
|
|
|
this.providerUrl = utils.buildUrl(options.protocol || options.storageConfig.upload.protocol, options.host || options.storageConfig.upload.host, options.port || options.storageConfig.upload.port);
|
2018-06-01 15:58:11 +00:00
|
|
|
|
2018-05-31 10:18:25 +00:00
|
|
|
this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/';
|
2018-06-01 15:58:11 +00:00
|
|
|
|
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
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-06-01 15:58:11 +00:00
|
|
|
self.events.request("services:register", 'Swarm', function(cb){
|
2018-06-05 01:39:28 +00:00
|
|
|
self.logger.trace(`Checking Swarm availability on ${self.bzz.currentProvider}...`);
|
2018-04-30 05:56:43 +00:00
|
|
|
self.bzz.isAvailable().then(result => {
|
2018-06-05 01:39:28 +00:00
|
|
|
self.logger.trace("Swarm " + (result ? '':'un') + "available");
|
2018-04-30 05:56:43 +00:00
|
|
|
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) {
|
2018-06-14 23:37:52 +00:00
|
|
|
if(err){
|
|
|
|
return self.logger.error("Error getting package location for p-iteration: " + err);
|
2018-06-14 08:09:02 +00:00
|
|
|
}
|
2018-06-14 23:37:52 +00:00
|
|
|
self.embark.registerImportFile("p-iteration", fs.dappPath(location));
|
2018-05-25 07:13:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
|