mirror of https://github.com/embarklabs/embark.git
Added a global storage module that inits ipfs or swarm depending on dappConnection options. WIP.
This commit is contained in:
parent
a26fbfff36
commit
f9bfe20de1
|
@ -263,19 +263,25 @@ class Engine {
|
||||||
}
|
}
|
||||||
|
|
||||||
storageService(_options) {
|
storageService(_options) {
|
||||||
this.registerModule('ipfs', {
|
this.registerModule('storage', {
|
||||||
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
||||||
storageConfig: this.config.storageConfig,
|
storageConfig: this.config.storageConfig,
|
||||||
host: _options.host,
|
host: _options.host,
|
||||||
port: _options.port
|
port: _options.port
|
||||||
});
|
});
|
||||||
this.registerModule('swarm', {
|
// this.registerModule('ipfs', {
|
||||||
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
// addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
||||||
storageConfig: this.config.storageConfig,
|
// storageConfig: this.config.storageConfig,
|
||||||
// TODO: this should not be needed and should be deducted from the config instead
|
// host: _options.host,
|
||||||
// the eth provider is not necessary the same as the swarm one
|
// port: _options.port
|
||||||
bzz: this.blockchain.web3.bzz
|
// });
|
||||||
});
|
// 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) {
|
web3Service(options) {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import IpfsApi from 'ipfs-api';
|
import IpfsApi from 'ipfs-api';
|
||||||
//import {some} from 'p-iteration';
|
|
||||||
|
|
||||||
let __embarkIPFS = {};
|
let __embarkIPFS = {};
|
||||||
|
|
||||||
|
@ -31,29 +30,6 @@ __embarkIPFS.setProvider = function (options) {
|
||||||
return promise;
|
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) {
|
__embarkIPFS.saveText = function (text) {
|
||||||
const self = this;
|
const self = this;
|
||||||
var promise = new Promise(function (resolve, reject) {
|
var promise = new Promise(function (resolve, reject) {
|
||||||
|
|
|
@ -18,11 +18,11 @@ class IPFS {
|
||||||
this.addCheck = options.addCheck;
|
this.addCheck = options.addCheck;
|
||||||
this.embark = embark;
|
this.embark = embark;
|
||||||
|
|
||||||
this.commandlineDeploy();
|
// this.commandlineDeploy();
|
||||||
this.setServiceCheck();
|
// this.setServiceCheck();
|
||||||
this.addIPFSToEmbarkJS();
|
// this.addProviderToEmbarkJS();
|
||||||
this.addSetProvider();
|
// this.addSetProvider();
|
||||||
this.addIpfsObjectToConsole();
|
// this.addObjectToConsole();
|
||||||
}
|
}
|
||||||
|
|
||||||
commandlineDeploy() {
|
commandlineDeploy() {
|
||||||
|
@ -80,7 +80,7 @@ class IPFS {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addIPFSToEmbarkJS() {
|
addProviderToEmbarkJS() {
|
||||||
const self = this;
|
const self = this;
|
||||||
// TODO: make this a shouldAdd condition
|
// TODO: make this a shouldAdd condition
|
||||||
if (this.storageConfig === {}) {
|
if (this.storageConfig === {}) {
|
||||||
|
@ -116,17 +116,17 @@ class IPFS {
|
||||||
this.embark.addCodeToEmbarkJS(code);
|
this.embark.addCodeToEmbarkJS(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
addSetProvider() {
|
// addSetProvider() {
|
||||||
let code = "\nEmbarkJS.Storage.setProviders('ipfs'," + JSON.stringify(this.storageConfig.dappConnection) + ");";
|
// let code = "\nEmbarkJS.Storage.setProviders('ipfs'," + JSON.stringify(this.storageConfig.dappConnection) + ");";
|
||||||
|
|
||||||
let shouldInit = (storageConfig) => {
|
// let shouldInit = (storageConfig) => {
|
||||||
return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'ipfs') && storageConfig.enabled === true);
|
// 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);
|
let ipfs = IpfsApi(this.storageConfig.host, this.storageConfig.port);
|
||||||
RunCode.doEval("", {ipfs: ipfs});
|
RunCode.doEval("", {ipfs: ipfs});
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
};
|
|
@ -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;
|
|
@ -1,18 +1,18 @@
|
||||||
/*global web3 */
|
/*global web3, some */
|
||||||
let __embarkSwarm = {};
|
let __embarkSwarm = {};
|
||||||
const bytes = require("eth-lib/lib/bytes");
|
const bytes = require("eth-lib/lib/bytes");
|
||||||
import {some} from 'p-iteration';
|
|
||||||
|
|
||||||
__embarkSwarm.setProvider = function (options) {
|
__embarkSwarm.setProvider = function (options) {
|
||||||
this.bzz = web3.bzz;
|
this.bzz = web3.bzz;
|
||||||
this.protocol = options.protocol || 'http';
|
this.protocol = options.protocol || 'http';
|
||||||
this.connectUrl = `${this.protocol}://${options.host}:${options.port}`;
|
this.connectUrl = `${this.protocol}://${options.host}:${options.port}`;
|
||||||
this.connectError = new Error(`Cannot connect to Swarm node on ${this.connectUrl}`);
|
this.connectError = new Error(`Cannot connect to Swarm node on ${this.connectUrl}`);
|
||||||
|
this.useOnlyCurrentProvider = options.useOnlyCurrentProvider;
|
||||||
//this._getUrl = options.getUrl || `${this.connectUrl}/bzzr:/`;
|
//this._getUrl = options.getUrl || `${this.connectUrl}/bzzr:/`;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
if (!this.bzz.currentProvider) {
|
if (!this.bzz.currentProvider && !this.useOnlyCurrentProvider) {
|
||||||
this.bzz.setProvider(this.connectUrl);
|
this.bzz.setProvider(this.connectUrl);
|
||||||
}
|
}
|
||||||
resolve(this);
|
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 () {
|
__embarkSwarm.isAvailable = function () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.bzz) {
|
if (!this.bzz) {
|
||||||
|
|
|
@ -16,14 +16,14 @@ class Swarm {
|
||||||
this.embark = embark;
|
this.embark = embark;
|
||||||
this.bzz = options.bzz;
|
this.bzz = options.bzz;
|
||||||
|
|
||||||
this.initSwarmProvider();
|
// this.initProvider();
|
||||||
this.commandlineDeploy();
|
// this.commandlineDeploy();
|
||||||
this.setServiceCheck();
|
// this.setServiceCheck();
|
||||||
this.addSwarmToEmbarkJS();
|
// this.addProviderToEmbarkJS();
|
||||||
this.addSetProvider();
|
// this.addSetProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
initSwarmProvider(){
|
initProvider(){
|
||||||
if(!this.bzz.currentProvider) {
|
if(!this.bzz.currentProvider) {
|
||||||
this.bzz.setProvider(`${this.protocol}://${this.host}:${this.port}`);
|
this.bzz.setProvider(`${this.protocol}://${this.host}:${this.port}`);
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ class Swarm {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addSwarmToEmbarkJS() {
|
addProviderToEmbarkJS() {
|
||||||
let self = this;
|
let self = this;
|
||||||
// TODO: make this a shouldAdd condition
|
// TODO: make this a shouldAdd condition
|
||||||
if (this.storageConfig === {}) {
|
if (this.storageConfig === {}) {
|
||||||
|
@ -103,15 +103,15 @@ class Swarm {
|
||||||
this.embark.addCodeToEmbarkJS(code);
|
this.embark.addCodeToEmbarkJS(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
addSetProvider() {
|
// addSetProvider() {
|
||||||
let code = "\nEmbarkJS.Storage.setProviders('swarm'," + JSON.stringify(this.storageConfig.dappConnection) + ");";
|
// let code = "\nEmbarkJS.Storage.setProviders('swarm'," + JSON.stringify(this.storageConfig.dappConnection) + ");";
|
||||||
|
|
||||||
let shouldInit = (storageConfig) => {
|
// let shouldInit = (storageConfig) => {
|
||||||
return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'swarm') && storageConfig.enabled === true);
|
// 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;
|
module.exports = Swarm;
|
||||||
|
|
Loading…
Reference in New Issue