-
Notifications
You must be signed in to change notification settings - Fork 1
Use raw format to GET data from IPFS gateway #24
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Copyright 2026 Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: AGPL-3.0-only | ||
|
|
||
| import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; | ||
| import { fetchFromIpfs, fetchCarFromIpfs } from "@dotli/content/ipfs"; | ||
|
|
||
| const CID = "bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy"; | ||
| const GATEWAY = "https://gw.example"; | ||
|
|
||
| let fetchMock: ReturnType<typeof vi.fn>; | ||
|
|
||
| beforeEach(() => { | ||
| fetchMock = vi.fn(); | ||
| vi.stubGlobal("fetch", fetchMock); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("fetchFromIpfs", () => { | ||
| // Regression guard: a bare GET lets a gateway content-negotiate and mutate | ||
| // the body (e.g. Cloudflare rewriting text/html), so the bytes no longer | ||
| // hash to the CID. We must request the raw block as a binary type. | ||
| it("requests the raw block with ?format=raw and the raw Accept header", async () => { | ||
| fetchMock.mockResolvedValue( | ||
| new Response(new Uint8Array([1, 2, 3]), { status: 200 }), | ||
| ); | ||
|
|
||
| await fetchFromIpfs(CID, GATEWAY); | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledWith( | ||
| `${GATEWAY}/ipfs/${CID}?format=raw`, | ||
| expect.objectContaining({ | ||
| headers: { Accept: "application/vnd.ipld.raw" }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("returns the response bytes and content type", async () => { | ||
| fetchMock.mockResolvedValue( | ||
| new Response(new Uint8Array([0xde, 0xad, 0xbe, 0xef]), { | ||
| status: 200, | ||
| headers: { "content-type": "application/vnd.ipld.raw" }, | ||
| }), | ||
| ); | ||
|
|
||
| const { data, contentType } = await fetchFromIpfs(CID, GATEWAY); | ||
|
|
||
| expect(Array.from(data)).toEqual([0xde, 0xad, 0xbe, 0xef]); | ||
| expect(contentType).toBe("application/vnd.ipld.raw"); | ||
| }); | ||
|
|
||
| it("throws on a non-ok response", async () => { | ||
| fetchMock.mockResolvedValue(new Response(null, { status: 502 })); | ||
|
|
||
| await expect(fetchFromIpfs(CID, GATEWAY)).rejects.toThrow(/502/); | ||
| }); | ||
| }); | ||
|
|
||
| describe("fetchCarFromIpfs", () => { | ||
| // CAR is also a binary type, so it is likewise immune to gateway text/html | ||
| // rewriting — this asserts the request stays binary. | ||
| it("requests the CAR archive with ?format=car and the CAR Accept header", async () => { | ||
| fetchMock.mockResolvedValue( | ||
| new Response(new Uint8Array([1, 2, 3]), { status: 200 }), | ||
| ); | ||
|
|
||
| await fetchCarFromIpfs(CID, GATEWAY); | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledWith( | ||
| `${GATEWAY}/ipfs/${CID}?format=car`, | ||
| expect.objectContaining({ | ||
| headers: { Accept: "application/vnd.ipld.car" }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("returns the response bytes", async () => { | ||
| fetchMock.mockResolvedValue( | ||
| new Response(new Uint8Array([0xca, 0xfe]), { status: 200 }), | ||
| ); | ||
|
|
||
| const data = await fetchCarFromIpfs(CID, GATEWAY); | ||
|
|
||
| expect(Array.from(data)).toEqual([0xca, 0xfe]); | ||
| }); | ||
|
|
||
| it("throws on a non-ok response", async () => { | ||
| fetchMock.mockResolvedValue(new Response(null, { status: 502 })); | ||
|
|
||
| await expect(fetchCarFromIpfs(CID, GATEWAY)).rejects.toThrow(/502/); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.