SwarmJS browser support

This commit is contained in:
emizzle 2018-09-10 10:21:52 +10:00
parent 3f5fe21b90
commit e44b62d550
2 changed files with 20 additions and 16 deletions

View File

@ -1,6 +1,7 @@
/*global web3 */ /*global web3 */
let __embarkSwarm = {}; import SwarmJS from 'swarmjs';
const bytes = require("eth-lib/lib/bytes"); let __embarkSwarm = {_swarmConnection: undefined};
import bytes from "eth-lib/lib/bytes";
__embarkSwarm.setProvider = function (options) { __embarkSwarm.setProvider = function (options) {
let protocol = options.protocol || 'http'; let protocol = options.protocol || 'http';
@ -13,10 +14,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 SwarmJS({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 SwarmJS({gateway: web3.bzz.givenProvider});
} }
resolve(this); resolve(this);
} catch (err) { } catch (err) {
@ -29,18 +30,18 @@ __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.swarm) {
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()
.then(resolve) .then(resolve)
.catch(() => { .catch(() => {
reject(this._connectError); reject(this._connectError);
@ -54,7 +55,7 @@ __embarkSwarm.saveText = function (text) {
if (!isAvailable) { if (!isAvailable) {
return reject(this._connectError); return reject(this._connectError);
} }
web3.bzz.upload(text) this._swarmConnection.uploadRaw(text)
.then(resolve) .then(resolve)
.catch(reject); .catch(reject);
}).catch(reject); }).catch(reject);
@ -67,7 +68,7 @@ __embarkSwarm.get = function (hash) {
if (!isAvailable) { if (!isAvailable) {
return reject(this._connectError); return reject(this._connectError);
} }
web3.bzz.download(hash) this._swarmConnection.downloadRaw(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);
@ -89,7 +90,7 @@ __embarkSwarm.uploadFile = function (inputSelector) {
if (!isAvailable) { if (!isAvailable) {
return reject(this._connectError); return reject(this._connectError);
} }
web3.bzz.upload(fileContent) this._swarmConnection.upload(fileContent)
.then(resolve) .then(resolve)
.catch(reject); .catch(reject);
}).catch(reject); }).catch(reject);
@ -100,7 +101,7 @@ __embarkSwarm.uploadFile = function (inputSelector) {
}; };
__embarkSwarm.getUrl = function (hash) { __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"; const NotAvailable = "Not available with Swarm";

View File

@ -27,13 +27,12 @@ class Swarm {
if (!this.isSwarmEnabledInTheConfig()) { if (!this.isSwarmEnabledInTheConfig()) {
return; return;
} }
console.dir('STARTING SWARMJS');
this.swarm = new SwarmJS({gateway: this.providerUrl}); this.swarm = new SwarmJS({gateway: this.providerUrl});
this.setServiceCheck(); this.setServiceCheck();
this.addProviderToEmbarkJS(); this.addProviderToEmbarkJS();
this.addObjectToConsole();
this.registerUploadCommand(); this.registerUploadCommand();
// swarm needs geth to be running first // swarm needs geth to be running first
@ -49,6 +48,10 @@ class Swarm {
}); });
} }
addObjectToConsole() {
this.events.emit("runcode:register", "swarm", this.swarm);
}
setServiceCheck() { setServiceCheck() {
let self = this; let self = this;