mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-18 08:36:51 +00:00
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:
parent
d378ccf150
commit
0d72ebeda8
@ -22,12 +22,14 @@ class IPFS {
|
|||||||
|
|
||||||
if (this.isIpfsStorageEnabledInTheConfig()) {
|
if (this.isIpfsStorageEnabledInTheConfig()) {
|
||||||
this.setServiceCheck();
|
this.setServiceCheck();
|
||||||
this.addStorageProviderToEmbarkJS();
|
|
||||||
this.addObjectToConsole();
|
|
||||||
this.registerUploadCommand();
|
this.registerUploadCommand();
|
||||||
|
|
||||||
this.events.request("processes:register", "ipfs", (cb) => {
|
this.events.request("processes:register", "ipfs", (cb) => {
|
||||||
self.startProcess(cb);
|
self.startProcess(() => {
|
||||||
|
this.addStorageProviderToEmbarkJS();
|
||||||
|
this.addObjectToConsole();
|
||||||
|
cb();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this._checkService((err) => {
|
this._checkService((err) => {
|
||||||
@ -83,18 +85,22 @@ class IPFS {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_getNodeUrl() {
|
_getNodeUrlConfig() {
|
||||||
if (this.storageConfig.upload.provider === 'ipfs') {
|
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) {
|
for (let connection of this.storageConfig.dappConnection) {
|
||||||
if (connection.provider === 'ipfs') {
|
if (connection.provider === 'ipfs') {
|
||||||
return utils.buildUrlFromConfig(connection) + '/api/v0/version';
|
return connection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getNodeUrl() {
|
||||||
|
return utils.buildUrlFromConfig(this._getNodeUrlConfig()) + '/api/v0/version';
|
||||||
|
}
|
||||||
|
|
||||||
_checkService(cb) {
|
_checkService(cb) {
|
||||||
let url = this._getNodeUrl();
|
let url = this._getNodeUrl();
|
||||||
utils.getJson(url, cb);
|
utils.getJson(url, cb);
|
||||||
@ -106,7 +112,7 @@ class IPFS {
|
|||||||
this.logger.error(__('Error downloading IPFS API'));
|
this.logger.error(__('Error downloading IPFS API'));
|
||||||
return this.logger.error(err.message || err);
|
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) => {
|
this.events.request('code-generator:symlink:generate', location, 'ipfs-api', (err, symlinkDest) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
this.logger.error(__('Error creating a symlink to IPFS API'));
|
this.logger.error(__('Error creating a symlink to IPFS API'));
|
||||||
@ -119,6 +125,7 @@ class IPFS {
|
|||||||
code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);";
|
code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);";
|
||||||
|
|
||||||
this.embark.addCodeToEmbarkJS(code);
|
this.embark.addCodeToEmbarkJS(code);
|
||||||
|
this.embark.addConsoleProviderInit("storage", code, (storageConfig) => storageConfig.enabled);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -126,7 +133,8 @@ class IPFS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addObjectToConsole() {
|
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);
|
this.events.emit("runcode:register", "ipfs", ipfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,37 +72,27 @@ Storage.isAvailable = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// TODO: most of this logic should move to the provider implementations themselves
|
// TODO: most of this logic should move to the provider implementations themselves
|
||||||
Storage.setProviders = async function (dappConnOptions) {
|
Storage.setProviders = function (dappConnOptions) {
|
||||||
const self = this;
|
const self = this;
|
||||||
try {
|
detectSeries(dappConnOptions, (dappConn, callback) => {
|
||||||
await detectSeries(dappConnOptions, async (dappConn, callback) => {
|
let options = dappConn;
|
||||||
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
|
if (dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
|
||||||
let options = dappConn;
|
try {
|
||||||
if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
|
self.setProvider(dappConn === '$BZZ' ? dappConn : dappConn.provider, options).then(() => {
|
||||||
try{
|
self.isAvailable().then((isAvailable) => {
|
||||||
await self.setProvider('swarm', options);
|
|
||||||
let isAvailable = await self.isAvailable();
|
|
||||||
callback(null, isAvailable);
|
callback(null, isAvailable);
|
||||||
}catch(err){
|
}).catch(() => {
|
||||||
callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used
|
callback(null, false);
|
||||||
}
|
});
|
||||||
}
|
}).catch(() => {
|
||||||
else if(dappConn.provider === 'ipfs') {
|
callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used
|
||||||
// set the provider then check the connection, if true, use that provider, else, check next provider
|
});
|
||||||
try{
|
} catch (err) {
|
||||||
await self.setProvider('ipfs', dappConn);
|
callback(null, false);
|
||||||
let isAvailable = await self.isAvailable();
|
}
|
||||||
callback(null, isAvailable);
|
}, function (err, result) {
|
||||||
} catch(err) {
|
if (!result) console.error('Could not connect to a storage provider using any of the dappConnections in the storage config');
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Storage;
|
export default Storage;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user