2018-05-30 06:34:36 +00:00
|
|
|
const UploadIPFS = require('./upload.js');
|
|
|
|
const utils = require('../../utils/utils.js');
|
|
|
|
const fs = require('../../core/fs.js');
|
2018-05-30 06:34:36 +00:00
|
|
|
const IpfsApi = require('ipfs-api');
|
2018-05-25 07:13:57 +00:00
|
|
|
const _ = require('underscore');
|
2017-12-27 00:55:42 +00:00
|
|
|
|
|
|
|
class IPFS {
|
|
|
|
|
|
|
|
constructor(embark, options) {
|
|
|
|
this.logger = embark.logger;
|
2017-12-27 01:32:51 +00:00
|
|
|
this.events = embark.events;
|
|
|
|
this.buildDir = options.buildDir;
|
2018-06-01 15:58:11 +00:00
|
|
|
this.storageConfig = embark.config.storageConfig;
|
2018-05-25 07:13:57 +00:00
|
|
|
this.host = options.host || this.storageConfig.upload.host;
|
|
|
|
this.port = options.port || this.storageConfig.upload.port;
|
|
|
|
this.protocol = options.protocol || this.storageConfig.upload.protocol;
|
2017-12-27 01:32:51 +00:00
|
|
|
this.embark = embark;
|
|
|
|
}
|
|
|
|
|
|
|
|
commandlineDeploy() {
|
|
|
|
let upload_ipfs = new UploadIPFS({
|
|
|
|
buildDir: this.buildDir || 'dist/',
|
2018-05-30 06:34:36 +00:00
|
|
|
storageConfig: this.storageConfig.upload,
|
2017-12-27 01:32:51 +00:00
|
|
|
configIpfsBin: this.storageConfig.ipfs_bin || "ipfs"
|
2017-12-27 00:55:42 +00:00
|
|
|
});
|
|
|
|
|
2017-12-27 01:32:51 +00:00
|
|
|
this.embark.registerUploadCommand('ipfs', upload_ipfs.deploy.bind(upload_ipfs));
|
|
|
|
}
|
|
|
|
|
|
|
|
setServiceCheck() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
let storageConfig = this.storageConfig;
|
|
|
|
|
|
|
|
if (!storageConfig.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-31 10:18:25 +00:00
|
|
|
if (_.findWhere(this.storageConfig.dappConnection, {'provider': 'ipfs'}) === undefined && (storageConfig.upload.provider !== 'ipfs' || storageConfig.available_providers.indexOf("ipfs") < 0)) {
|
2017-12-27 01:32:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.events.on('check:backOnline:IPFS', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('IPFS node detected') + '..');
|
2017-12-27 01:32:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.events.on('check:wentOffline:IPFS', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('IPFS node is offline') + '..');
|
2017-12-27 01:32:51 +00:00
|
|
|
});
|
|
|
|
|
2018-06-01 15:58:11 +00:00
|
|
|
self.events.request("services:register", 'IPFS', function (cb) {
|
2018-05-25 07:13:57 +00:00
|
|
|
let url = (self.protocol || 'http') + '://' + self.host + ':' + self.port + '/api/v0/version';
|
2018-06-05 01:39:28 +00:00
|
|
|
self.logger.trace(`Checking IPFS version on ${url}...`);
|
2018-05-25 07:13:57 +00:00
|
|
|
if(self.protocol !== 'https'){
|
|
|
|
utils.httpGetJson(url, versionCb);
|
|
|
|
} else {
|
|
|
|
utils.httpsGetJson(url, versionCb);
|
|
|
|
}
|
|
|
|
function versionCb(err, body) {
|
2017-12-31 02:44:59 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.trace("Check IPFS version error: " + err);
|
2017-12-27 01:32:51 +00:00
|
|
|
return cb({name: "IPFS ", status: 'off'});
|
|
|
|
}
|
2017-12-31 02:44:59 +00:00
|
|
|
if (body.Version) {
|
2018-06-05 01:39:28 +00:00
|
|
|
self.logger.trace("Swarm available");
|
2017-12-31 02:44:59 +00:00
|
|
|
return cb({name: ("IPFS " + body.Version), status: 'on'});
|
|
|
|
}
|
2018-06-05 01:39:28 +00:00
|
|
|
self.logger.trace("Swarm available");
|
2017-12-31 02:44:59 +00:00
|
|
|
return cb({name: "IPFS ", status: 'on'});
|
2018-05-25 07:13:57 +00:00
|
|
|
}
|
2017-12-27 01:32:51 +00:00
|
|
|
});
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
addProviderToEmbarkJS() {
|
2018-01-10 16:15:32 +00:00
|
|
|
const self = this;
|
2017-12-29 21:11:45 +00:00
|
|
|
// TODO: make this a shouldAdd condition
|
2017-12-28 17:16:50 +00:00
|
|
|
if (this.storageConfig === {}) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 21:11:45 +00:00
|
|
|
|
2018-05-25 07:13:57 +00:00
|
|
|
if (this.storageConfig.available_providers.indexOf('ipfs') < 0 || _.findWhere(this.storageConfig.dappConnection, {'provider': 'ipfs'}) === undefined || this.storageConfig.enabled !== true) {
|
2017-12-28 17:16:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-10 16:15:32 +00:00
|
|
|
self.events.request("version:get:ipfs-api", function(ipfsApiVersion) {
|
|
|
|
let currentIpfsApiVersion = require('../../../package.json').dependencies["ipfs-api"];
|
|
|
|
if (ipfsApiVersion !== currentIpfsApiVersion) {
|
|
|
|
self.events.request("version:getPackageLocation", "ipfs-api", ipfsApiVersion, function(err, location) {
|
2018-04-02 19:06:56 +00:00
|
|
|
self.embark.registerImportFile("ipfs-api", fs.dappPath(location));
|
2018-01-10 16:15:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-25 07:13:57 +00:00
|
|
|
self.events.request("version:get:p-iteration", function(pIterationVersion) {
|
|
|
|
let currentPIterationVersion = require('../../../package.json').dependencies["p-iteration"];
|
|
|
|
if (pIterationVersion !== currentPIterationVersion) {
|
|
|
|
self.events.request("version:getPackageLocation", "p-iteration", pIterationVersion, function(err, location) {
|
|
|
|
self.embark.registerImportFile("p-iteration", fs.dappPath(location));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-28 17:16:50 +00:00
|
|
|
let code = "";
|
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
|
|
|
code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);";
|
|
|
|
|
|
|
|
this.embark.addCodeToEmbarkJS(code);
|
|
|
|
}
|
2017-12-28 22:42:25 +00:00
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
addObjectToConsole() {
|
2018-05-30 06:34:36 +00:00
|
|
|
let ipfs = IpfsApi(this.host, this.port);
|
2018-05-30 21:22:12 +00:00
|
|
|
this.events.emit("runcode:register", "ipfs", ipfs);
|
2018-05-18 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = IPFS;
|