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,9 +327,13 @@ 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);
if (!this.httpIPFSgateway) {
throw(err);
} else {
try { try {
let ipfsurl = TransportIPFS.ipfsGatewayFrom(url); debug("Trying IPFS HTTP gateway");
let ipfsurl = this.ipfsGatewayFrom(url);
return await utils.p_timeout( return await utils.p_timeout(
httptools.p_GET(ipfsurl), // Returns a buffer httptools.p_GET(ipfsurl), // Returns a buffer
timeoutMS, "Timed out IPFS fetch of "+ipfsurl) timeoutMS, "Timed out IPFS fetch of "+ipfsurl)
@ -338,6 +344,7 @@ class TransportIPFS extends Transport {
} }
} }
} }
}
async p_rawstore(data) { async p_rawstore(data) {
/* /*