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-07-20 15:55:17 +00:00
|
|
|
// TODO: not great, breaks module isolation
|
|
|
|
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
|
2017-12-27 00:55:42 +00:00
|
|
|
|
|
|
|
class Swarm {
|
|
|
|
|
2018-07-08 22:21:27 +00:00
|
|
|
constructor(embark, _options) {
|
2017-12-27 00:55:42 +00:00
|
|
|
this.logger = embark.logger;
|
2018-04-30 05:56:43 +00:00
|
|
|
this.events = embark.events;
|
2018-07-08 17:40:06 +00:00
|
|
|
this.buildDir = embark.config.buildDir;
|
2018-06-01 15:58:11 +00:00
|
|
|
this.storageConfig = embark.config.storageConfig;
|
2018-07-08 17:40:06 +00:00
|
|
|
this.host = this.storageConfig.host;
|
|
|
|
this.port = this.storageConfig.port;
|
2018-04-30 05:56:43 +00:00
|
|
|
this.embark = embark;
|
2018-06-01 15:58:11 +00:00
|
|
|
|
2018-07-07 21:46:15 +00:00
|
|
|
this.webServerConfig = embark.config.webServerConfig;
|
|
|
|
this.blockchainConfig = embark.config.blockchainConfig;
|
2018-07-07 16:29:04 +00:00
|
|
|
|
2018-07-08 17:40:06 +00:00
|
|
|
this.providerUrl = utils.buildUrl(this.storageConfig.upload.protocol, this.storageConfig.upload.host, this.storageConfig.upload.port);
|
2018-06-01 15:58:11 +00:00
|
|
|
|
2018-07-08 17:40:06 +00:00
|
|
|
this.getUrl = this.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/';
|
2018-06-01 15:58:11 +00:00
|
|
|
|
2018-07-08 18:28:43 +00:00
|
|
|
if (!this.isSwarmEnabledInTheConfig()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 06:34:36 +00:00
|
|
|
this.bzz = new Web3Bzz(this.providerUrl);
|
2018-07-07 16:29:04 +00:00
|
|
|
|
|
|
|
this.setServiceCheck();
|
|
|
|
this.addProviderToEmbarkJS();
|
|
|
|
this.startProcess(() => {});
|
2018-07-08 17:40:06 +00:00
|
|
|
this.registerUploadCommand();
|
2018-07-07 17:56:34 +00:00
|
|
|
|
|
|
|
this._checkService((err) => {
|
|
|
|
if (!err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.logger.info("Swarm node not found, attempting to start own node");
|
|
|
|
self.startProcess(() => {});
|
|
|
|
});
|
2018-05-25 07:13:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
setServiceCheck() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
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-07-07 17:56:34 +00:00
|
|
|
self._checkService((err, result) => {
|
|
|
|
if (err) {
|
|
|
|
self.logger.trace("Check Swarm availability error: " + err);
|
|
|
|
return cb({name: "Swarm ", status: 'off'});
|
|
|
|
}
|
|
|
|
self.logger.trace("Swarm " + (result ? '':'on') + "available");
|
2018-04-30 05:56:43 +00:00
|
|
|
return cb({name: "Swarm ", status: result ? 'on':'off'});
|
2018-07-07 17:56:34 +00:00
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-07 17:56:34 +00:00
|
|
|
_checkService(cb) {
|
|
|
|
this.bzz.isAvailable().then(result => {
|
|
|
|
cb(null, result);
|
|
|
|
}).catch(cb);
|
|
|
|
}
|
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
addProviderToEmbarkJS() {
|
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);
|
|
|
|
}
|
2018-07-07 16:29:04 +00:00
|
|
|
|
|
|
|
startProcess(callback) {
|
|
|
|
let self = this;
|
|
|
|
const storageProcessesLauncher = new StorageProcessesLauncher({
|
|
|
|
logger: self.logger,
|
|
|
|
events: self.events,
|
|
|
|
storageConfig: self.storageConfig,
|
|
|
|
webServerConfig: self.webServerConfig,
|
|
|
|
blockchainConfig: self.blockchainConfig
|
|
|
|
});
|
|
|
|
self.logger.trace(`Storage module: Launching swarm process...`);
|
2018-07-08 17:43:41 +00:00
|
|
|
return storageProcessesLauncher.launchProcess('swarm', callback);
|
2018-07-07 16:29:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 12:08:00 +00:00
|
|
|
registerUploadCommand() {
|
2018-07-08 17:40:06 +00:00
|
|
|
const self = this;
|
2018-07-27 12:08:00 +00:00
|
|
|
this.embark.registerUploadCommand('swarm', (cb) => {
|
2018-07-08 17:40:06 +00:00
|
|
|
let upload_swarm = new UploadSwarm({
|
|
|
|
buildDir: self.buildDir || 'dist/',
|
|
|
|
storageConfig: self.storageConfig,
|
|
|
|
getUrl: self.getUrl,
|
|
|
|
bzz: self.bzz
|
|
|
|
});
|
|
|
|
|
|
|
|
upload_swarm.deploy(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-08 18:14:44 +00:00
|
|
|
isSwarmEnabledInTheConfig() {
|
2018-07-08 22:21:27 +00:00
|
|
|
let {enabled, available_providers, dappConnection} = this.storageConfig;
|
2018-07-08 18:14:44 +00:00
|
|
|
return enabled && (available_providers.indexOf('swarm') > 0 || dappConnection.find(c => c.provider === 'swarm'));
|
|
|
|
}
|
|
|
|
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Swarm;
|