From f9bfe20de1c876412d5fab34ab3eed7c8e43cbbc Mon Sep 17 00:00:00 2001 From: emizzle Date: Mon, 28 May 2018 22:59:18 +1000 Subject: [PATCH] Added a global storage module that inits ipfs or swarm depending on dappConnection options. WIP. --- lib/core/engine.js | 22 +++++++++------ lib/modules/ipfs/embarkjs.js | 24 ---------------- lib/modules/ipfs/index.js | 28 +++++++++--------- lib/modules/storage/embarkjs.js | 32 +++++++++++++++++++++ lib/modules/storage/index.js | 50 +++++++++++++++++++++++++++++++++ lib/modules/swarm/embarkjs.js | 28 ++---------------- lib/modules/swarm/index.js | 28 +++++++++--------- 7 files changed, 127 insertions(+), 85 deletions(-) create mode 100644 lib/modules/storage/embarkjs.js create mode 100644 lib/modules/storage/index.js diff --git a/lib/core/engine.js b/lib/core/engine.js index 7cf14908..d45aa5bf 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -263,19 +263,25 @@ class Engine { } storageService(_options) { - this.registerModule('ipfs', { + this.registerModule('storage', { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), storageConfig: this.config.storageConfig, host: _options.host, port: _options.port }); - this.registerModule('swarm', { - addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), - storageConfig: this.config.storageConfig, - // TODO: this should not be needed and should be deducted from the config instead - // the eth provider is not necessary the same as the swarm one - bzz: this.blockchain.web3.bzz - }); + // this.registerModule('ipfs', { + // addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + // storageConfig: this.config.storageConfig, + // host: _options.host, + // port: _options.port + // }); + // this.registerModule('swarm', { + // addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + // storageConfig: this.config.storageConfig, + // // TODO: this should not be needed and should be deducted from the config instead + // // the eth provider is not necessary the same as the swarm one + // bzz: this.blockchain.web3.bzz + // }); } web3Service(options) { diff --git a/lib/modules/ipfs/embarkjs.js b/lib/modules/ipfs/embarkjs.js index b90ca176..39c849ed 100644 --- a/lib/modules/ipfs/embarkjs.js +++ b/lib/modules/ipfs/embarkjs.js @@ -1,5 +1,4 @@ import IpfsApi from 'ipfs-api'; -//import {some} from 'p-iteration'; let __embarkIPFS = {}; @@ -31,29 +30,6 @@ __embarkIPFS.setProvider = function (options) { return promise; }; -__embarkIPFS.setProviders = async function (dappConnOptions) { - var self = this; - try { - let workingConnFound = await some(dappConnOptions, async (dappConn) => { - if(dappConn === '$BZZ' || dappConn.provider !== 'ipfs') return false; // swarm has no bearing for ipfs plugin, continue - else { - // set the provider then check the connection, if true, use that provider, else, check next provider - try{ - await self.setProvider(dappConn); - return await self.isAvailable(); - } catch(err) { - return false; - } - } - }); - if(!workingConnFound) throw new Error('Could not connect to IPFS using any of the dappConnections in the storage config'); - else return self; - } catch (err) { - self.ipfsConnection = null; - throw new Error('Failed to connect to IPFS: ' + err.message); - } -}; - __embarkIPFS.saveText = function (text) { const self = this; var promise = new Promise(function (resolve, reject) { diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 234fc33c..19017ec8 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -18,11 +18,11 @@ class IPFS { this.addCheck = options.addCheck; this.embark = embark; - this.commandlineDeploy(); - this.setServiceCheck(); - this.addIPFSToEmbarkJS(); - this.addSetProvider(); - this.addIpfsObjectToConsole(); + // this.commandlineDeploy(); + // this.setServiceCheck(); + // this.addProviderToEmbarkJS(); + // this.addSetProvider(); + // this.addObjectToConsole(); } commandlineDeploy() { @@ -80,7 +80,7 @@ class IPFS { }); } - addIPFSToEmbarkJS() { + addProviderToEmbarkJS() { const self = this; // TODO: make this a shouldAdd condition if (this.storageConfig === {}) { @@ -116,17 +116,17 @@ class IPFS { this.embark.addCodeToEmbarkJS(code); } - addSetProvider() { - let code = "\nEmbarkJS.Storage.setProviders('ipfs'," + JSON.stringify(this.storageConfig.dappConnection) + ");"; + // addSetProvider() { + // let code = "\nEmbarkJS.Storage.setProviders('ipfs'," + JSON.stringify(this.storageConfig.dappConnection) + ");"; - let shouldInit = (storageConfig) => { - return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'ipfs') && storageConfig.enabled === true); - }; + // let shouldInit = (storageConfig) => { + // return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'ipfs') && storageConfig.enabled === true); + // }; - this.embark.addProviderInit('storage', code, shouldInit); - } + // this.embark.addProviderInit('storage', code, shouldInit); + // } - addIpfsObjectToConsole() { + addObjectToConsole() { let ipfs = IpfsApi(this.storageConfig.host, this.storageConfig.port); RunCode.doEval("", {ipfs: ipfs}); } diff --git a/lib/modules/storage/embarkjs.js b/lib/modules/storage/embarkjs.js new file mode 100644 index 00000000..dac27481 --- /dev/null +++ b/lib/modules/storage/embarkjs.js @@ -0,0 +1,32 @@ +/* global EmbarkJS */ + +import {findSeries} from 'p-iteration'; + +let __embarkStorage = {}; + +__embarkStorage.setProviders = async function (dappConnOptions) { + var self = this; + try { + let workingConnFound = await findSeries(dappConnOptions, async (dappConn) => { + if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){ + let options = dappConnOptions; + options.useOnlyCurrentProvider = dappConn === '$BZZ'; + await EmbarkJS.Storage.setProvider('swarm', options); + return EmbarkJS.Storage.isAvailable(); + } + 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); + return EmbarkJS.Storage.isAvailable(); + } catch(err) { + return false; + } + } + }); + if(!workingConnFound) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config'); + } catch (err) { + self.ipfsConnection = null; + throw new Error('Failed to connect to IPFS: ' + err.message); + } + }; diff --git a/lib/modules/storage/index.js b/lib/modules/storage/index.js new file mode 100644 index 00000000..af67d58c --- /dev/null +++ b/lib/modules/storage/index.js @@ -0,0 +1,50 @@ + +let utils = require('../../utils/utils.js'); +let fs = require('../../core/fs.js'); +let _ = require('underscore'); + +class Storage { + constructor(embark, options){ + this._storageConfig = options.storageConfig; + + let storageProviderCls = require(`../${this._storageConfig.upload.provider}/index.js`); + this._uploadProvider = new storageProviderCls(embark, this._storageConfig); /*eslint no-new: "off"*/ + + if(typeof this._uploadProvider.initProvider == 'function') this._uploadProvider.initProvider(); + if(typeof this._uploadProvider.commandlineDeploy == 'function') this._uploadProvider.commandlineDeploy(); + if(typeof this._uploadProvider.setServiceCheck == 'function') this._uploadProvider.setServiceCheck(); + if(typeof this._uploadProvider.addObjectToConsole == 'function') this._uploadProvider.addObjectToConsole(); + + // loop through all available providers and add "register provider" code in EmbarkJS + // which allows the provider to be set in the DApp + this._storageConfig.available_providers.forEach(providerStr => { + let storageProviderCls = require(`../${providerStr}/index.js`); + this._storageProvider = new storageProviderCls(this.storageConfig); /*eslint no-new: "off"*/ + if(typeof this._storageProvider.addProviderToEmbarkJS == 'function') this._storageProvider.addProviderToEmbarkJS(); + }); + + // add the code to call setProviders in embarkjs + this.addSetProviders(); + } + + addSetProviders() { + // TODO: make this a shouldAdd condition + if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) { + return; + } + + // filter list of dapp connections based on available_providers set in config + let hasSwarm = _.contains(this._storageConfig.available_providers, 'swarm'); // don't need to eval this in every loop iteration + let connectionsToSet = _.filter(this._storageConfig.dappConnection, (conn) => { + return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm); + }); + + let code = ""; + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); + code += `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)}));`; + + this.embark.addCodeToEmbarkJS(code); + } +} + +module.exports = Storage; diff --git a/lib/modules/swarm/embarkjs.js b/lib/modules/swarm/embarkjs.js index 78d163ec..18ed0b08 100644 --- a/lib/modules/swarm/embarkjs.js +++ b/lib/modules/swarm/embarkjs.js @@ -1,18 +1,18 @@ -/*global web3 */ +/*global web3, some */ let __embarkSwarm = {}; const bytes = require("eth-lib/lib/bytes"); -import {some} from 'p-iteration'; __embarkSwarm.setProvider = function (options) { this.bzz = web3.bzz; this.protocol = options.protocol || 'http'; this.connectUrl = `${this.protocol}://${options.host}:${options.port}`; this.connectError = new Error(`Cannot connect to Swarm node on ${this.connectUrl}`); + this.useOnlyCurrentProvider = options.useOnlyCurrentProvider; //this._getUrl = options.getUrl || `${this.connectUrl}/bzzr:/`; return new Promise((resolve, reject) => { try { - if (!this.bzz.currentProvider) { + if (!this.bzz.currentProvider && !this.useOnlyCurrentProvider) { this.bzz.setProvider(this.connectUrl); } resolve(this); @@ -23,28 +23,6 @@ __embarkSwarm.setProvider = function (options) { }); }; -__embarkSwarm.setProviders = async function (dappConnOptions) { - var self = this; - try { - let workingConnFound = await some(dappConnOptions, async (dappConn) => { - if(dappConn === '$BZZ'){ - return self.isAvailable(); - } - else if(dappConn.provider === 'swarm') - { - // set the provider then check the connection, if true, use that provider, else, check next provider - await self.setProvider(dappConn); - return self.isAvailable(); - } - else return false; - }); - if(!workingConnFound) throw new Error('Could not connect to Swarm using any of the dappConnections in the storage config'); - else return self; - } catch (err) { - throw new Error('Failed to connect to Swarm: ' + err.message); - } -}; - __embarkSwarm.isAvailable = function () { return new Promise((resolve, reject) => { if (!this.bzz) { diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index f336c0f7..b14b2939 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -16,14 +16,14 @@ class Swarm { this.embark = embark; this.bzz = options.bzz; - this.initSwarmProvider(); - this.commandlineDeploy(); - this.setServiceCheck(); - this.addSwarmToEmbarkJS(); - this.addSetProvider(); + // this.initProvider(); + // this.commandlineDeploy(); + // this.setServiceCheck(); + // this.addProviderToEmbarkJS(); + // this.addSetProvider(); } - initSwarmProvider(){ + initProvider(){ if(!this.bzz.currentProvider) { this.bzz.setProvider(`${this.protocol}://${this.host}:${this.port}`); } @@ -76,7 +76,7 @@ class Swarm { }); } - addSwarmToEmbarkJS() { + addProviderToEmbarkJS() { let self = this; // TODO: make this a shouldAdd condition if (this.storageConfig === {}) { @@ -103,15 +103,15 @@ class Swarm { this.embark.addCodeToEmbarkJS(code); } - addSetProvider() { - let code = "\nEmbarkJS.Storage.setProviders('swarm'," + JSON.stringify(this.storageConfig.dappConnection) + ");"; + // addSetProvider() { + // let code = "\nEmbarkJS.Storage.setProviders('swarm'," + JSON.stringify(this.storageConfig.dappConnection) + ");"; - let shouldInit = (storageConfig) => { - return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'swarm') && storageConfig.enabled === true); - }; + // let shouldInit = (storageConfig) => { + // return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'swarm') && storageConfig.enabled === true); + // }; - this.embark.addProviderInit('storage', code, shouldInit); - } + // this.embark.addProviderInit('storage', code, shouldInit); + // } } module.exports = Swarm;