diff --git a/packages/embark/src/lib/modules/ipfs/index.js b/packages/embark/src/lib/modules/ipfs/index.js index 4a74b22a6..642f89265 100644 --- a/packages/embark/src/lib/modules/ipfs/index.js +++ b/packages/embark/src/lib/modules/ipfs/index.js @@ -22,12 +22,14 @@ class IPFS { if (this.isIpfsStorageEnabledInTheConfig()) { this.setServiceCheck(); - this.addStorageProviderToEmbarkJS(); - this.addObjectToConsole(); this.registerUploadCommand(); this.events.request("processes:register", "ipfs", (cb) => { - self.startProcess(cb); + self.startProcess(() => { + this.addStorageProviderToEmbarkJS(); + this.addObjectToConsole(); + cb(); + }); }); this._checkService((err) => { @@ -83,18 +85,22 @@ class IPFS { }); } - _getNodeUrl() { + _getNodeUrlConfig() { if (this.storageConfig.upload.provider === 'ipfs') { - return utils.buildUrlFromConfig(this.storageConfig.upload) + '/api/v0/version'; + return this.storageConfig.upload; } for (let connection of this.storageConfig.dappConnection) { if (connection.provider === 'ipfs') { - return utils.buildUrlFromConfig(connection) + '/api/v0/version'; + return connection; } } } + _getNodeUrl() { + return utils.buildUrlFromConfig(this._getNodeUrlConfig()) + '/api/v0/version'; + } + _checkService(cb) { let url = this._getNodeUrl(); utils.getJson(url, cb); @@ -106,7 +112,7 @@ class IPFS { this.logger.error(__('Error downloading IPFS API')); return this.logger.error(err.message || err); } - this.events.once('code-generator:ready', () => { + this.events.request('code-generator:ready', () => { this.events.request('code-generator:symlink:generate', location, 'ipfs-api', (err, symlinkDest) => { if (err) { this.logger.error(__('Error creating a symlink to IPFS API')); @@ -119,6 +125,7 @@ class IPFS { code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);"; this.embark.addCodeToEmbarkJS(code); + this.embark.addConsoleProviderInit("storage", code, (storageConfig) => storageConfig.enabled); }); }); }); @@ -126,7 +133,8 @@ class IPFS { } addObjectToConsole() { - let ipfs = IpfsApi(this.host, this.port); + const {host, port} = this._getNodeUrlConfig(); + let ipfs = IpfsApi(host, port); this.events.emit("runcode:register", "ipfs", ipfs); } diff --git a/packages/embarkjs/src/storage.js b/packages/embarkjs/src/storage.js index a3a209298..f2aaeb296 100644 --- a/packages/embarkjs/src/storage.js +++ b/packages/embarkjs/src/storage.js @@ -72,37 +72,27 @@ Storage.isAvailable = function () { }; // TODO: most of this logic should move to the provider implementations themselves -Storage.setProviders = async function (dappConnOptions) { +Storage.setProviders = function (dappConnOptions) { const self = this; - try { - await detectSeries(dappConnOptions, async (dappConn, callback) => { - if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){ - let options = dappConn; - if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true}; - try{ - await self.setProvider('swarm', options); - let isAvailable = await self.isAvailable(); + detectSeries(dappConnOptions, (dappConn, callback) => { + let options = dappConn; + if (dappConn === '$BZZ') options = {"useOnlyGivenProvider": true}; + try { + self.setProvider(dappConn === '$BZZ' ? dappConn : dappConn.provider, options).then(() => { + self.isAvailable().then((isAvailable) => { callback(null, isAvailable); - }catch(err){ - callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used - } - } - else if(dappConn.provider === 'ipfs') { - // set the provider then check the connection, if true, use that provider, else, check next provider - try{ - await self.setProvider('ipfs', dappConn); - let isAvailable = await self.isAvailable(); - callback(null, isAvailable); - } catch(err) { - callback(null, false); // catch but keep looping by not passing err to callback - } - } - }, function(err, result){ - if(!result) console.error('Could not connect to a storage provider using any of the dappConnections in the storage config'); - }); - } catch (err) { - console.error('Failed to connect to a storage provider: ' + err.message); - } + }).catch(() => { + callback(null, false); + }); + }).catch(() => { + callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used + }); + } catch (err) { + callback(null, false); + } + }, function (err, result) { + if (!result) console.error('Could not connect to a storage provider using any of the dappConnections in the storage config'); + }); }; export default Storage;