embark/lib/modules/ipfs/process.js

134 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-05-28 16:49:44 +00:00
const child_process = require('child_process');
2018-07-27 21:33:50 +00:00
const ProcessWrapper = require('../../core/processes/processWrapper');
2018-05-28 15:48:27 +00:00
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) {
2018-05-28 15:48:27 +00:00
super();
this.cors = options.cors;
2018-07-20 14:54:02 +00:00
this.command = 'ipfs';
this.checkIPFSVersion();
2018-05-28 15:48:27 +00:00
this.startIPFSDaemon();
}
checkIPFSVersion() {
child_process.exec(this.command + ' --version', {silent: true}, (err, stdout, _stderr) => {
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) {
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`);
}
}
});
}
_bindChildEvents(childProcess){
2018-05-28 15:51:38 +00:00
const self = this;
2018-05-28 19:43:27 +00:00
childProcess.on('error', (err) => {
2018-05-28 19:43:27 +00:00
err = err.toString();
console.error('IPFS error: ', err);
});
childProcess.stderr.on('data', (data) => {
2018-05-28 19:43:27 +00:00
data = data.toString();
console.log(`IPFS error: ${data}`);
// `ipfs daemon called`, but `ipfs init` had not been run yet
if(!self.initCalled && data.indexOf('no IPFS repo found') > -1) {
self.initCalled = true;
let ipfsInitChild = child_process.spawn(this.command, ['init']);
self._bindChildEvents(ipfsInitChild);
}
2018-05-28 19:43:27 +00:00
});
childProcess.stdout.on('data', (data) => {
2018-05-28 19:43:27 +00:00
data = data.toString();
// ipfs init just run, and we have a successful result
// re-run `ipfs daemon`
if(self.initCalled && !self.readyCalled && data.indexOf('peer identity:') > -1) {
self.startIPFSDaemon();
}
else if (!self.readyCalled && data.indexOf('Daemon is ready') > -1) {
2018-05-28 15:51:38 +00:00
self.readyCalled = true;
// update IPFS cors before spawning a daemon (muhaha)
let ipfsCorsCmd = `${self.command} config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\\"${self.cors.join('\\", \\"')}\\"]"`;
console.trace(`Updating IPFS CORS using command: ${ipfsCorsCmd}`);
child_process.exec(ipfsCorsCmd, {silent: true}, (err, stdout, _stderr) => {
if(err){
err = err.toString();
console.error('IPFS CORS update error: ', err);
}
if(_stderr){
_stderr = _stderr.toString();
console.error(`IPFS CORS update error: ${_stderr}`);
}
child_process.exec(self.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\\"true\\"]"', {silent: true}, (err, stdout, _stderr) => {
if(err){
err = err.toString();
console.error('IPFS CORS update error: ', err);
}
if(_stderr){
_stderr = _stderr.toString();
console.error(`IPFS CORS update error: ${_stderr}`);
}
child_process.exec(self.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\\"PUT\\", \\"POST\\", \\"GET\\"]"', {silent: true}, (err, stdout, _stderr) => {
if(err){
err = err.toString();
console.error('IPFS CORS update error: ', err);
}
if(_stderr){
_stderr = _stderr.toString();
console.error(`IPFS CORS update error: ${_stderr}`);
}
self.send({result: constants.storage.initiated});
});
});
});
2018-05-28 15:51:38 +00:00
}
console.log('IPFS: ' + data);
});
childProcess.on('exit', (code) => {
2018-05-28 15:51:38 +00:00
if (code) {
console.error('IPFS exited with error code ' + code);
}
});
}
startIPFSDaemon() {
const self = this;
// spawn the daemon (muhaha)
this.child = child_process.spawn(this.command, ['daemon']);
self._bindChildEvents(this.child);
2018-05-28 15:48:27 +00:00
}
2018-05-30 14:52:15 +00:00
kill() {
if (this.child) {
this.child.kill();
}
}
2018-05-28 15:48:27 +00:00
}
process.on('message', (msg) => {
2018-05-30 14:52:15 +00:00
if (msg === 'exit') {
return ipfsProcess.kill();
}
if (msg.action === constants.storage.init) {
2018-05-28 15:48:27 +00:00
ipfsProcess = new IPFSProcess(msg.options);
}
});