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

122 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-12-27 00:55:42 +00:00
let UploadIPFS = require('./upload.js');
2017-12-27 01:32:51 +00:00
let utils = require('../../utils/utils.js');
2017-12-28 17:16:50 +00:00
let fs = require('../../core/fs.js');
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;
this.storageConfig = options.storageConfig;
this.host = options.host || this.storageConfig.host;
this.port = options.port || this.storageConfig.port;
this.addCheck = options.addCheck;
this.embark = embark;
2017-12-27 00:55:42 +00:00
2017-12-27 01:32:51 +00:00
this.commandlineDeploy();
this.setServiceCheck();
2017-12-28 17:16:50 +00:00
this.addIPFSToEmbarkJS();
2017-12-28 22:42:25 +00:00
this.addSetProvider();
2017-12-27 01:32:51 +00:00
}
commandlineDeploy() {
let upload_ipfs = new UploadIPFS({
buildDir: this.buildDir || 'dist/',
storageConfig: this.storageConfig,
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;
}
if (storageConfig.provider !== 'ipfs' && storageConfig.available_providers.indexOf("ipfs") < 0) {
return;
}
self.events.on('check:backOnline:IPFS', function () {
self.logger.info('IPFS node detected..');
});
self.events.on('check:wentOffline:IPFS', function () {
self.logger.info('IPFS node is offline..');
});
let server = 'http://' + this.host + ':' + this.port;
self.logger.info(server);
this.addCheck('IPFS', function (cb) {
utils.checkIsAvailable(server, function (available) {
if (available) {
//Ideally this method should be in an IPFS API JSONRPC wrapper
//The URL should also be flexible to accept non-default IPFS url
self.logger.trace("Checking IPFS version...");
utils.httpGet(server + '/api/v0/version', function (err, body) {
if (err) {
self.logger.trace("Check IPFS version error: " + err);
return cb({name: "IPFS ", status: 'off'});
}
try {
let parsed = JSON.parse(body);
if (parsed.Version) {
return cb({name: ("IPFS " + parsed.Version), status: 'on'});
}
else {
return cb({name: "IPFS ", status: 'on'});
}
}
catch (e) {
return cb({name: "IPFS ", status: 'off'});
}
});
}
else {
return cb({name: "IPFS ", status: 'off'});
}
});
});
2017-12-27 00:55:42 +00:00
}
2017-12-28 17:16:50 +00:00
addIPFSToEmbarkJS() {
// 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) && (this.storageConfig.provider !== 'ipfs' || this.storageConfig.enabled !== true)) {
2017-12-28 17:16:50 +00:00
return;
}
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
addSetProvider() {
let config = JSON.stringify({
server: this.storageConfig.host,
port: this.storageConfig.port,
getUrl: this.storageConfig.getUrl
});
let code = "\nEmbarkJS.Storage.setProvider('ipfs'," + config + ");";
let shouldInit = (storageConfig) => {
return (storageConfig.provider === 'ipfs' && storageConfig.enabled === true);
};
this.embark.addProviderInit('storage', code, shouldInit);
}
2017-12-27 00:55:42 +00:00
}
module.exports = IPFS;