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..0860ba5 100644 --- a/src/index.js +++ b/src/index.js @@ -53,6 +53,13 @@ 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; + if (pathname == "/") 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" 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`); + }); });