2018-05-28 16:49:44 +00:00
|
|
|
const child_process = require('child_process');
|
2018-05-28 15:48:27 +00:00
|
|
|
const ProcessWrapper = require('../../process/processWrapper');
|
|
|
|
const constants = require('../../constants');
|
|
|
|
|
2018-05-28 15:51:38 +00:00
|
|
|
let ipfsProcess; // eslint-disable-line no-unused-vars
|
2018-05-28 15:48:27 +00:00
|
|
|
|
|
|
|
class IPFSProcess extends ProcessWrapper {
|
|
|
|
constructor(_options) {
|
|
|
|
super();
|
|
|
|
|
2018-05-25 17:48:56 +00:00
|
|
|
this.checkIPFSVersion();
|
2018-05-28 15:48:27 +00:00
|
|
|
this.startIPFSDaemon();
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:48:56 +00:00
|
|
|
checkIPFSVersion() {
|
2018-05-28 16:49:44 +00:00
|
|
|
child_process.exec('ipfs --version', {silent: true}, (err, stdout, _stderr) => {
|
2018-05-25 17:48:56 +00:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const match = stdout.match(/[0-9]+\.[0-9]+\.[0-9]+/);
|
|
|
|
if (match[0]) {
|
|
|
|
const versions = match[0].split('.');
|
2018-05-25 17:50:44 +00:00
|
|
|
if (versions[0] <= 0 && versions[1] <= 4 && versions[2] <= 14) {
|
2018-05-25 17:48:56 +00:00
|
|
|
console.error(`You are using IPFS version ${match[0]} which has an issue with processes.`);
|
|
|
|
console.error(`Please update to IPFS version 0.4.15 or more recent: https://github.com/ipfs/ipfs-update`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-28 15:48:27 +00:00
|
|
|
startIPFSDaemon() {
|
2018-05-28 15:51:38 +00:00
|
|
|
const self = this;
|
2018-05-28 16:49:44 +00:00
|
|
|
const child = child_process.spawn('ipfs', ['daemon']);
|
2018-05-28 15:51:38 +00:00
|
|
|
let lastMessage;
|
|
|
|
child.stdout.on('data', (data) => {
|
|
|
|
if (!self.readyCalled && data.indexOf('Daemon is ready') > -1) {
|
|
|
|
self.readyCalled = true;
|
|
|
|
self.send({result: constants.storage.initiated});
|
|
|
|
}
|
|
|
|
lastMessage = data;
|
|
|
|
console.log('IPFS: ' + data);
|
|
|
|
});
|
|
|
|
child.on('exit', (code) => {
|
|
|
|
if (code) {
|
|
|
|
console.error('IPFS exited with error code ' + code);
|
|
|
|
console.error(lastMessage);
|
|
|
|
}
|
|
|
|
});
|
2018-05-28 15:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on('message', (msg) => {
|
2018-05-23 19:10:53 +00:00
|
|
|
if (msg.action === constants.storage.init) {
|
2018-05-28 15:48:27 +00:00
|
|
|
ipfsProcess = new IPFSProcess(msg.options);
|
|
|
|
}
|
|
|
|
});
|