embark-area-51/lib/modules/ipfs/index.js

161 lines
4.9 KiB
JavaScript
Raw Normal View History

const UploadIPFS = require('./upload.js');
const utils = require('../../utils/utils.js');
const fs = require('../../core/fs.js');
const IpfsApi = require('ipfs-api');
const _ = require('underscore');
2018-07-07 16:29:04 +00:00
const StorageProcessesLauncher = require('../../processes/storageProcesses/storageProcessesLauncher');
2017-12-27 00:55:42 +00:00
class IPFS {
constructor(embark, options) {
const self = this;
2017-12-27 00:55:42 +00:00
this.logger = embark.logger;
2017-12-27 01:32:51 +00:00
this.events = embark.events;
this.buildDir = options.buildDir;
this.storageConfig = embark.config.storageConfig;
2017-12-27 01:32:51 +00:00
this.embark = embark;
2018-07-07 16:29:04 +00:00
this.webServerConfig = embark.config.webServerConfig
2018-07-07 16:29:04 +00:00
this.blockchainConfig = embark.config.blockchainConfig
this.commandlineDeploy();
this.setServiceCheck();
this.addProviderToEmbarkJS();
this.addObjectToConsole();
this._checkService((err) => {
if (!err) {
return;
}
self.logger.info("IPFS node not found, attempting to start own node");
self.startProcess(() => {});
});
2017-12-27 01:32:51 +00:00
}
commandlineDeploy() {
let upload_ipfs = new UploadIPFS({
buildDir: this.buildDir || 'dist/',
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
});
this.events.setCommandHandler('storage:upload:ipfs', upload_ipfs.deploy.bind(upload_ipfs));
2017-12-27 01:32:51 +00:00
}
setServiceCheck() {
console.dir("setServiceCheck");
2017-12-27 01:32:51 +00:00
let self = this;
let storageConfig = this.storageConfig;
if (!storageConfig.enabled) {
return;
}
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
});
self.events.request("services:register", 'IPFS', function (cb) {
self._checkService((err, body) => {
2017-12-31 02:44:59 +00:00
if (err) {
self.logger.info("IPFS unavailable");
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) {
self.logger.info("IPFS available");
2017-12-31 02:44:59 +00:00
return cb({name: ("IPFS " + body.Version), status: 'on'});
}
self.logger.info("IPFS available");
2017-12-31 02:44:59 +00:00
return cb({name: "IPFS ", status: 'on'});
});
2017-12-27 01:32:51 +00:00
});
2017-12-27 00:55:42 +00:00
}
_getNodeUrl() {
if (this.storageConfig.upload.provider === 'ipfs') {
let { protocol, host, port } = this.storageConfig.upload.provider;
return (protocol || 'http') + '://' + host + ':' + port + '/api/v0/version';
}
for (let connection of this.storageConfig.dappConnection) {
if (connection.provider === 'ipfs') {
let { protocol, host, port } = connection;
return (protocol || 'http') + '://' + host + ':' + port + '/api/v0/version';
}
}
}
_checkService(cb) {
const self = this;
let url = this._getNodeUrl();
self.logger.info(`Checking IPFS version on ${url}...`);
if (self.protocol !== 'https'){
utils.httpGetJson(url, cb);
} else {
utils.httpsGetJson(url, cb);
}
}
addProviderToEmbarkJS() {
2018-01-10 16:15:32 +00:00
const self = this;
// TODO: make this a shouldAdd condition
2017-12-28 17:16:50 +00:00
if (this.storageConfig === {}) {
return;
}
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
});
}
});
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
addObjectToConsole() {
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
}
2018-07-07 16:29:04 +00:00
startProcess(callback) {
let self = this;
const storageProcessesLauncher = new StorageProcessesLauncher({
logger: self.logger,
events: self.events,
storageConfig: self.storageConfig,
webServerConfig: self.webServerConfig,
blockchainConfig: self.blockchainConfig
});
self.logger.trace(`Storage module: Launching ipfs process...`);
return storageProcessesLauncher.launchProcess('ipfs', (err) => {
if (err) {
return callback(err);
}
callback();
});
}
2017-12-27 00:55:42 +00:00
}
module.exports = IPFS;