embark-area-51/lib/modules/ipfs/embarkjs/default.js

48 lines
1.3 KiB
JavaScript

import IpfsApi from 'ipfs-api';
let __embarkIPFS = {};
__embarkIPFS.setProvider = function (options) {
var self = this;
var promise = new Promise(function (resolve, reject) {
try {
if (options === undefined) {
self._config = options;
self._ipfsConnection = IpfsApi('localhost', '5001');
self._getUrl = "http://localhost:8080/ipfs/";
} else {
var ipfsOptions = {host: options.host || options.server, protocol: 'http'};
if (options.protocol) {
ipfsOptions.protocol = options.protocol;
}
if (options.port && options.port !== 'false') {
ipfsOptions.port = options.port;
}
self._ipfsConnection = IpfsApi(ipfsOptions);
self._getUrl = options.getUrl || "http://localhost:8080/ipfs/";
}
resolve(self);
} catch (err) {
console.error(err);
self._ipfsConnection = null;
reject(new Error('Failed to connect to IPFS'));
}
});
return promise;
};
__embarkIPFS.isAvailable = function () {
return new Promise((resolve) => {
if (!this._ipfsConnection) {
return resolve(false);
}
this._ipfsConnection.id()
.then((id) => {
resolve(Boolean(id));
})
.catch(() => {
resolve(false);
});
});
};