mirror of https://github.com/embarklabs/embark.git
fix(@embark/ipfs): Fix IPFS gateway CORS for embark-status plugin
As well as the IPFS API CORS, update the IPFS gateway CORS to successfully run the `embark-status` plugin.
This commit is contained in:
parent
774ae4af9a
commit
e4d1e4ea87
|
@ -1,10 +1,11 @@
|
||||||
const child_process = require('child_process');
|
const child_process = require('child_process');
|
||||||
const ProcessWrapper = require('../../core/processes/processWrapper');
|
const ProcessWrapper = require('../../core/processes/processWrapper');
|
||||||
const constants = require('../../constants');
|
const constants = require('../../constants');
|
||||||
|
const async = require('async');
|
||||||
|
|
||||||
let ipfsProcess; // eslint-disable-line no-unused-vars
|
let ipfsProcess; // eslint-disable-line no-unused-vars
|
||||||
|
|
||||||
const IPFS_DEFAULT_CONFIG_ERROR = "API.HTTPHeaders key has no attributes";
|
const IPFS_DEFAULT_CONFIG_ERROR = ".HTTPHeaders key has no attributes";
|
||||||
|
|
||||||
class IPFSProcess extends ProcessWrapper {
|
class IPFSProcess extends ProcessWrapper {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
|
@ -53,7 +54,7 @@ class IPFSProcess extends ProcessWrapper {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
childProcess.stdout.on('data', (data) => {
|
childProcess.stdout.on('data', async (data) => {
|
||||||
data = data.toString();
|
data = data.toString();
|
||||||
|
|
||||||
// ipfs init just run, and we have a successful result
|
// ipfs init just run, and we have a successful result
|
||||||
|
@ -65,31 +66,31 @@ class IPFSProcess extends ProcessWrapper {
|
||||||
self.readyCalled = true;
|
self.readyCalled = true;
|
||||||
|
|
||||||
// check cors config before updating if needed
|
// check cors config before updating if needed
|
||||||
self.getCorsConfig((err, config) => {
|
async.parallel({
|
||||||
if(err && err.indexOf(IPFS_DEFAULT_CONFIG_ERROR) === -1){
|
api: (next) => {
|
||||||
return console.error('Error getting IPFS CORS config: ', err);
|
self.getApiCorsConfig(next);
|
||||||
|
},
|
||||||
|
gateway: (next) => {
|
||||||
|
self.getGatewayCorsConfig(next);
|
||||||
}
|
}
|
||||||
let needsUpdate = false;
|
}, (err, corsConfig) => {
|
||||||
try {
|
// results is now equals to: {one: 1, two: 2}
|
||||||
let corsConfig = new Set(JSON.parse(config));
|
if(err && err.indexOf(IPFS_DEFAULT_CONFIG_ERROR) === -1){
|
||||||
// test to ensure we have all cors needed
|
return console.error('Error getting IPFS CORS config: ', err);
|
||||||
needsUpdate = !self.cors.every(address => corsConfig.has(address));
|
}
|
||||||
}
|
|
||||||
catch (_e) {
|
if(self.corsConfigNeedsUpdate(corsConfig)){
|
||||||
needsUpdate = true;
|
// update IPFS cors config
|
||||||
}
|
return self.updateCorsConfig(err => {
|
||||||
if(needsUpdate){
|
if(err){
|
||||||
// update IPFS cors config
|
console.error('IPFS CORS update error: ', err);
|
||||||
return self.updateCorsConfig(err => {
|
}
|
||||||
if(err){
|
self.send({result: constants.storage.restart}, () => {
|
||||||
console.error('IPFS CORS update error: ', err);
|
childProcess.kill();
|
||||||
}
|
});
|
||||||
self.send({result: constants.storage.restart}, () => {
|
|
||||||
childProcess.kill();
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
self.send({result: constants.storage.initiated});
|
||||||
self.send({result: constants.storage.initiated});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
console.log('IPFS: ' + data);
|
console.log('IPFS: ' + data);
|
||||||
|
@ -101,7 +102,20 @@ class IPFSProcess extends ProcessWrapper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getCorsConfig(cb){
|
corsConfigNeedsUpdate(config) {
|
||||||
|
let needsUpdate = false;
|
||||||
|
try {
|
||||||
|
// test to ensure we have all cors needed
|
||||||
|
let corsConfig = new Set(JSON.parse(config.api).concat(JSON.parse(config.gateway)));
|
||||||
|
needsUpdate = !this.cors.every(address => corsConfig.has(address));
|
||||||
|
}
|
||||||
|
catch (_e) {
|
||||||
|
needsUpdate = true;
|
||||||
|
}
|
||||||
|
return needsUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
getApiCorsConfig(cb){
|
||||||
let ipfsCorsCmd = `${this.command} config API.HTTPHeaders.Access-Control-Allow-Origin`;
|
let ipfsCorsCmd = `${this.command} config API.HTTPHeaders.Access-Control-Allow-Origin`;
|
||||||
|
|
||||||
child_process.exec(ipfsCorsCmd, {silent: true}, (err, stdout, stderr) => {
|
child_process.exec(ipfsCorsCmd, {silent: true}, (err, stdout, stderr) => {
|
||||||
|
@ -113,26 +127,46 @@ class IPFSProcess extends ProcessWrapper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getGatewayCorsConfig(cb){
|
||||||
|
let ipfsCorsCmd = `${this.command} config Gateway.HTTPHeaders.Access-Control-Allow-Origin`;
|
||||||
|
|
||||||
|
child_process.exec(ipfsCorsCmd, {silent: true}, (err, stdout, stderr) => {
|
||||||
|
if(err || stderr){
|
||||||
|
err = (err || stderr).toString();
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
cb(null, stdout);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
updateCorsConfig(cb){
|
updateCorsConfig(cb){
|
||||||
// update IPFS cors before spawning a daemon (muhaha)
|
// update IPFS cors before spawning a daemon (muhaha)
|
||||||
let ipfsCorsCmd = `${this.command} config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\\"${this.cors.join('\\", \\"')}\\"]"`;
|
let ipfsCorsCmd = `${this.command} config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\\"${this.cors.join('\\", \\"')}\\"]"`;
|
||||||
console.trace(`Updating IPFS CORS using command: ${ipfsCorsCmd}`);
|
console.trace(`Updating IPFS CORS using command: ${ipfsCorsCmd}`);
|
||||||
|
// update the API CORS
|
||||||
child_process.exec(ipfsCorsCmd, {silent: true}, (err, _stdout, stderr) => {
|
child_process.exec(ipfsCorsCmd, {silent: true}, (err, _stdout, stderr) => {
|
||||||
if(err || stderr){
|
if(err || stderr){
|
||||||
err = (err || stderr).toString();
|
err = (err || stderr).toString();
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
child_process.exec(this.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\\"true\\"]"', {silent: true}, (err, _stdout, stderr) => {
|
// update the Gateway CORS as well
|
||||||
|
child_process.exec(ipfsCorsCmd.replace("API", "Gateway"), {silent: true}, (err, _stdout, stderr) => {
|
||||||
if(err || stderr){
|
if(err || stderr){
|
||||||
err = (err || stderr).toString();
|
err = (err || stderr).toString();
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
child_process.exec(this.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\\"PUT\\", \\"POST\\", \\"GET\\"]"', {silent: true}, (err, stdout, stderr) => {
|
child_process.exec(this.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\\"true\\"]"', {silent: true}, (err, _stdout, stderr) => {
|
||||||
if(err || stderr){
|
if(err || stderr){
|
||||||
err = (err || stderr).toString();
|
err = (err || stderr).toString();
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
cb(null, stdout);
|
child_process.exec(this.command + ' config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\\"PUT\\", \\"POST\\", \\"GET\\"]"', {silent: true}, (err, stdout, stderr) => {
|
||||||
|
if(err || stderr){
|
||||||
|
err = (err || stderr).toString();
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
cb(null, stdout);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue