Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #14 (add support for subdomain IPFS URLs) #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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`
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class IpfsGatewayTools {
return `${desiredGatewayPrefix}/ipns/${results.cid}${splitUrl[1]}`;
}

//case 4 - the subdomain https://cid.ipfs.<gateway-host>.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"
Expand Down
32 changes: 32 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`);
Expand Down Expand Up @@ -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`);
});
});