From e44b62d5504c896f5be058fa0904ce04a7414af9 Mon Sep 17 00:00:00 2001 From: emizzle Date: Mon, 10 Sep 2018 10:21:52 +1000 Subject: [PATCH] SwarmJS browser support --- lib/modules/swarm/embarkjs.js | 27 ++++++++++++++------------- lib/modules/swarm/index.js | 9 ++++++--- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/modules/swarm/embarkjs.js b/lib/modules/swarm/embarkjs.js index fdce4c80..f874d641 100644 --- a/lib/modules/swarm/embarkjs.js +++ b/lib/modules/swarm/embarkjs.js @@ -1,6 +1,7 @@ /*global web3 */ -let __embarkSwarm = {}; -const bytes = require("eth-lib/lib/bytes"); +import SwarmJS from 'swarmjs'; +let __embarkSwarm = {_swarmConnection: undefined}; +import bytes from "eth-lib/lib/bytes"; __embarkSwarm.setProvider = function (options) { let protocol = options.protocol || 'http'; @@ -13,10 +14,10 @@ __embarkSwarm.setProvider = function (options) { return new Promise((resolve, reject) => { try { if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) { - web3.bzz.setProvider(this._connectUrl); + this._swarmConnection = new SwarmJS({gateway: this._connectUrl}); } else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){ - web3.bzz.setProvider(web3.bzz.givenProvider); + this._swarmConnection = new SwarmJS({gateway: web3.bzz.givenProvider}); } resolve(this); } catch (err) { @@ -29,18 +30,18 @@ __embarkSwarm.setProvider = function (options) { __embarkSwarm.isAvailable = function () { return new Promise((resolve, reject) => { // if web3 swarm object doesn't exist - if (!web3.bzz) { + if (!this.swarm) { return resolve(false); } // 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); + else if(!this._swarmConnection.gateway && this._config.host){ + this._swarmConnection.gateway = this._connectUrl; } - if (!web3.bzz.currentProvider) { + if (!this._swarmConnection.gateway) { return resolve(false); } - web3.bzz.isAvailable() + this._swarmConnection.isAvailable() .then(resolve) .catch(() => { reject(this._connectError); @@ -54,7 +55,7 @@ __embarkSwarm.saveText = function (text) { if (!isAvailable) { return reject(this._connectError); } - web3.bzz.upload(text) + this._swarmConnection.uploadRaw(text) .then(resolve) .catch(reject); }).catch(reject); @@ -67,7 +68,7 @@ __embarkSwarm.get = function (hash) { if (!isAvailable) { return reject(this._connectError); } - web3.bzz.download(hash) + this._swarmConnection.downloadRaw(hash) .then((uint8Array) => resolve(bytes.toString(bytes.fromUint8Array(uint8Array)))) .catch(reject); }).catch(reject); @@ -89,7 +90,7 @@ __embarkSwarm.uploadFile = function (inputSelector) { if (!isAvailable) { return reject(this._connectError); } - web3.bzz.upload(fileContent) + this._swarmConnection.upload(fileContent) .then(resolve) .catch(reject); }).catch(reject); @@ -100,7 +101,7 @@ __embarkSwarm.uploadFile = function (inputSelector) { }; __embarkSwarm.getUrl = function (hash) { - return `${this._config.getUrl || this._connectUrl + '/bzz:/'}${hash}`; + return `${this._config.getUrl || (this._connectUrl + '/bzz-raw:/')}${hash}`; }; const NotAvailable = "Not available with Swarm"; diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index 741c49ea..0425c6c9 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -27,13 +27,12 @@ class Swarm { if (!this.isSwarmEnabledInTheConfig()) { return; } - console.dir('STARTING SWARMJS'); + this.swarm = new SwarmJS({gateway: this.providerUrl}); this.setServiceCheck(); this.addProviderToEmbarkJS(); - - + this.addObjectToConsole(); this.registerUploadCommand(); // swarm needs geth to be running first @@ -49,6 +48,10 @@ class Swarm { }); } + addObjectToConsole() { + this.events.emit("runcode:register", "swarm", this.swarm); + } + setServiceCheck() { let self = this;