-
Notifications
You must be signed in to change notification settings - Fork 3
fix: save binary fetch responses to a file instead of corrupting them as text #62
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 all commits
Commits
Show all changes
3 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
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,123 @@ | ||
| import { describe, test, expect, vi, afterEach } from "vitest"; | ||
| import { readFileSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { NWCClient } from "@getalby/sdk"; | ||
| import { fetch402 } from "../tools/lightning/fetch.js"; | ||
|
|
||
| // A minimal WAV header: contains bytes (0xAC, 0x88, ...) that are invalid | ||
| // UTF-8, so a text decode would corrupt them into U+FFFD. | ||
| const WAV_BYTES = new Uint8Array([ | ||
| 0x52, 0x49, 0x46, 0x46, 0x24, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, | ||
| 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, | ||
| 0x44, 0xac, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, | ||
| ]); | ||
|
|
||
| const URL = "https://example.com/protected"; | ||
|
|
||
| const client = {} as unknown as NWCClient; | ||
| const savedFiles: string[] = []; | ||
|
|
||
| function stubResponse( | ||
| body: BodyInit, | ||
| headers: Record<string, string> = {}, | ||
| ): void { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn().mockResolvedValue(new Response(body, { status: 200, headers })), | ||
| ); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| for (const file of savedFiles.splice(0)) { | ||
| rmSync(file, { force: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe("fetch binary responses", () => { | ||
| test("saves a binary body to a temp file instead of returning mangled text", async () => { | ||
| stubResponse(WAV_BYTES, { "content-type": "audio/wav" }); | ||
|
|
||
| const result = await fetch402(client, { url: URL }); | ||
| savedFiles.push(result.outputPath!); | ||
|
|
||
| expect(result.content).toBeUndefined(); | ||
| expect(result.outputPath).toContain(tmpdir()); | ||
| expect(result.outputPath).toMatch(/\.wav$/); | ||
| expect(result.contentType).toBe("audio/wav"); | ||
| expect(result.sizeBytes).toBe(WAV_BYTES.length); | ||
| // The saved bytes are the original response, byte for byte. | ||
| expect(new Uint8Array(readFileSync(result.outputPath!))).toEqual(WAV_BYTES); | ||
| }); | ||
|
|
||
| test("saves an unlabeled binary body with a .bin extension", async () => { | ||
| stubResponse(WAV_BYTES); | ||
|
|
||
| const result = await fetch402(client, { url: URL }); | ||
| savedFiles.push(result.outputPath!); | ||
|
|
||
| expect(result.outputPath).toMatch(/\.bin$/); | ||
| expect(new Uint8Array(readFileSync(result.outputPath!))).toEqual(WAV_BYTES); | ||
| }); | ||
|
|
||
| test("returns a declared-text body inline even when not valid UTF-8", async () => { | ||
| stubResponse(new Uint8Array([0x68, 0x69, 0xff]), { | ||
| "content-type": "text/plain; charset=utf-8", | ||
| }); | ||
|
|
||
| const result = await fetch402(client, { url: URL }); | ||
|
|
||
| expect(result.outputPath).toBeUndefined(); | ||
| expect(result.content).toBe("hi�"); | ||
| }); | ||
|
|
||
| test("returns an unlabeled body inline when it decodes cleanly as UTF-8", async () => { | ||
| // Bytes without a content-type header: a string body would get an | ||
| // automatic text/plain and never reach the sniffing path. | ||
| stubResponse(new TextEncoder().encode('{"ok":true}')); | ||
|
|
||
| const result = await fetch402(client, { url: URL }); | ||
|
|
||
| expect(result.outputPath).toBeUndefined(); | ||
| expect(result.content).toBe('{"ok":true}'); | ||
| }); | ||
|
|
||
| test("--output saves any body, text included, to the given path", async () => { | ||
| stubResponse('{"ok":true}', { "content-type": "application/json" }); | ||
| const outputPath = join(tmpdir(), `fetch-binary-test-${process.pid}.json`); | ||
|
|
||
| const result = await fetch402(client, { url: URL, outputPath }); | ||
| savedFiles.push(outputPath); | ||
|
|
||
| expect(result.content).toBeUndefined(); | ||
| expect(result.outputPath).toBe(outputPath); | ||
| expect(readFileSync(outputPath, "utf-8")).toBe('{"ok":true}'); | ||
| }); | ||
|
|
||
| test("falls back to base64 inline when a binary body cannot be saved", async () => { | ||
| // A failed write must not throw away the (possibly paid) response. | ||
| stubResponse(WAV_BYTES, { "content-type": "audio/wav" }); | ||
| const outputPath = join(tmpdir(), "no-such-dir", "out.wav"); | ||
|
|
||
| const result = await fetch402(client, { url: URL, outputPath }); | ||
|
|
||
| expect(result.outputPath).toBeUndefined(); | ||
| expect(result.writeError).toContain("ENOENT"); | ||
| expect(result.contentType).toBe("audio/wav"); | ||
| expect( | ||
| new Uint8Array(Buffer.from(result.contentBase64!, "base64")), | ||
| ).toEqual(WAV_BYTES); | ||
| }); | ||
|
|
||
| test("falls back to inline text when a text body cannot be saved", async () => { | ||
| stubResponse('{"ok":true}', { "content-type": "application/json" }); | ||
| const outputPath = join(tmpdir(), "no-such-dir", "out.json"); | ||
|
|
||
| const result = await fetch402(client, { url: URL, outputPath }); | ||
|
|
||
| expect(result.outputPath).toBeUndefined(); | ||
| expect(result.writeError).toContain("ENOENT"); | ||
| expect(result.content).toBe('{"ok":true}'); | ||
| }); | ||
| }); |
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
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.