From d669dface87bd11da239b968dc6fdabd90df1b9f Mon Sep 17 00:00:00 2001 From: MylesMor Date: Fri, 11 Nov 2022 01:32:42 +0000 Subject: [PATCH 1/3] Fix for issue #14 Add support for `https://CID.ipfs.gateway.com` style URLs --- README.md | 4 ++++ src/index.js | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index b67c993..72737c9 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ The IPFS gateway toolkit currently contains the following functions: - `https://example-gateway.com/ipfs/CID` - `https://example-gateway.com/ipfs/CID/exampleFile.json` - `https://example-gateway.com/ipns/CID` + - `https://CID.ipfs.example-gateway.com` + - `https://CID.ipfs.example-gateway.com/exampleFile.json` #### Response @@ -75,6 +77,8 @@ The IPFS gateway toolkit currently contains the following functions: - `https://example-gateway.com/ipfs/CID` - `https://example-gateway.com/ipfs/CID/exampleFile.json` - `https://example-gateway.com/ipns/CID` + - `https://CID.ipfs.example-gateway.com` + - `https://CID.ipfs.example-gateway.com/exampleFile.json` - `desiredGatewayPrefix` - The desired gateway you want to convert your source URL to. A few examples of this would be: - `https://mygateway.mypinata.cloud` - `https://ipfs.io` diff --git a/src/index.js b/src/index.js index 6b52064..c5d3eaf 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const isIPFS = require("is-ipfs"); +const http = require('url'); class IpfsGatewayTools { constructor() {} @@ -53,6 +54,12 @@ class IpfsGatewayTools { return `${desiredGatewayPrefix}/ipns/${results.cid}${splitUrl[1]}`; } + //case 4 - the subdomain https://cid.ipfs..tld/path/to/resource path (eg. https://cid.ipfs.dweb.link) + if (sourceUrl.includes(`https://${results.cid}.ipfs.`)) { + let pathname = new URL(sourceUrl).pathname; + return `${desiredGatewayPrefix}/ipfs/${results.cid}${pathname}` + } + //this is the fallback if no supported patterns are provided throw new Error( "unsupported URL pattern, please submit a github issue with the URL utilized" From 0d65c89d6ca6910d80a65d5c0aac872adc549b8e Mon Sep 17 00:00:00 2001 From: MylesMor Date: Fri, 11 Nov 2022 01:59:19 +0000 Subject: [PATCH 2/3] Add required tests --- tests/test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test.js b/tests/test.js index 9c2c7b8..34473ea 100644 --- a/tests/test.js +++ b/tests/test.js @@ -24,6 +24,22 @@ describe("containsCID function testing", () => { expectedResult ); + expect( + ipfsGatewayTools.containsCID( + `https://ipfs.io/ipfs/${CIDToLookFor}?filename=IMG_20210917_135500_HDR` + ) + ).toEqual(expectedResult); + }); + test("returns true if base32 CID", () => { + const CIDToLookFor = "bafybeigeweglu3c2hgyuobwuhugxzp3xgea5fdtht4yg4e4mcdfz3hx7hy"; + const expectedResult = { + containsCid: true, + cid: CIDToLookFor, + }; + expect(ipfsGatewayTools.containsCID(`ipfs://${CIDToLookFor}`)).toEqual( + expectedResult + ); + expect( ipfsGatewayTools.containsCID( `https://ipfs.io/ipfs/${CIDToLookFor}?filename=IMG_20210917_135500_HDR` @@ -35,6 +51,8 @@ describe("containsCID function testing", () => { describe("convertToDesiredGateway function testing", () => { const desiredGatewayPrefix = "https://gateway.pinata.cloud"; const theCID = "QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o"; + + const base32CID = "bafybeigeweglu3c2hgyuobwuhugxzp3xgea5fdtht4yg4e4mcdfz3hx7hy"; test("throws if url does not contain a CID", () => { expect(() => { ipfsGatewayTools.convertToDesiredGateway(`ipfs://testing`); @@ -77,4 +95,18 @@ describe("convertToDesiredGateway function testing", () => { ); expect(results).toEqual(`${desiredGatewayPrefix}/ipfs/${theCID}/test.json`); }); + test("correctly returns subdomain style path", () => { + const results = ipfsGatewayTools.convertToDesiredGateway( + `https://${base32CID}.ipfs.dweb.link`, + desiredGatewayPrefix + ); + expect(results).toEqual(`${desiredGatewayPrefix}/ipfs/${base32CID}`); + }); + test("correctly returns subdomain style path with directory path", () => { + const results = ipfsGatewayTools.convertToDesiredGateway( + `https://${base32CID}.ipfs.dweb.link/test.json`, + desiredGatewayPrefix + ); + expect(results).toEqual(`${desiredGatewayPrefix}/ipfs/${base32CID}/test.json`); + }); }); From ef2870f6e2a13772c9f8e251b6113594fbf00064 Mon Sep 17 00:00:00 2001 From: MylesMor Date: Fri, 11 Nov 2022 01:59:30 +0000 Subject: [PATCH 3/3] Fix minor bug with new case --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index c5d3eaf..0860ba5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ const isIPFS = require("is-ipfs"); -const http = require('url'); class IpfsGatewayTools { constructor() {} @@ -57,6 +56,7 @@ class IpfsGatewayTools { //case 4 - the subdomain https://cid.ipfs..tld/path/to/resource path (eg. https://cid.ipfs.dweb.link) if (sourceUrl.includes(`https://${results.cid}.ipfs.`)) { let pathname = new URL(sourceUrl).pathname; + if (pathname == "/") pathname = ""; return `${desiredGatewayPrefix}/ipfs/${results.cid}${pathname}` }