fix(@embark/storage): Fix storage not connecting error

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.
This commit is contained in:
emizzle 2019-02-21 19:24:15 +11:00 committed by Eric Mastro
parent d378ccf150
commit 0d72ebeda8
2 changed files with 35 additions and 37 deletions

View File

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

View File

@ -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;