mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-16 23:57:11 +00:00
Dynamic selection of storage provider now working based on improved storage config.
swarm plugin now re-initialises the bzz object when it's availability is checked. this creates a much more stable swarm implementation on the dapp side. surrounded the storage provider init code block with embark env ready added alternate swarm gateway url can now upload dapp to ipfs and run swarm storage and vice versa
This commit is contained in:
parent
6ac8672a31
commit
85117cf55c
12
js/embark.js
12
js/embark.js
@ -230,18 +230,6 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
|
|||||||
return providerObj.setProvider(options);
|
return providerObj.setProvider(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.setProviders = function(provider, dappConnOptions) {
|
|
||||||
let providerObj = this.Providers[provider];
|
|
||||||
|
|
||||||
if (!providerObj) {
|
|
||||||
throw new Error('Unknown storage provider');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.currentStorage = providerObj;
|
|
||||||
|
|
||||||
return providerObj.setProviders(dappConnOptions);
|
|
||||||
};
|
|
||||||
|
|
||||||
EmbarkJS.Storage.isAvailable = function(){
|
EmbarkJS.Storage.isAvailable = function(){
|
||||||
if (!this.currentStorage) {
|
if (!this.currentStorage) {
|
||||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||||
|
@ -135,7 +135,6 @@ class Engine {
|
|||||||
plugins: this.plugins
|
plugins: this.plugins
|
||||||
});
|
});
|
||||||
this.events.on('code-generator-ready', function () {
|
this.events.on('code-generator-ready', function () {
|
||||||
console.log('CODE GENERATOR READY EVENT FIRED');
|
|
||||||
self.events.request('code', function (abi, contractsJSON) {
|
self.events.request('code', function (abi, contractsJSON) {
|
||||||
pipeline.build(abi, contractsJSON, null, () => {
|
pipeline.build(abi, contractsJSON, null, () => {
|
||||||
if (self.watch) {
|
if (self.watch) {
|
||||||
|
@ -132,6 +132,11 @@
|
|||||||
"IPFS node detected": "IPFS node detected",
|
"IPFS node detected": "IPFS node detected",
|
||||||
"Webserver is offline": "Webserver is offline",
|
"Webserver is offline": "Webserver is offline",
|
||||||
"DApp path length is too long: \"": "DApp path length is too long: \"",
|
"DApp path length is too long: \"": "DApp path length is too long: \"",
|
||||||
|
"This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less.": "This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less.",
|
||||||
|
"deploying to swarm!": "deploying to swarm!",
|
||||||
|
"adding %s to swarm": "adding %s to swarm",
|
||||||
|
"successfully uploaded to swarm": "successfully uploaded to swarm",
|
||||||
|
"Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}{{port}}.": "Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}{{port}}."
|
||||||
"This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less.": "This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less."
|
"This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less.": "This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less."
|
||||||
"no contracts found": "no contracts found",
|
"no contracts found": "no contracts found",
|
||||||
"DApp path length is too long: \"": "DApp path length is too long: \"",
|
"DApp path length is too long: \"": "DApp path length is too long: \"",
|
||||||
|
@ -24,7 +24,7 @@ __embarkIPFS.setProvider = function (options) {
|
|||||||
resolve(self);
|
resolve(self);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
self.ipfsConnection = null;
|
self._ipfsConnection = null;
|
||||||
reject(new Error('Failed to connect to IPFS'));
|
reject(new Error('Failed to connect to IPFS'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
let UploadIPFS = require('./upload.js');
|
const UploadIPFS = require('./upload.js');
|
||||||
let utils = require('../../utils/utils.js');
|
const utils = require('../../utils/utils.js');
|
||||||
let fs = require('../../core/fs.js');
|
const fs = require('../../core/fs.js');
|
||||||
let RunCode = require('../../coderunner/runCode');
|
const RunCode = require('../../coderunner/runCode');
|
||||||
let IpfsApi = require('ipfs-api');
|
const IpfsApi = require('ipfs-api');
|
||||||
const _ = require('underscore');
|
const _ = require('underscore');
|
||||||
const UploadIPFS = require('./upload.js');
|
const UploadIPFS = require('./upload.js');
|
||||||
const utils = require('../../utils/utils.js');
|
const utils = require('../../utils/utils.js');
|
||||||
@ -22,12 +22,6 @@ class IPFS {
|
|||||||
this.protocol = options.protocol || this.storageConfig.upload.protocol;
|
this.protocol = options.protocol || this.storageConfig.upload.protocol;
|
||||||
this.addCheck = options.addCheck;
|
this.addCheck = options.addCheck;
|
||||||
this.embark = embark;
|
this.embark = embark;
|
||||||
|
|
||||||
// this.commandlineDeploy();
|
|
||||||
// this.setServiceCheck();
|
|
||||||
// this.addProviderToEmbarkJS();
|
|
||||||
// this.addSetProvider();
|
|
||||||
// this.addObjectToConsole();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commandlineDeploy() {
|
commandlineDeploy() {
|
||||||
@ -132,7 +126,7 @@ class IPFS {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
addObjectToConsole() {
|
addObjectToConsole() {
|
||||||
let ipfs = IpfsApi(this.storageConfig.host, this.storageConfig.port);
|
let ipfs = IpfsApi(this.host, this.port);
|
||||||
this.events.emit("runcode:register", "ipfs", ipfs);
|
this.events.emit("runcode:register", "ipfs", ipfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,20 +5,25 @@ import {findSeries} from 'p-iteration';
|
|||||||
let __embarkStorage = {};
|
let __embarkStorage = {};
|
||||||
|
|
||||||
__embarkStorage.setProviders = async function (dappConnOptions) {
|
__embarkStorage.setProviders = async function (dappConnOptions) {
|
||||||
var self = this;
|
|
||||||
try {
|
try {
|
||||||
let workingConnFound = await findSeries(dappConnOptions, async (dappConn) => {
|
let workingConnFound = await findSeries(dappConnOptions, async (dappConn) => {
|
||||||
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
|
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
|
||||||
let options = dappConnOptions;
|
let options = dappConn;
|
||||||
options.useOnlyCurrentProvider = dappConn === '$BZZ';
|
if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
|
||||||
await EmbarkJS.Storage.setProvider('swarm', options);
|
try{
|
||||||
return EmbarkJS.Storage.isAvailable();
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(dappConn.provider === 'ipfs') {
|
else if(dappConn.provider === 'ipfs') {
|
||||||
// set the provider then check the connection, if true, use that provider, else, check next provider
|
// set the provider then check the connection, if true, use that provider, else, check next provider
|
||||||
try{
|
try{
|
||||||
await EmbarkJS.Storage.setProvider('ipfs', dappConn);
|
await EmbarkJS.Storage.setProvider('ipfs', dappConn);
|
||||||
return EmbarkJS.Storage.isAvailable();
|
let isAvailable = await EmbarkJS.Storage.isAvailable();
|
||||||
|
return isAvailable;
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -26,7 +31,6 @@ __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');
|
if(!workingConnFound) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
self.ipfsConnection = null;
|
throw new Error('Failed to connect to a storage provider: ' + err.message);
|
||||||
throw new Error('Failed to connect to IPFS: ' + err.message);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -5,28 +5,52 @@ let _ = require('underscore');
|
|||||||
|
|
||||||
class Storage {
|
class Storage {
|
||||||
constructor(embark, options){
|
constructor(embark, options){
|
||||||
|
this._embark = embark;
|
||||||
this._storageConfig = options.storageConfig;
|
this._storageConfig = options.storageConfig;
|
||||||
|
|
||||||
let storageProviderCls = require(`../${this._storageConfig.upload.provider}/index.js`);
|
let storageProviderCls = require(`../${this._storageConfig.upload.provider}/index.js`);
|
||||||
this._uploadProvider = new storageProviderCls(embark, this._storageConfig); /*eslint no-new: "off"*/
|
let uploadProvider = new storageProviderCls(embark, options); /*eslint no-new: "off"*/
|
||||||
|
|
||||||
if(typeof this._uploadProvider.initProvider == 'function') this._uploadProvider.initProvider();
|
if(typeof uploadProvider.commandlineDeploy == 'function') uploadProvider.commandlineDeploy();
|
||||||
if(typeof this._uploadProvider.commandlineDeploy == 'function') this._uploadProvider.commandlineDeploy();
|
if(typeof uploadProvider.setServiceCheck == 'function') uploadProvider.setServiceCheck();
|
||||||
if(typeof this._uploadProvider.setServiceCheck == 'function') this._uploadProvider.setServiceCheck();
|
if(typeof uploadProvider.addObjectToConsole == 'function') uploadProvider.addObjectToConsole();
|
||||||
if(typeof this._uploadProvider.addObjectToConsole == 'function') this._uploadProvider.addObjectToConsole();
|
|
||||||
|
|
||||||
// loop through all available providers and add "register provider" code in EmbarkJS
|
// loop through all available providers and add the provider code to embarkjs
|
||||||
// which allows the provider to be set in the DApp
|
|
||||||
this._storageConfig.available_providers.forEach(providerStr => {
|
this._storageConfig.available_providers.forEach(providerStr => {
|
||||||
let storageProviderCls = require(`../${providerStr}/index.js`);
|
let storageProvider;
|
||||||
this._storageProvider = new storageProviderCls(this.storageConfig); /*eslint no-new: "off"*/
|
|
||||||
if(typeof this._storageProvider.addProviderToEmbarkJS == 'function') this._storageProvider.addProviderToEmbarkJS();
|
// 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();
|
||||||
});
|
});
|
||||||
|
|
||||||
// add the code to call setProviders in embarkjs
|
// add the storage provider code (__embarkStorage) to embarkjs
|
||||||
|
this.addProviderToEmbarkJS();
|
||||||
|
|
||||||
|
// add the code to call setProviders in embarkjs after embark is ready
|
||||||
this.addSetProviders();
|
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() {
|
addSetProviders() {
|
||||||
// TODO: make this a shouldAdd condition
|
// TODO: make this a shouldAdd condition
|
||||||
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
|
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
|
||||||
@ -39,11 +63,13 @@ class Storage {
|
|||||||
return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm);
|
return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm);
|
||||||
});
|
});
|
||||||
|
|
||||||
let code = "";
|
let code = `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)});`;
|
||||||
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
|
||||||
code += `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)}));`;
|
let shouldInit = (storageConfig) => {
|
||||||
|
return (connectionsToSet !== undefined && connectionsToSet.length > 0 && storageConfig.enabled === true);
|
||||||
|
};
|
||||||
|
|
||||||
this.embark.addCodeToEmbarkJS(code);
|
this._embark.addProviderInit('storage', code, shouldInit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
/*global web3, some */
|
/*global web3 */
|
||||||
let __embarkSwarm = {};
|
let __embarkSwarm = {};
|
||||||
const bytes = require("eth-lib/lib/bytes");
|
const bytes = require("eth-lib/lib/bytes");
|
||||||
|
|
||||||
__embarkSwarm.setProvider = function (options) {
|
__embarkSwarm.setProvider = function (options) {
|
||||||
this.bzz = web3.bzz;
|
let protocol = options.protocol || 'http';
|
||||||
this.protocol = options.protocol || 'http';
|
let port = options.port ? `:${options.port}` : '';
|
||||||
this.connectUrl = `${this.protocol}://${options.host}:${options.port}`;
|
|
||||||
this.connectError = new Error(`Cannot connect to Swarm node on ${this.connectUrl}`);
|
this._config = options;
|
||||||
this.useOnlyCurrentProvider = options.useOnlyCurrentProvider;
|
this._connectUrl = `${protocol}://${options.host}${port}`;
|
||||||
//this._getUrl = options.getUrl || `${this.connectUrl}/bzzr:/`;
|
this._connectError = new Error(`Cannot connect to Swarm node on ${this._connectUrl}`);
|
||||||
|
|
||||||
this._config = options;
|
this._config = options;
|
||||||
this._connectUrl = `${protocol}://${options.host}${port}`;
|
this._connectUrl = `${protocol}://${options.host}${port}`;
|
||||||
@ -16,8 +16,11 @@ __embarkSwarm.setProvider = function (options) {
|
|||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
if (!this.bzz.currentProvider && !this.useOnlyCurrentProvider) {
|
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
|
||||||
this.bzz.setProvider(this.connectUrl);
|
web3.bzz.setProvider(this._connectUrl);
|
||||||
|
}
|
||||||
|
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
|
||||||
|
web3.bzz.setProvider(web3.bzz.givenProvider);
|
||||||
}
|
}
|
||||||
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
|
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
|
||||||
web3.bzz.setProvider(web3.bzz.givenProvider);
|
web3.bzz.setProvider(web3.bzz.givenProvider);
|
||||||
|
@ -10,24 +10,18 @@ class Swarm {
|
|||||||
this.events = embark.events;
|
this.events = embark.events;
|
||||||
this.buildDir = options.buildDir;
|
this.buildDir = options.buildDir;
|
||||||
this.storageConfig = options.storageConfig;
|
this.storageConfig = options.storageConfig;
|
||||||
this.host = options.host || options.storageConfig.upload.host;
|
|
||||||
this.port = options.port || options.storageConfig.upload.port;
|
|
||||||
this.protocol = options.protocol || options.storageConfig.upload.protocol;
|
|
||||||
this.addCheck = options.addCheck;
|
this.addCheck = options.addCheck;
|
||||||
this.embark = embark;
|
this.embark = embark;
|
||||||
this.bzz = options.bzz;
|
|
||||||
|
let host = options.host || options.storageConfig.upload.host;
|
||||||
// this.initProvider();
|
let port = options.port || options.storageConfig.upload.port;
|
||||||
// this.commandlineDeploy();
|
if(port) port = ':' + port;
|
||||||
// this.setServiceCheck();
|
else port = '';
|
||||||
// this.addProviderToEmbarkJS();
|
let protocol = options.protocol || options.storageConfig.upload.protocol || 'http';
|
||||||
// this.addSetProvider();
|
this.providerUrl = `${protocol}://${host}${port}`;
|
||||||
}
|
this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzzr:/';
|
||||||
|
|
||||||
initProvider(){
|
this.bzz = new Web3Bzz(this.providerUrl);
|
||||||
if(!this.bzz.currentProvider) {
|
|
||||||
this.bzz.setProvider(`${this.protocol}://${this.host}:${this.port}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commandlineDeploy() {
|
commandlineDeploy() {
|
||||||
@ -105,16 +99,6 @@ class Swarm {
|
|||||||
|
|
||||||
this.embark.addCodeToEmbarkJS(code);
|
this.embark.addCodeToEmbarkJS(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// this.embark.addProviderInit('storage', code, shouldInit);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Swarm;
|
module.exports = Swarm;
|
||||||
|
@ -20,10 +20,10 @@
|
|||||||
"development": {
|
"development": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"upload": {
|
"upload": {
|
||||||
"provider": "ipfs",
|
"provider": "swarm",
|
||||||
"host": "localhost",
|
"host": "localhost",
|
||||||
"port": 5001,
|
"port": 8500,
|
||||||
"getUrl": "http://localhost:8080/ipfs/"
|
"getUrl": "http://localhost:8500/bzzr:/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"livenet": {
|
"livenet": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user