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 */
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";

View File

@ -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;