mirror of https://github.com/embarklabs/embark.git
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
0a331e9352
commit
30f0f4dc72
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")');
|
||||||
|
|
|
@ -203,7 +203,7 @@ class Cmd {
|
||||||
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
|
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
|
||||||
.option('--locale [locale]', __('language to use (default: en)'))
|
.option('--locale [locale]', __('language to use (default: en)'))
|
||||||
.option('-c, --client [client]', __('Use a specific ethereum client or simulator (supported: %s)', 'geth, testrpc'))
|
.option('-c, --client [client]', __('Use a specific ethereum client or simulator (supported: %s)', 'geth, testrpc'))
|
||||||
.description(__('Upload your dapp to a decentralized storage') + ' (e.g embark upload ipfs).')
|
.description(__('Upload your dapp to a decentralized storage') + '.')
|
||||||
.action(function (env, _options) {
|
.action(function (env, _options) {
|
||||||
i18n.setOrDetectLocale(_options.locale);
|
i18n.setOrDetectLocale(_options.locale);
|
||||||
_options.env = env || 'development';
|
_options.env = env || 'development';
|
||||||
|
|
|
@ -134,7 +134,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: \"",
|
||||||
|
|
|
@ -7,7 +7,8 @@ __embarkIPFS.setProvider = function (options) {
|
||||||
var promise = new Promise(function (resolve, reject) {
|
var promise = new Promise(function (resolve, reject) {
|
||||||
try {
|
try {
|
||||||
if (options === undefined) {
|
if (options === undefined) {
|
||||||
self.ipfsConnection = IpfsApi('localhost', '5001');
|
self._config = options;
|
||||||
|
self._ipfsConnection = IpfsApi('localhost', '5001');
|
||||||
self._getUrl = "http://localhost:8080/ipfs/";
|
self._getUrl = "http://localhost:8080/ipfs/";
|
||||||
} else {
|
} else {
|
||||||
var ipfsOptions = {host: options.host || options.server, protocol: 'http'};
|
var ipfsOptions = {host: options.host || options.server, protocol: 'http'};
|
||||||
|
@ -17,13 +18,13 @@ __embarkIPFS.setProvider = function (options) {
|
||||||
if (options.port && options.port !== 'false') {
|
if (options.port && options.port !== 'false') {
|
||||||
ipfsOptions.port = options.port;
|
ipfsOptions.port = options.port;
|
||||||
}
|
}
|
||||||
self.ipfsConnection = IpfsApi(ipfsOptions);
|
self._ipfsConnection = IpfsApi(ipfsOptions);
|
||||||
self._getUrl = options.getUrl || "http://localhost:8080/ipfs/";
|
self._getUrl = options.getUrl || "http://localhost:8080/ipfs/";
|
||||||
}
|
}
|
||||||
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'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -33,11 +34,11 @@ __embarkIPFS.setProvider = function (options) {
|
||||||
__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) {
|
||||||
if (!self.ipfsConnection) {
|
if (!self._ipfsConnection) {
|
||||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||||
reject(connectionError);
|
reject(connectionError);
|
||||||
}
|
}
|
||||||
self.ipfsConnection.add(self.ipfsConnection.Buffer.from(text), function (err, result) {
|
self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
@ -54,11 +55,11 @@ __embarkIPFS.get = function (hash) {
|
||||||
// TODO: detect type, then convert if needed
|
// TODO: detect type, then convert if needed
|
||||||
//var ipfsHash = web3.toAscii(hash);
|
//var ipfsHash = web3.toAscii(hash);
|
||||||
var promise = new Promise(function (resolve, reject) {
|
var promise = new Promise(function (resolve, reject) {
|
||||||
if (!self.ipfsConnection) {
|
if (!self._ipfsConnection) {
|
||||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||||
reject(connectionError);
|
reject(connectionError);
|
||||||
}
|
}
|
||||||
self.ipfsConnection.get(hash, function (err, files) {
|
self._ipfsConnection.get(hash, function (err, files) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
|
@ -78,15 +79,15 @@ __embarkIPFS.uploadFile = function (inputSelector) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var promise = new Promise(function (resolve, reject) {
|
var promise = new Promise(function (resolve, reject) {
|
||||||
if (!self.ipfsConnection) {
|
if (!self._ipfsConnection) {
|
||||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||||
reject(connectionError);
|
reject(connectionError);
|
||||||
}
|
}
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.onloadend = function () {
|
reader.onloadend = function () {
|
||||||
var fileContent = reader.result;
|
var fileContent = reader.result;
|
||||||
var buffer = self.ipfsConnection.Buffer.from(fileContent);
|
var buffer = self._ipfsConnection.Buffer.from(fileContent);
|
||||||
self.ipfsConnection.add(buffer, function (err, result) {
|
self._ipfsConnection.add(buffer, function (err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
@ -102,10 +103,10 @@ __embarkIPFS.uploadFile = function (inputSelector) {
|
||||||
|
|
||||||
__embarkIPFS.isAvailable = function () {
|
__embarkIPFS.isAvailable = function () {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
if (!this.ipfsConnection) {
|
if (!this._ipfsConnection) {
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
}
|
}
|
||||||
this.ipfsConnection.id()
|
this._ipfsConnection.id()
|
||||||
.then((id) => {
|
.then((id) => {
|
||||||
resolve(Boolean(id));
|
resolve(Boolean(id));
|
||||||
})
|
})
|
||||||
|
|
|
@ -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');
|
||||||
|
|
||||||
class IPFS {
|
class IPFS {
|
||||||
|
@ -17,18 +17,12 @@ 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() {
|
||||||
let upload_ipfs = new UploadIPFS({
|
let upload_ipfs = new UploadIPFS({
|
||||||
buildDir: this.buildDir || 'dist/',
|
buildDir: this.buildDir || 'dist/',
|
||||||
storageConfig: this.storageConfig,
|
storageConfig: this.storageConfig.upload,
|
||||||
configIpfsBin: this.storageConfig.ipfs_bin || "ipfs"
|
configIpfsBin: this.storageConfig.ipfs_bin || "ipfs"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -127,7 +121,7 @@ class IPFS {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
addObjectToConsole() {
|
addObjectToConsole() {
|
||||||
let ipfs = IpfsApi(this.storageConfig.host, this.storageConfig.port);
|
let ipfs = IpfsApi(this.host, this.port);
|
||||||
RunCode.doEval("", {ipfs: ipfs});
|
RunCode.doEval("", {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};
|
||||||
|
try{
|
||||||
await EmbarkJS.Storage.setProvider('swarm', options);
|
await EmbarkJS.Storage.setProvider('swarm', options);
|
||||||
return EmbarkJS.Storage.isAvailable();
|
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 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`);
|
let storageProviderCls = require(`../${providerStr}/index.js`);
|
||||||
this._storageProvider = new storageProviderCls(this.storageConfig); /*eslint no-new: "off"*/
|
storageProvider = new storageProviderCls(embark, options); /*eslint no-new: "off"*/
|
||||||
if(typeof this._storageProvider.addProviderToEmbarkJS == 'function') this._storageProvider.addProviderToEmbarkJS();
|
}
|
||||||
|
|
||||||
|
// 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)}));`;
|
|
||||||
|
|
||||||
this.embark.addCodeToEmbarkJS(code);
|
let shouldInit = (storageConfig) => {
|
||||||
|
return (connectionsToSet !== undefined && connectionsToSet.length > 0 && storageConfig.enabled === true);
|
||||||
|
};
|
||||||
|
|
||||||
|
this._embark.addProviderInit('storage', code, shouldInit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,37 +1,49 @@
|
||||||
/*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}`);
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
resolve(this);
|
resolve(this);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
reject(this.connectError);
|
reject(this._connectError);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkSwarm.isAvailable = function () {
|
__embarkSwarm.isAvailable = function () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.bzz) {
|
// if web3 swarm object doesn't exist
|
||||||
|
if (!web3.bzz) {
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
}
|
}
|
||||||
this.bzz.isAvailable()
|
// swarm obj exists, but has no provider set (seems to happen a LOT!),
|
||||||
|
// try setting the provider to our currently set provider again
|
||||||
|
else if(!web3.bzz.currentProvider && this._config.host){
|
||||||
|
web3.bzz.setProvider(this._connectUrl);
|
||||||
|
}
|
||||||
|
if (!web3.bzz.currentProvider) {
|
||||||
|
return resolve(false);
|
||||||
|
}
|
||||||
|
web3.bzz.isAvailable()
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
reject(this.connectError);
|
reject(this._connectError);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -40,9 +52,9 @@ __embarkSwarm.saveText = function (text) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.isAvailable().then((isAvailable) => {
|
this.isAvailable().then((isAvailable) => {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this.connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
this.bzz.upload(text)
|
web3.bzz.upload(text)
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
|
@ -53,9 +65,9 @@ __embarkSwarm.get = function (hash) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.isAvailable().then((isAvailable) => {
|
this.isAvailable().then((isAvailable) => {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this.connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
this.bzz.download(hash)
|
web3.bzz.download(hash)
|
||||||
.then((uint8Array) => resolve(bytes.toString(bytes.fromUint8Array(uint8Array))))
|
.then((uint8Array) => resolve(bytes.toString(bytes.fromUint8Array(uint8Array))))
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
|
@ -75,9 +87,9 @@ __embarkSwarm.uploadFile = function (inputSelector) {
|
||||||
const fileContent = new Uint8Array(event.target.result);
|
const fileContent = new Uint8Array(event.target.result);
|
||||||
this.isAvailable().then((isAvailable) => {
|
this.isAvailable().then((isAvailable) => {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this.connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
this.bzz.upload(fileContent)
|
web3.bzz.upload(fileContent)
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
|
@ -88,6 +100,6 @@ __embarkSwarm.uploadFile = function (inputSelector) {
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkSwarm.getUrl = function (hash) {
|
__embarkSwarm.getUrl = function (hash) {
|
||||||
return this._getUrl + hash;
|
return `${this._config.getUrl || this._connectUrl + '/bzzr:/'}${hash}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
let UploadSwarm = require('./upload.js');
|
const UploadSwarm = 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');
|
||||||
|
const Web3Bzz = require('web3-bzz');
|
||||||
|
|
||||||
class Swarm {
|
class Swarm {
|
||||||
|
|
||||||
|
@ -9,30 +10,26 @@ 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;
|
|
||||||
|
|
||||||
// this.initProvider();
|
let host = options.host || options.storageConfig.upload.host;
|
||||||
// this.commandlineDeploy();
|
let port = options.port || options.storageConfig.upload.port;
|
||||||
// this.setServiceCheck();
|
if(port) port = ':' + port;
|
||||||
// this.addProviderToEmbarkJS();
|
else port = '';
|
||||||
// this.addSetProvider();
|
let protocol = options.protocol || options.storageConfig.upload.protocol || 'http';
|
||||||
}
|
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() {
|
||||||
this.upload_swarm = new UploadSwarm({
|
this.upload_swarm = new UploadSwarm({
|
||||||
buildDir: this.buildDir || 'dist/',
|
buildDir: this.buildDir || 'dist/',
|
||||||
storageConfig: this.storageConfig,
|
storageConfig: this.storageConfig,
|
||||||
|
connectUrl: this.providerUrl,
|
||||||
|
getUrl: this.getUrl,
|
||||||
bzz: this.bzz
|
bzz: this.bzz
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -102,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;
|
||||||
|
|
|
@ -7,6 +7,8 @@ class Swarm {
|
||||||
this.buildDir = options.buildDir || 'dist/';
|
this.buildDir = options.buildDir || 'dist/';
|
||||||
this.bzz = options.bzz;
|
this.bzz = options.bzz;
|
||||||
this.storageConfig = options.storageConfig;
|
this.storageConfig = options.storageConfig;
|
||||||
|
this.providerUrl = options.providerUrl;
|
||||||
|
this.getUrl = options.getUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
deploy() {
|
deploy() {
|
||||||
|
@ -31,7 +33,8 @@ class Swarm {
|
||||||
if (!dir_hash) {
|
if (!dir_hash) {
|
||||||
return callback('No directory hash was returned');
|
return callback('No directory hash was returned');
|
||||||
}
|
}
|
||||||
console.log(("=== " + __("DApp available at") + `${self.storageConfig.getUrl}${dir_hash}/`).green);
|
console.log(("=== " + __("DApp available at") + ` ${self.getUrl}${dir_hash}/`).green);
|
||||||
|
console.log(("=== " + __("DApp available at") + ` http://swarm-gateways.net/bzzr:/${dir_hash}`).green);
|
||||||
|
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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…
Reference in New Issue