Merge pull request #802 from embark-framework/swarm-support-latest
Switch to SwarmJS which supports latest version of Swarm
This commit is contained in:
commit
1bbd8beef8
|
@ -29,6 +29,7 @@ class Console {
|
||||||
// TODO: only if the blockchain is actually active!
|
// TODO: only if the blockchain is actually active!
|
||||||
// will need to pass te current embark state here
|
// will need to pass te current embark state here
|
||||||
'ipfs - ' + __('instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)'),
|
'ipfs - ' + __('instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)'),
|
||||||
|
'swarm - ' + __('instantiated swarm-api object configured to the current environment (available if swarm is enabled)'),
|
||||||
'web3 - ' + __('instantiated web3.js object configured to the current environment'),
|
'web3 - ' + __('instantiated web3.js object configured to the current environment'),
|
||||||
'EmbarkJS - ' + __('EmbarkJS static functions for Storage, Messages, Names, etc.'),
|
'EmbarkJS - ' + __('EmbarkJS static functions for Storage, Messages, Names, etc.'),
|
||||||
'quit - ' + __('to immediatly exit (alias: exit)'),
|
'quit - ' + __('to immediatly exit (alias: exit)'),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*global web3 */
|
/*global web3 */
|
||||||
let __embarkSwarm = {};
|
let __embarkSwarm = {_swarmConnection: undefined};
|
||||||
const bytes = require("eth-lib/lib/bytes");
|
import SwarmAPI from 'swarm-api';
|
||||||
|
|
||||||
__embarkSwarm.setProvider = function (options) {
|
__embarkSwarm.setProvider = function (options) {
|
||||||
let protocol = options.protocol || 'http';
|
let protocol = options.protocol || 'http';
|
||||||
|
@ -13,10 +13,10 @@ __embarkSwarm.setProvider = function (options) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
|
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
|
||||||
web3.bzz.setProvider(this._connectUrl);
|
this._swarmConnection = new SwarmAPI({gateway: this._connectUrl});
|
||||||
}
|
}
|
||||||
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
|
else if (options.useOnlyGivenProvider && web3.bzz.givenProvider !== null) {
|
||||||
web3.bzz.setProvider(web3.bzz.givenProvider);
|
this._swarmConnection = new SwarmAPI({gateway: web3.bzz.givenProvider});
|
||||||
}
|
}
|
||||||
resolve(this);
|
resolve(this);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -29,21 +29,20 @@ __embarkSwarm.setProvider = function (options) {
|
||||||
__embarkSwarm.isAvailable = function () {
|
__embarkSwarm.isAvailable = function () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// if web3 swarm object doesn't exist
|
// if web3 swarm object doesn't exist
|
||||||
if (!web3.bzz) {
|
if (!this._swarmConnection) {
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
}
|
}
|
||||||
// swarm obj exists, but has no provider set (seems to happen a LOT!),
|
// swarm obj exists, but has no provider set (seems to happen a LOT!),
|
||||||
// try setting the provider to our currently set provider again
|
// try setting the provider to our currently set provider again
|
||||||
else if(!web3.bzz.currentProvider && this._config.host){
|
else if (!this._swarmConnection.gateway && this._config.host) {
|
||||||
web3.bzz.setProvider(this._connectUrl);
|
this._swarmConnection.gateway = this._connectUrl;
|
||||||
}
|
}
|
||||||
if (!web3.bzz.currentProvider) {
|
if (!this._swarmConnection.gateway) {
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
}
|
}
|
||||||
web3.bzz.isAvailable()
|
this._swarmConnection.isAvailable((err, isAvailable) => {
|
||||||
.then(resolve)
|
if (err) return reject(err);
|
||||||
.catch(() => {
|
resolve(isAvailable);
|
||||||
reject(this._connectError);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -54,10 +53,11 @@ __embarkSwarm.saveText = function (text) {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this._connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
web3.bzz.upload(text)
|
this._swarmConnection.uploadRaw(text, (err, hash) => {
|
||||||
.then(resolve)
|
if (err) return reject(err);
|
||||||
.catch(reject);
|
resolve(hash);
|
||||||
}).catch(reject);
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -67,10 +67,11 @@ __embarkSwarm.get = function (hash) {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this._connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
web3.bzz.download(hash)
|
this._swarmConnection.downloadRaw(hash, (err, content) => {
|
||||||
.then((uint8Array) => resolve(bytes.toString(bytes.fromUint8Array(uint8Array))))
|
if (err) return reject(err);
|
||||||
.catch(reject);
|
resolve(content);
|
||||||
}).catch(reject);
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -89,10 +90,11 @@ __embarkSwarm.uploadFile = function (inputSelector) {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this._connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
web3.bzz.upload(fileContent)
|
this._swarmConnection.uploadRaw(fileContent, (err, hash) => {
|
||||||
.then(resolve)
|
if (err) return reject(err);
|
||||||
.catch(reject);
|
resolve(hash);
|
||||||
}).catch(reject);
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
reader.onerror = reject;
|
reader.onerror = reject;
|
||||||
reader.readAsArrayBuffer(file);
|
reader.readAsArrayBuffer(file);
|
||||||
|
@ -100,7 +102,7 @@ __embarkSwarm.uploadFile = function (inputSelector) {
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkSwarm.getUrl = function (hash) {
|
__embarkSwarm.getUrl = function (hash) {
|
||||||
return `${this._config.getUrl || this._connectUrl + '/bzz:/'}${hash}`;
|
return `${this._connectUrl}/bzz-raw:/${hash}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const NotAvailable = "Not available with Swarm";
|
const NotAvailable = "Not available with Swarm";
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
const UploadSwarm = require('./upload.js');
|
const UploadSwarm = require('./upload.js');
|
||||||
const utils = require('../../utils/utils.js');
|
const utils = require('../../utils/utils.js');
|
||||||
const fs = require('../../core/fs.js');
|
const fs = require('../../core/fs.js');
|
||||||
const Web3Bzz = require('web3-bzz');
|
const SwarmAPI = require('swarm-api');
|
||||||
// TODO: not great, breaks module isolation
|
// TODO: not great, breaks module isolation
|
||||||
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
|
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
|
||||||
|
const constants = require('../../constants.json');
|
||||||
|
|
||||||
class Swarm {
|
class Swarm {
|
||||||
|
|
||||||
|
@ -27,21 +28,28 @@ class Swarm {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.bzz = new Web3Bzz(this.providerUrl);
|
this.swarm = new SwarmAPI({gateway: this.providerUrl});
|
||||||
|
|
||||||
this.setServiceCheck();
|
this.setServiceCheck();
|
||||||
this.addProviderToEmbarkJS();
|
this.addProviderToEmbarkJS();
|
||||||
// TODO add check to see if we need to start process
|
this.addObjectToConsole();
|
||||||
this.startProcess(() => {});
|
|
||||||
this.registerUploadCommand();
|
this.registerUploadCommand();
|
||||||
|
|
||||||
this._checkService((err) => {
|
// swarm needs geth to be running first
|
||||||
if (!err) {
|
this.events.once(constants.blockchain.blockchainReady, () => {
|
||||||
|
this.swarm.isAvailable((err, isAvailable) => {
|
||||||
|
if (!err || isAvailable) {
|
||||||
|
this.logger.info("Swarm node found, using currently running node");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.logger.info("Swarm node not found, attempting to start own node");
|
this.logger.info("SWARM: Swarm node not found, attempting to start own node");
|
||||||
self.startProcess(() => {});
|
return this.startProcess(() => {});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
addObjectToConsole() {
|
||||||
|
this.events.emit("runcode:register", "swarm", this.swarm);
|
||||||
}
|
}
|
||||||
|
|
||||||
setServiceCheck() {
|
setServiceCheck() {
|
||||||
|
@ -55,23 +63,21 @@ class Swarm {
|
||||||
self.logger.info(__('Swarm node is offline...'));
|
self.logger.info(__('Swarm node is offline...'));
|
||||||
});
|
});
|
||||||
|
|
||||||
self.events.request("services:register", 'Swarm', function(cb){
|
self.events.request("services:register", 'Swarm', function (cb) {
|
||||||
self.logger.trace(`Checking Swarm availability on ${self.bzz.currentProvider}...`);
|
self.logger.trace(`Checking Swarm availability on ${self.providerUrl}...`);
|
||||||
self._checkService((err, result) => {
|
self._checkService((err, result) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
self.logger.trace("Check Swarm availability error: " + err);
|
self.logger.trace("Check Swarm availability error: " + err);
|
||||||
return cb({name: "Swarm ", status: 'off'});
|
return cb({name: "Swarm ", status: 'off'});
|
||||||
}
|
}
|
||||||
self.logger.trace("Swarm " + (result ? '':'on') + "available");
|
self.logger.trace("Swarm " + (result ? '' : 'un') + "available");
|
||||||
return cb({name: "Swarm ", status: result ? 'on':'off'});
|
return cb({name: "Swarm ", status: result ? 'on' : 'off'});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkService(cb) {
|
_checkService(cb) {
|
||||||
this.bzz.isAvailable().then(result => {
|
this.swarm.isAvailable(cb);
|
||||||
cb(null, result);
|
|
||||||
}).catch(cb);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addProviderToEmbarkJS() {
|
addProviderToEmbarkJS() {
|
||||||
|
@ -101,8 +107,8 @@ class Swarm {
|
||||||
let upload_swarm = new UploadSwarm({
|
let upload_swarm = new UploadSwarm({
|
||||||
buildDir: self.buildDir || 'dist/',
|
buildDir: self.buildDir || 'dist/',
|
||||||
storageConfig: self.storageConfig,
|
storageConfig: self.storageConfig,
|
||||||
getUrl: self.getUrl,
|
providerUrl: self.providerUrl,
|
||||||
bzz: self.bzz
|
swarm: self.swarm
|
||||||
});
|
});
|
||||||
|
|
||||||
upload_swarm.deploy(cb);
|
upload_swarm.deploy(cb);
|
||||||
|
|
|
@ -5,33 +5,26 @@ class Swarm {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.buildDir = options.buildDir || 'dist/';
|
this.buildDir = options.buildDir || 'dist/';
|
||||||
this.bzz = options.bzz;
|
this.swarm = options.swarm;
|
||||||
this.getUrl = options.getUrl;
|
this.providerUrl = options.providerUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
deploy(cb) {
|
deploy(cb) {
|
||||||
console.log(__("deploying to swarm!"));
|
console.log(__("deploying to swarm!"));
|
||||||
let self = this;
|
const self = this;
|
||||||
let bzz = this.bzz;
|
const swarm = this.swarm;
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function runCommand(callback) {
|
function runCommand(callback) {
|
||||||
console.log(("=== " + __("adding %s to swarm", self.buildDir)).green);
|
console.log(("=== " + __("adding %s to swarm", self.buildDir)).green);
|
||||||
bzz.upload({
|
swarm.uploadDirectory(self.buildDir, 'index.html', callback);
|
||||||
path: self.buildDir, // path to data / file / directory
|
|
||||||
kind: "directory", // could also be "file" or "data"
|
|
||||||
defaultFile: "index.html" // optional, and only for kind === "directory"
|
|
||||||
})
|
|
||||||
.then((success) => {
|
|
||||||
callback(null, success);
|
|
||||||
})
|
|
||||||
.catch(callback);
|
|
||||||
},
|
},
|
||||||
function printUrls(dir_hash, callback) {
|
function printUrls(dir_hash, callback) {
|
||||||
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.getUrl}${dir_hash}/`).green);
|
console.log(("=== " + __("DApp available at") + ` ${self.providerUrl}/bzz:/${dir_hash}/index.html`).green);
|
||||||
console.log(("=== " + __("DApp available at") + ` https://swarm-gateways.net/bzz:/${dir_hash}`).green);
|
console.log(("=== " + __("DApp available at") + ` https://swarm-gateways.net/bzz:/${dir_hash}/index.html`).green);
|
||||||
|
console.log(("=== " + __("NOTE: Swarm AND a blockchain node must be running for the dApp to work correctly (ie 'embark run')").yellow));
|
||||||
|
|
||||||
callback(null, dir_hash);
|
callback(null, dir_hash);
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -81,6 +81,7 @@
|
||||||
"solc": "0.4.24",
|
"solc": "0.4.24",
|
||||||
"string-replace-async": "^1.2.1",
|
"string-replace-async": "^1.2.1",
|
||||||
"style-loader": "^0.19.0",
|
"style-loader": "^0.19.0",
|
||||||
|
"swarm-api": "^0.1.2",
|
||||||
"tar": "^3.1.5",
|
"tar": "^3.1.5",
|
||||||
"toposort": "^1.0.0",
|
"toposort": "^1.0.0",
|
||||||
"underscore": "^1.9.0",
|
"underscore": "^1.9.0",
|
||||||
|
|
Loading…
Reference in New Issue