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 ProcessWrapper = require('../../core/processes/processWrapper');
|
||||
const constants = require('../../constants');
|
||||
const async = require('async');
|
||||
|
||||
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 {
|
||||
constructor(options) {
|
||||
|
@ -53,7 +54,7 @@ class IPFSProcess extends ProcessWrapper {
|
|||
}
|
||||
});
|
||||
|
||||
childProcess.stdout.on('data', (data) => {
|
||||
childProcess.stdout.on('data', async (data) => {
|
||||
data = data.toString();
|
||||
|
||||
// ipfs init just run, and we have a successful result
|
||||
|
@ -65,20 +66,20 @@ class IPFSProcess extends ProcessWrapper {
|
|||
self.readyCalled = true;
|
||||
|
||||
// check cors config before updating if needed
|
||||
self.getCorsConfig((err, config) => {
|
||||
async.parallel({
|
||||
api: (next) => {
|
||||
self.getApiCorsConfig(next);
|
||||
},
|
||||
gateway: (next) => {
|
||||
self.getGatewayCorsConfig(next);
|
||||
}
|
||||
}, (err, corsConfig) => {
|
||||
// results is now equals to: {one: 1, two: 2}
|
||||
if(err && err.indexOf(IPFS_DEFAULT_CONFIG_ERROR) === -1){
|
||||
return console.error('Error getting IPFS CORS config: ', err);
|
||||
}
|
||||
let needsUpdate = false;
|
||||
try {
|
||||
let corsConfig = new Set(JSON.parse(config));
|
||||
// test to ensure we have all cors needed
|
||||
needsUpdate = !self.cors.every(address => corsConfig.has(address));
|
||||
}
|
||||
catch (_e) {
|
||||
needsUpdate = true;
|
||||
}
|
||||
if(needsUpdate){
|
||||
|
||||
if(self.corsConfigNeedsUpdate(corsConfig)){
|
||||
// update IPFS cors config
|
||||
return self.updateCorsConfig(err => {
|
||||
if(err){
|
||||
|
@ -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`;
|
||||
|
||||
child_process.exec(ipfsCorsCmd, {silent: true}, (err, stdout, stderr) => {
|
||||
|
@ -113,11 +127,30 @@ 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){
|
||||
// update IPFS cors before spawning a daemon (muhaha)
|
||||
let ipfsCorsCmd = `${this.command} config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\\"${this.cors.join('\\", \\"')}\\"]"`;
|
||||
console.trace(`Updating IPFS CORS using command: ${ipfsCorsCmd}`);
|
||||
// update the API CORS
|
||||
child_process.exec(ipfsCorsCmd, {silent: true}, (err, _stdout, stderr) => {
|
||||
if(err || stderr){
|
||||
err = (err || stderr).toString();
|
||||
return cb(err);
|
||||
}
|
||||
// update the Gateway CORS as well
|
||||
child_process.exec(ipfsCorsCmd.replace("API", "Gateway"), {silent: true}, (err, _stdout, stderr) => {
|
||||
if(err || stderr){
|
||||
err = (err || stderr).toString();
|
||||
return cb(err);
|
||||
|
@ -136,6 +169,7 @@ class IPFSProcess extends ProcessWrapper {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
startIPFSDaemon() {
|
||||
|
|
Loading…
Reference in New Issue