Added a global storage module that inits ipfs or swarm depending on dappConnection options. WIP.

This commit is contained in:
emizzle 2018-05-28 22:59:18 +10:00
parent 942a57aa3a
commit 6ac8672a31
7 changed files with 68 additions and 135 deletions

View File

@ -264,18 +264,24 @@ class Engine {
}
storageService(_options) {
this.registerModule('ipfs', {
this.registerModule('storage', {
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
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) {

View File

@ -1,5 +1,4 @@
import IpfsApi from 'ipfs-api';
//import {some} from 'p-iteration';
let __embarkIPFS = {};
@ -32,29 +31,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) {

View File

@ -22,6 +22,12 @@ class IPFS {
this.protocol = options.protocol || this.storageConfig.upload.protocol;
this.addCheck = options.addCheck;
this.embark = embark;
// this.commandlineDeploy();
// this.setServiceCheck();
// this.addProviderToEmbarkJS();
// this.addSetProvider();
// this.addObjectToConsole();
}
commandlineDeploy() {
@ -115,18 +121,18 @@ 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);
// }
addObjectToConsole() {
let ipfs = IpfsApi(this.host, this.port);
let ipfs = IpfsApi(this.storageConfig.host, this.storageConfig.port);
this.events.emit("runcode:register", "ipfs", ipfs);
}

View File

@ -5,25 +5,20 @@ 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 = dappConn;
if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
try{
await EmbarkJS.Storage.setProvider('swarm', options);
let isAvailable = await EmbarkJS.Storage.isAvailable();
return isAvailable;
}catch(err){
return false; // catch errors for when bzz object not initialised but config has requested it to be used
}
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);
let isAvailable = await EmbarkJS.Storage.isAvailable();
return isAvailable;
return EmbarkJS.Storage.isAvailable();
} catch(err) {
return false;
}
@ -31,6 +26,7 @@ __embarkStorage.setProviders = async function (dappConnOptions) {
});
if(!workingConnFound) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config');
} catch (err) {
throw new Error('Failed to connect to a storage provider: ' + err.message);
self.ipfsConnection = null;
throw new Error('Failed to connect to IPFS: ' + err.message);
}
};

View File

@ -5,52 +5,28 @@ let _ = require('underscore');
class Storage {
constructor(embark, options){
this._embark = embark;
this._storageConfig = options.storageConfig;
let storageProviderCls = require(`../${this._storageConfig.upload.provider}/index.js`);
let uploadProvider = new storageProviderCls(embark, options); /*eslint no-new: "off"*/
this._uploadProvider = new storageProviderCls(embark, this._storageConfig); /*eslint no-new: "off"*/
if(typeof uploadProvider.commandlineDeploy == 'function') uploadProvider.commandlineDeploy();
if(typeof uploadProvider.setServiceCheck == 'function') uploadProvider.setServiceCheck();
if(typeof uploadProvider.addObjectToConsole == 'function') uploadProvider.addObjectToConsole();
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 the provider code to embarkjs
// 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 storageProvider;
// check if we've already instantiated our storage class and reuse
if(providerStr === this._storageConfig.upload.provider){
storageProvider = uploadProvider;
}
// otherwise instantiate the storage provider
else {
let storageProviderCls = require(`../${providerStr}/index.js`);
storageProvider = new storageProviderCls(embark, options); /*eslint no-new: "off"*/
}
// add __embarkSwarm and __embarkIPFS objects to EmbarkJS
if(typeof storageProvider.addProviderToEmbarkJS == 'function') storageProvider.addProviderToEmbarkJS();
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 storage provider code (__embarkStorage) to embarkjs
this.addProviderToEmbarkJS();
// add the code to call setProviders in embarkjs after embark is ready
// add the code to call setProviders in embarkjs
this.addSetProviders();
}
addProviderToEmbarkJS(){
// TODO: make this a shouldAdd condition
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
return;
}
let code = "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
this._embark.addCodeToEmbarkJS(code);
}
addSetProviders() {
// TODO: make this a shouldAdd condition
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
@ -63,13 +39,11 @@ class Storage {
return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm);
});
let code = `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)});`;
let shouldInit = (storageConfig) => {
return (connectionsToSet !== undefined && connectionsToSet.length > 0 && storageConfig.enabled === true);
};
let code = "";
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
code += `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)}));`;
this._embark.addProviderInit('storage', code, shouldInit);
this.embark.addCodeToEmbarkJS(code);
}
}

View File

@ -1,13 +1,13 @@
/*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:/`;
this._config = options;
@ -16,7 +16,7 @@ __embarkSwarm.setProvider = function (options) {
return new Promise((resolve, reject) => {
try {
if (!this.bzz.currentProvider) {
if (!this.bzz.currentProvider && !this.useOnlyCurrentProvider) {
this.bzz.setProvider(this.connectUrl);
}
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
@ -30,28 +30,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 web3 swarm object doesn't exist

View File

@ -15,19 +15,16 @@ class Swarm {
this.protocol = options.protocol || options.storageConfig.upload.protocol;
this.addCheck = options.addCheck;
this.embark = embark;
let host = options.host || options.storageConfig.upload.host;
let port = options.port || options.storageConfig.upload.port;
if(port) port = ':' + port;
else port = '';
let protocol = options.protocol || options.storageConfig.upload.protocol || 'http';
this.providerUrl = `${protocol}://${host}${port}`;
this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzzr:/';
this.bzz = new Web3Bzz(this.providerUrl);
this.bzz = options.bzz;
// 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}`);
}
@ -82,7 +79,7 @@ class Swarm {
});
}
addSwarmToEmbarkJS() {
addProviderToEmbarkJS() {
let self = this;
// TODO: make this a shouldAdd condition
if (this.storageConfig === {}) {
@ -109,15 +106,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;