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');
|
2018-09-12 02:43:10 +00:00
|
|
|
const SwarmAPI = require('swarm-api');
|
2018-07-20 15:55:17 +00:00
|
|
|
// TODO: not great, breaks module isolation
|
|
|
|
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
|
2018-09-05 06:07:30 +00:00
|
|
|
const constants = require('../../constants.json');
|
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-09-10 00:21:52 +00:00
|
|
|
|
2018-09-12 02:43:10 +00:00
|
|
|
this.swarm = new SwarmAPI({gateway: this.providerUrl});
|
2018-07-07 16:29:04 +00:00
|
|
|
|
|
|
|
this.setServiceCheck();
|
|
|
|
this.addProviderToEmbarkJS();
|
2018-09-10 00:21:52 +00:00
|
|
|
this.addObjectToConsole();
|
2018-07-08 17:40:06 +00:00
|
|
|
this.registerUploadCommand();
|
2018-07-07 17:56:34 +00:00
|
|
|
|
2018-09-05 06:07:30 +00:00
|
|
|
// swarm needs geth to be running first
|
|
|
|
this.events.once(constants.blockchain.blockchainReady, () => {
|
|
|
|
this.swarm.isAvailable((err, isAvailable) => {
|
2018-09-06 11:57:10 +00:00
|
|
|
if (!err || isAvailable) {
|
2018-09-05 06:07:30 +00:00
|
|
|
this.logger.info("Swarm node found, using currently running node");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.logger.info("SWARM: Swarm node not found, attempting to start own node");
|
2018-09-15 18:48:53 +00:00
|
|
|
this.listenToCommands();
|
|
|
|
this.registerConsoleCommands();
|
2018-09-05 06:07:30 +00:00
|
|
|
return this.startProcess(() => {});
|
|
|
|
});
|
2018-07-07 17:56:34 +00:00
|
|
|
});
|
2018-05-25 07:13:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 00:21:52 +00:00
|
|
|
addObjectToConsole() {
|
|
|
|
this.events.emit("runcode:register", "swarm", this.swarm);
|
|
|
|
}
|
|
|
|
|
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-09-05 06:07:30 +00:00
|
|
|
self.events.request("services:register", 'Swarm', function (cb) {
|
2018-08-16 10:41:18 +00:00
|
|
|
self.logger.trace(`Checking Swarm availability on ${self.providerUrl}...`);
|
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'});
|
|
|
|
}
|
2018-09-10 06:27:19 +00:00
|
|
|
self.logger.trace("Swarm " + (result ? '' : 'un') + "available");
|
2018-09-05 06:07:30 +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) {
|
2018-08-16 10:41:18 +00:00
|
|
|
this.swarm.isAvailable(cb);
|
2018-07-07 17:56:34 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2018-10-23 09:26:15 +00:00
|
|
|
corsParts: self.embark.config.corsParts,
|
2018-10-23 15:19:15 +00:00
|
|
|
blockchainConfig: self.blockchainConfig,
|
2018-08-02 16:04:34 +00:00
|
|
|
embark: self.embark
|
2018-07-07 16:29:04 +00:00
|
|
|
});
|
|
|
|
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,
|
2018-09-10 06:27:19 +00:00
|
|
|
providerUrl: self.providerUrl,
|
2018-09-27 05:35:37 +00:00
|
|
|
swarm: self.swarm,
|
|
|
|
env: self.embark.env
|
2018-07-08 17:40:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
upload_swarm.deploy(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-15 18:48:53 +00:00
|
|
|
listenToCommands() {
|
|
|
|
this.events.setCommandHandler('logs:swarm:turnOn', (cb) => {
|
|
|
|
this.events.emit('logs:storage:enable');
|
|
|
|
return cb(null, 'Enabling Swarm logs');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.events.setCommandHandler('logs:swarm:turnOff', (cb) => {
|
|
|
|
this.events.emit('logs:storage:disable');
|
|
|
|
return cb(null, 'Disabling Swarm logs');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
registerConsoleCommands() {
|
|
|
|
const self = this;
|
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => cmd === 'log swarm on',
|
|
|
|
process: (cb) => self.events.request('logs:swarm:turnOn', cb)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => cmd === 'log swarm off',
|
|
|
|
process: (cb) => self.events.request('logs:swarm:turnOff', 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;
|