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