embark-area-51/lib/processes/storageProcesses/swarm.js

73 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-05-28 16:54:06 +00:00
const child_process = require('child_process');
2018-05-28 15:50:01 +00:00
const ProcessWrapper = require('../../process/processWrapper');
const constants = require('../../constants');
2018-05-28 19:37:25 +00:00
const fs = require('../../core/fs');
2018-05-28 15:50:01 +00:00
let swarmProcess;
class SwarmProcess extends ProcessWrapper {
constructor(options) {
super();
this.storageConfig = options.storageConfig;
this.webServerConfig = options.webServerConfig;
2018-05-28 15:50:01 +00:00
}
startSwarmDaemon() {
2018-05-28 15:52:49 +00:00
const self = this;
2018-05-28 15:50:39 +00:00
if (!this.storageConfig.account || !this.storageConfig.account.address || !this.storageConfig.account.password) {
2018-05-28 15:50:01 +00:00
return 'Account address and password are needed in the storage config to start the Swarm process';
}
let corsDomain = 'http://localhost:8000';
if (self.webServerConfig && self.webServerConfig && self.webServerConfig.host && self.webServerConfig.port) {
corsDomain = `http://${self.webServerConfig.host}:${self.webServerConfig.port}`;
}
2018-05-28 19:37:25 +00:00
const args = [
`--bzzaccount=${this.storageConfig.account.address}`,
`--password=${fs.dappPath(this.storageConfig.account.password)}`,
`--corsdomain=${corsDomain}`,
`--ens-api=''`
];
const child = child_process.spawn(this.storageConfig.swarmPath || 'swarm', args, {shell: true});
child.on('error', (err) => {
err = err.toString();
console.error('Swarm error: ', err);
});
2018-05-24 14:25:32 +00:00
let lastMessage;
2018-05-28 19:37:25 +00:00
child.stdout.on('data', (data) => {
data = data.toString();
console.log(`Swarm error: ${data}`);
});
// Geth logs appear in stderr somehow
2018-05-28 15:52:49 +00:00
child.stderr.on('data', (data) => {
2018-05-28 19:37:25 +00:00
data = data.toString();
2018-05-24 14:25:32 +00:00
if (!self.readyCalled && data.indexOf('Swarm http proxy started') > -1) {
self.readyCalled = true;
self.send({result: constants.storage.initiated});
}
lastMessage = data;
console.log('Swarm: ' + data);
});
child.on('exit', (code) => {
if (code) {
console.error('Swarm exited with error code ' + code);
2018-05-28 19:37:25 +00:00
if (lastMessage) {
console.error(lastMessage);
}
2018-05-24 14:25:32 +00:00
}
});
2018-05-28 15:50:01 +00:00
}
}
process.on('message', (msg) => {
if (msg.action === constants.storage.init) {
swarmProcess = new SwarmProcess(msg.options);
const error = swarmProcess.startSwarmDaemon();
2018-05-24 14:25:32 +00:00
if (error) {
swarmProcess.send({result: constants.storage.initiated, error});
}
2018-05-28 15:50:01 +00:00
}
});