storage.js
1 import {detectSeries} from './async'; 2 3 const Storage = {}; 4 5 Storage.Providers = {}; 6 Storage.noProviderError = 'Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")'; 7 8 Storage.saveText = function (text) { 9 if (!this.currentStorage) { 10 throw new Error(this.noProviderError); 11 } 12 return this.currentStorage.saveText(text); 13 }; 14 15 Storage.get = function (hash) { 16 if (!this.currentStorage) { 17 throw new Error(this.noProviderError); 18 } 19 return this.currentStorage.get(hash); 20 }; 21 22 Storage.uploadFile = function (inputSelector) { 23 if (!this.currentStorage) { 24 throw new Error(this.noProviderError); 25 } 26 return this.currentStorage.uploadFile(inputSelector); 27 }; 28 29 Storage.getUrl = function (hash) { 30 if (!this.currentStorage) { 31 throw new Error(this.noProviderError); 32 } 33 return this.currentStorage.getUrl(hash); 34 }; 35 36 Storage.resolve = function (name, callback) { 37 if (!this.currentStorage) { 38 throw new Error(this.noProviderError); 39 } 40 return this.currentStorage.resolve(name, callback); 41 }; 42 43 Storage.register = function (addr, callback) { 44 if (!this.currentStorage) { 45 throw new Error(this.noProviderError); 46 } 47 return this.currentStorage.register(addr, callback); 48 }; 49 50 Storage.registerProvider = function (providerName, obj) { 51 this.Providers[providerName] = obj; 52 }; 53 54 Storage.setProvider = function (providerName, options) { 55 let provider = this.Providers[providerName]; 56 57 if (!provider) { 58 throw new Error('Unknown storage provider'); 59 } 60 61 this.currentProviderName = providerName; 62 this.currentStorage = provider; 63 64 return provider.setProvider(options); 65 }; 66 67 Storage.isAvailable = function () { 68 if (!this.currentStorage) { 69 return false; 70 } 71 return this.currentStorage.isAvailable(); 72 }; 73 74 // TODO: most of this logic should move to the provider implementations themselves 75 Storage.setProviders = async function (dappConnOptions) { 76 const self = this; 77 try { 78 await detectSeries(dappConnOptions, async (dappConn, callback) => { 79 if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){ 80 let options = dappConn; 81 if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true}; 82 try{ 83 await self.setProvider('swarm', options); 84 let isAvailable = await self.isAvailable(); 85 callback(null, isAvailable); 86 }catch(err){ 87 callback(null, false); // catch errors for when bzz object not initialised but config has requested it to be used 88 } 89 } 90 else if(dappConn.provider === 'ipfs') { 91 // set the provider then check the connection, if true, use that provider, else, check next provider 92 try{ 93 await self.setProvider('ipfs', dappConn); 94 let isAvailable = await self.isAvailable(); 95 callback(null, isAvailable); 96 } catch(err) { 97 callback(null, false); // catch but keep looping by not passing err to callback 98 } 99 } 100 }, function(err, result){ 101 if(!result) console.error('Could not connect to a storage provider using any of the dappConnections in the storage config'); 102 }); 103 } catch (err) { 104 console.error('Failed to connect to a storage provider: ' + err.message); 105 } 106 }; 107 108 export default Storage;