mirror of https://github.com/embarklabs/embark.git
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:
parent
e44b62d550
commit
9d39fa39ac
|
@ -1,7 +1,6 @@
|
||||||
/*global web3 */
|
/*global web3 */
|
||||||
import SwarmJS from 'swarmjs';
|
|
||||||
let __embarkSwarm = {_swarmConnection: undefined};
|
let __embarkSwarm = {_swarmConnection: undefined};
|
||||||
import bytes from "eth-lib/lib/bytes";
|
import SwarmJS from 'swarmjs';
|
||||||
|
|
||||||
__embarkSwarm.setProvider = function (options) {
|
__embarkSwarm.setProvider = function (options) {
|
||||||
let protocol = options.protocol || 'http';
|
let protocol = options.protocol || 'http';
|
||||||
|
@ -16,7 +15,7 @@ __embarkSwarm.setProvider = function (options) {
|
||||||
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
|
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
|
||||||
this._swarmConnection = new SwarmJS({gateway: 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) {
|
||||||
this._swarmConnection = new SwarmJS({gateway: web3.bzz.givenProvider});
|
this._swarmConnection = new SwarmJS({gateway: web3.bzz.givenProvider});
|
||||||
}
|
}
|
||||||
resolve(this);
|
resolve(this);
|
||||||
|
@ -30,22 +29,21 @@ __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 (!this.swarm) {
|
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(!this._swarmConnection.gateway && this._config.host){
|
else if (!this._swarmConnection.gateway && this._config.host) {
|
||||||
this._swarmConnection.gateway = this._connectUrl;
|
this._swarmConnection.gateway = this._connectUrl;
|
||||||
}
|
}
|
||||||
if (!this._swarmConnection.gateway) {
|
if (!this._swarmConnection.gateway) {
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
}
|
}
|
||||||
this._swarmConnection.isAvailable()
|
this._swarmConnection.isAvailable((err, isAvailable) => {
|
||||||
.then(resolve)
|
if (err) return reject(err);
|
||||||
.catch(() => {
|
resolve(isAvailable);
|
||||||
reject(this._connectError);
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -55,10 +53,11 @@ __embarkSwarm.saveText = function (text) {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this._connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
this._swarmConnection.uploadRaw(text)
|
this._swarmConnection.uploadRaw(text, (err, hash) => {
|
||||||
.then(resolve)
|
if (err) return reject(err);
|
||||||
.catch(reject);
|
resolve(hash);
|
||||||
}).catch(reject);
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -68,10 +67,11 @@ __embarkSwarm.get = function (hash) {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this._connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
this._swarmConnection.downloadRaw(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);
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -90,10 +90,11 @@ __embarkSwarm.uploadFile = function (inputSelector) {
|
||||||
if (!isAvailable) {
|
if (!isAvailable) {
|
||||||
return reject(this._connectError);
|
return reject(this._connectError);
|
||||||
}
|
}
|
||||||
this._swarmConnection.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);
|
||||||
|
@ -101,7 +102,7 @@ __embarkSwarm.uploadFile = function (inputSelector) {
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkSwarm.getUrl = function (hash) {
|
__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";
|
const NotAvailable = "Not available with Swarm";
|
||||||
|
|
|
@ -70,7 +70,7 @@ class Swarm {
|
||||||
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'});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -107,7 +107,7 @@ 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,
|
||||||
swarm: self.swarm
|
swarm: self.swarm
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ class Swarm {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.buildDir = options.buildDir || 'dist/';
|
this.buildDir = options.buildDir || 'dist/';
|
||||||
this.swarm = options.swarm;
|
this.swarm = options.swarm;
|
||||||
this.getUrl = options.getUrl;
|
this.providerUrl = options.providerUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
deploy(cb) {
|
deploy(cb) {
|
||||||
|
@ -22,8 +22,9 @@ class Swarm {
|
||||||
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}/`).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}`).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);
|
callback(null, dir_hash);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue