2018-05-28 12:59:18 +00:00
|
|
|
/* global EmbarkJS */
|
|
|
|
|
2018-06-27 00:45:46 +00:00
|
|
|
import {detectSeries} from 'async';
|
2018-05-28 12:59:18 +00:00
|
|
|
|
|
|
|
let __embarkStorage = {};
|
|
|
|
|
|
|
|
__embarkStorage.setProviders = async function (dappConnOptions) {
|
|
|
|
try {
|
2018-06-27 00:45:46 +00:00
|
|
|
await detectSeries(dappConnOptions, async (dappConn, callback) => {
|
2018-05-28 12:59:18 +00:00
|
|
|
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
|
2018-05-30 06:34:36 +00:00
|
|
|
let options = dappConn;
|
|
|
|
if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
|
|
|
|
try{
|
|
|
|
await EmbarkJS.Storage.setProvider('swarm', options);
|
|
|
|
let isAvailable = await EmbarkJS.Storage.isAvailable();
|
2018-06-27 00:45:46 +00:00
|
|
|
callback(null, isAvailable);
|
2018-05-30 06:34:36 +00:00
|
|
|
}catch(err){
|
2018-06-27 00:45:46 +00:00
|
|
|
callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used
|
2018-05-30 06:34:36 +00:00
|
|
|
}
|
2018-05-28 12:59:18 +00:00
|
|
|
}
|
|
|
|
else if(dappConn.provider === 'ipfs') {
|
|
|
|
// set the provider then check the connection, if true, use that provider, else, check next provider
|
|
|
|
try{
|
|
|
|
await EmbarkJS.Storage.setProvider('ipfs', dappConn);
|
2018-05-30 06:34:36 +00:00
|
|
|
let isAvailable = await EmbarkJS.Storage.isAvailable();
|
2018-06-27 00:45:46 +00:00
|
|
|
callback(null, isAvailable);
|
2018-05-28 12:59:18 +00:00
|
|
|
} catch(err) {
|
2018-06-27 00:45:46 +00:00
|
|
|
callback(null, false); // catch but keep looping by not passing err to callback
|
2018-05-28 12:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 00:45:46 +00:00
|
|
|
}, function(err, result){
|
|
|
|
if(!result) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config');
|
2018-05-28 12:59:18 +00:00
|
|
|
});
|
|
|
|
} catch (err) {
|
2018-05-30 06:34:36 +00:00
|
|
|
throw new Error('Failed to connect to a storage provider: ' + err.message);
|
2018-05-28 12:59:18 +00:00
|
|
|
}
|
|
|
|
};
|