Skip to content

Commit

Permalink
fix(@embark/storage): Fix storage not connecting error
Browse files Browse the repository at this point in the history
In addition to `EmbarkJS.Storage` not being available in the console (ie `await EmbarkJS.Storage.isAvailable() alway returned false), the error `Could not connect to a storage provider using any of the dappConnections in the storage config` would always appear in the console. The storage operations in the dapps were working OK.

The fix to this was three-fold:
1) Wait for the ipfs process to be started before attempting to run the `EmbarkJS.Storage.registerProvider/setProvider` in the console.
2) Wait for `EmbarkJS.Storage.registerProvider` to be called before `EmbarkJS.Storage.setProvider`. This was actually handled in the previous PR.
3) Remove any async operations from the `setProviders` method in the storage module. This was causing `callback is not defined` errors which were being swallowed and masqueraded as an unsuccessful attempt to connect to a `dappConnection` or `upload` config.
  • Loading branch information
emizzle committed Feb 21, 2019
1 parent d378ccf commit 0d72ebe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
24 changes: 16 additions & 8 deletions packages/embark/src/lib/modules/ipfs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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'));
Expand All @@ -119,14 +125,16 @@ class IPFS {
code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);";

this.embark.addCodeToEmbarkJS(code);
this.embark.addConsoleProviderInit("storage", code, (storageConfig) => storageConfig.enabled);
});
});
});
});
}

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);
}

Expand Down
48 changes: 19 additions & 29 deletions packages/embarkjs/src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 0d72ebe

Please sign in to comment.