Disable fallback to IPFS gateway, it almost never has the resource

This commit is contained in:
Mitra Ardron 2019-06-21 20:42:31 +10:00
parent 9f68f08262
commit c8520c6c7c

View File

@ -48,6 +48,8 @@ const defaultoptions = {
//init: true, // Comment out for Y //init: true, // Comment out for Y
EXPERIMENTAL: { pubsub: true }, EXPERIMENTAL: { pubsub: true },
preload: { enabled: false }, preload: { enabled: false },
//Off by default, it never seems to have the content (routing issues) pass as an argument if want to use
//httpIPFSgateway: "https://ipfs.io",
}; };
class TransportIPFS extends Transport { class TransportIPFS extends Transport {
@ -60,10 +62,10 @@ class TransportIPFS extends Transport {
constructor(options) { constructor(options) {
super(options); super(options);
if (options.urlUrlstore) { [ "urlUrlstore", "httpIPFSgateway"].forEach(k => {
this.urlUrlstore = options.urlUrlstore; this[k] = options[k];
delete options.urlUrlstore; delete options[k];
} });
this.ipfs = undefined; // Undefined till start IPFS this.ipfs = undefined; // Undefined till start IPFS
this.options = options; // Dictionary of options this.options = options; // Dictionary of options
this.name = "IPFS"; // For console log etc this.name = "IPFS"; // For console log etc
@ -262,14 +264,14 @@ class TransportIPFS extends Transport {
throw new errors.CodingError(`TransportIPFS.ipfsFrom: Cant convert url ${url} into a path starting /ipfs/`); throw new errors.CodingError(`TransportIPFS.ipfsFrom: Cant convert url ${url} into a path starting /ipfs/`);
} }
static ipfsGatewayFrom(url) { ipfsGatewayFrom(url) {
/* /*
url: CID, Url, or a string url: CID, Url, or a string
returns: https://ipfs.io/ipfs/<cid> returns: https://ipfs.io/ipfs/<cid>
*/ */
url = this._stringFrom(url); // Convert CID or Url to a string hopefully containing /ipfs/ url = this._stringFrom(url); // Convert CID or Url to a string hopefully containing /ipfs/
if (url.indexOf('/ipfs/') > -1) { if (url.indexOf('/ipfs/') > -1) {
return "https://ipfs.io" + url.slice(url.indexOf('/ipfs/')); return this.httpIPFSgateway + url.slice(url.indexOf('/ipfs/'));
} }
throw new errors.CodingError(`TransportIPFS.ipfsGatewayFrom: Cant convert url ${url} into a path starting /ipfs/`); throw new errors.CodingError(`TransportIPFS.ipfsGatewayFrom: Cant convert url ${url} into a path starting /ipfs/`);
} }
@ -325,16 +327,21 @@ class TransportIPFS extends Transport {
// Success logged by Transports // Success logged by Transports
return buff; return buff;
} catch (err) { // TimeoutError or could be some other error from IPFS etc } catch (err) { // TimeoutError or could be some other error from IPFS etc
debug("Caught error '%s' fetching via IPFS, trying IPFS HTTP gateway", err.message); debug("Caught error '%s' fetching via IPFS", err.message);
try { if (!this.httpIPFSgateway) {
let ipfsurl = TransportIPFS.ipfsGatewayFrom(url); throw(err);
return await utils.p_timeout( } else {
httptools.p_GET(ipfsurl), // Returns a buffer try {
timeoutMS, "Timed out IPFS fetch of "+ipfsurl) debug("Trying IPFS HTTP gateway");
} catch (err) { let ipfsurl = this.ipfsGatewayFrom(url);
// Failure logged by Transports: return await utils.p_timeout(
//debug("Failed to retrieve from gateway: %s", err.message); httptools.p_GET(ipfsurl), // Returns a buffer
throw err; timeoutMS, "Timed out IPFS fetch of "+ipfsurl)
} catch (err) {
// Failure logged by Transports:
//debug("Failed to retrieve from gateway: %s", err.message);
throw err;
}
} }
} }
} }