Swarm updates for embark

Change __embarkSwarm to use new SwarmJS and change over from promises.

Add note after upload that blockchain nodes needs to be running to access the swarm uploaded dapp.

Fix typo with swarm “on” available.

Linting / cleanup
This commit is contained in:
emizzle 2018-09-10 16:27:19 +10:00
parent e44b62d550
commit 9d39fa39ac
3 changed files with 29 additions and 27 deletions

View File

@ -1,7 +1,6 @@
/*global web3 */
import SwarmJS from 'swarmjs';
let __embarkSwarm = {_swarmConnection: undefined};
import bytes from "eth-lib/lib/bytes";
import SwarmJS from 'swarmjs';
__embarkSwarm.setProvider = function (options) {
let protocol = options.protocol || 'http';
@ -16,7 +15,7 @@ __embarkSwarm.setProvider = function (options) {
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
this._swarmConnection = new SwarmJS({gateway: this._connectUrl});
}
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
else if (options.useOnlyGivenProvider && web3.bzz.givenProvider !== null) {
this._swarmConnection = new SwarmJS({gateway: web3.bzz.givenProvider});
}
resolve(this);
@ -30,21 +29,20 @@ __embarkSwarm.setProvider = function (options) {
__embarkSwarm.isAvailable = function () {
return new Promise((resolve, reject) => {
// if web3 swarm object doesn't exist
if (!this.swarm) {
if (!this._swarmConnection) {
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(!this._swarmConnection.gateway && this._config.host){
else if (!this._swarmConnection.gateway && this._config.host) {
this._swarmConnection.gateway = this._connectUrl;
}
if (!this._swarmConnection.gateway) {
return resolve(false);
}
this._swarmConnection.isAvailable()
.then(resolve)
.catch(() => {
reject(this._connectError);
this._swarmConnection.isAvailable((err, isAvailable) => {
if (err) return reject(err);
resolve(isAvailable);
});
});
};
@ -55,10 +53,11 @@ __embarkSwarm.saveText = function (text) {
if (!isAvailable) {
return reject(this._connectError);
}
this._swarmConnection.uploadRaw(text)
.then(resolve)
.catch(reject);
}).catch(reject);
this._swarmConnection.uploadRaw(text, (err, hash) => {
if (err) return reject(err);
resolve(hash);
});
});
});
};
@ -68,10 +67,11 @@ __embarkSwarm.get = function (hash) {
if (!isAvailable) {
return reject(this._connectError);
}
this._swarmConnection.downloadRaw(hash)
.then((uint8Array) => resolve(bytes.toString(bytes.fromUint8Array(uint8Array))))
.catch(reject);
}).catch(reject);
this._swarmConnection.downloadRaw(hash, (err, content) => {
if (err) return reject(err);
resolve(content);
});
});
});
};
@ -90,10 +90,11 @@ __embarkSwarm.uploadFile = function (inputSelector) {
if (!isAvailable) {
return reject(this._connectError);
}
this._swarmConnection.upload(fileContent)
.then(resolve)
.catch(reject);
}).catch(reject);
this._swarmConnection.uploadRaw(fileContent, (err, hash) => {
if (err) return reject(err);
resolve(hash);
});
});
};
reader.onerror = reject;
reader.readAsArrayBuffer(file);
@ -101,7 +102,7 @@ __embarkSwarm.uploadFile = function (inputSelector) {
};
__embarkSwarm.getUrl = function (hash) {
return `${this._config.getUrl || (this._connectUrl + '/bzz-raw:/')}${hash}`;
return `${this._connectUrl}/bzz-raw:/${hash}`;
};
const NotAvailable = "Not available with Swarm";

View File

@ -70,7 +70,7 @@ class Swarm {
self.logger.trace("Check Swarm availability error: " + err);
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'});
});
});
@ -107,7 +107,7 @@ class Swarm {
let upload_swarm = new UploadSwarm({
buildDir: self.buildDir || 'dist/',
storageConfig: self.storageConfig,
getUrl: self.getUrl,
providerUrl: self.providerUrl,
swarm: self.swarm
});

View File

@ -6,7 +6,7 @@ class Swarm {
this.options = options;
this.buildDir = options.buildDir || 'dist/';
this.swarm = options.swarm;
this.getUrl = options.getUrl;
this.providerUrl = options.providerUrl;
}
deploy(cb) {
@ -22,8 +22,9 @@ class Swarm {
if (!dir_hash) {
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}/`).green);
console.log(("=== " + __("DApp available at") + ` https://swarm-gateways.net/bzz:/${dir_hash}`).green);
console.log(("=== " + __("NOTE: A blockchain node must be running for the dApp to work correctly (ie 'embark run' or 'embark blockchain')").yellow));
callback(null, dir_hash);
}