diff --git a/lib/core/config.js b/lib/core/config.js index e531e106..1f5759cb 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -7,6 +7,7 @@ const deepEqual = require('deep-equal'); const web3 = require('web3'); const constants = require('../constants'); const {canonicalHost, defaultHost} = require('../utils/host'); +const cloneDeep = require('lodash.clonedeep'); const DEFAULT_CONFIG_PATH = 'config/'; const unitRegex = /([0-9]+) ([a-zA-Z]+)/; @@ -29,6 +30,12 @@ var Config = function(options) { this.embarkConfig = {}; this.context = options.context || [constants.contexts.any]; this.shownNoAccountConfigMsg = false; // flag to ensure "no account config" message is only displayed once to the user + this.corsParts = []; + + this.events.setCommandHandler("config:cors:add", (url) => { + this.corsParts.push(url); + this._updateBlockchainCors(); + }); self.events.setCommandHandler("config:contractsConfig", (cb) => { cb(self.contractsConfig); @@ -104,7 +111,7 @@ Config.prototype._updateBlockchainCors = function(){ let blockchainConfig = this.blockchainConfig; let storageConfig = this.storageConfig; let webServerConfig = this.webServerConfig; - let corsParts = []; + let corsParts = cloneDeep(this.corsParts); if(webServerConfig && webServerConfig.host) { corsParts.push(utils.buildUrlFromConfig(webServerConfig)); @@ -233,6 +240,8 @@ Config.prototype.loadBlockchainConfigFile = function() { ); this.shownNoAccountConfigMsg = true; } + + this.events.emit('config:load:blockchain', this.blockchainConfig); }; Config.prototype.loadContractsConfigFile = function() { @@ -288,6 +297,8 @@ Config.prototype.loadContractsConfigFile = function() { if (!deepEqual(newContractsConfig, this.contractsConfig)) { this.contractsConfig = newContractsConfig; } + + this.events.emit('config:load:contracts', this.contractsConfig); }; Config.prototype.loadExternalContractsFiles = function() { @@ -393,6 +404,8 @@ Config.prototype.loadWebServerConfigFile = function() { } else { this.webServerConfig = webServerConfig; } + + this.events.emit('config:load:webserver', this.webServerConfig); }; Config.prototype.loadEmbarkConfigFile = function() { diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index c8f697f3..f4a5fcc1 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -115,7 +115,8 @@ class IPFS { events: self.events, storageConfig: self.storageConfig, webServerConfig: self.webServerConfig, - blockchainConfig: self.blockchainConfig + blockchainConfig: self.blockchainConfig, + corsParts: self.embark.config.corsParts }); self.logger.trace(`Storage module: Launching ipfs process...`); return storageProcessesLauncher.launchProcess('ipfs', callback); diff --git a/lib/modules/storage/storageProcessesLauncher.js b/lib/modules/storage/storageProcessesLauncher.js index b41ea74f..adddf945 100644 --- a/lib/modules/storage/storageProcessesLauncher.js +++ b/lib/modules/storage/storageProcessesLauncher.js @@ -4,6 +4,7 @@ const utils = require('../../utils/utils'); const ProcessLauncher = require('../../core/processes/processLauncher'); const constants = require('../../constants'); const {canonicalHost} = require('../../utils/host'); +const cloneDeep = require('lodash.clonedeep'); let References = { ipfs: 'https://ipfs.io/docs/install/', @@ -18,6 +19,7 @@ class StorageProcessesLauncher { this.webServerConfig = options.webServerConfig; this.blockchainConfig = options.blockchainConfig; this.processes = {}; + this.corsParts = options.corsParts || []; this.cors = this.buildCors(); @@ -30,7 +32,7 @@ class StorageProcessesLauncher { buildCors() { - let corsParts = []; + let corsParts = cloneDeep(this.corsParts); // add our webserver CORS if(this.webServerConfig.enabled){ if (this.webServerConfig && this.webServerConfig.host) { diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index bbd975c0..c5e83b84 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -97,7 +97,8 @@ class Swarm { events: self.events, storageConfig: self.storageConfig, webServerConfig: self.webServerConfig, - blockchainConfig: self.blockchainConfig + blockchainConfig: self.blockchainConfig, + corsParts: self.embark.config.corsParts }); self.logger.trace(`Storage module: Launching swarm process...`); return storageProcessesLauncher.launchProcess('swarm', callback);