-
Notifications
You must be signed in to change notification settings - Fork 305
fix(ts-sdk): decode base64 data URLs with media-type parameters #247
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,63 @@ describe("toImageBytes", () => { | |
| expect(new TextDecoder().decode(result)).toBe("test"); | ||
| }); | ||
|
|
||
| it("decodes a data URL whose media type carries a parameter", async () => { | ||
| // Valid per RFC 2397: the media type may be followed by ";param=value" | ||
| // (e.g. charset) before ";base64,". "Hello" base64-encoded. | ||
| const dataUrl = "data:image/svg+xml;charset=utf-8;base64,SGVsbG8="; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two regression cases are a good start. If we switch to a small parser, could we also cover an uppercase scheme/ |
||
| const result = await toImageBytes(dataUrl); | ||
|
|
||
| expect(result).toBeInstanceOf(Uint8Array); | ||
| expect(new TextDecoder().decode(result)).toBe("Hello"); | ||
| }); | ||
|
|
||
| it("decodes a data URL with an omitted media type", async () => { | ||
| // RFC 2397 permits an empty media type (defaults to text/plain). | ||
| const dataUrl = "data:;base64,SGVsbG8="; | ||
| const result = await toImageBytes(dataUrl); | ||
|
|
||
| expect(result).toBeInstanceOf(Uint8Array); | ||
| expect(new TextDecoder().decode(result)).toBe("Hello"); | ||
| }); | ||
|
|
||
| it("decodes a data URL with an uppercase scheme and BASE64 marker", async () => { | ||
| // RFC 2397: the scheme and the "base64" marker are case-insensitive. | ||
| const dataUrl = "DATA:image/png;BASE64,SGVsbG8="; | ||
| const result = await toImageBytes(dataUrl); | ||
|
|
||
| expect(result).toBeInstanceOf(Uint8Array); | ||
| expect(new TextDecoder().decode(result)).toBe("Hello"); | ||
| }); | ||
|
|
||
| it("percent-decodes the payload before base64 decoding", async () => { | ||
| // The "=" padding can arrive percent-escaped as "%3D". | ||
| const dataUrl = "data:image/png;base64,SGVsbG8%3D"; | ||
| const result = await toImageBytes(dataUrl); | ||
|
|
||
| expect(result).toBeInstanceOf(Uint8Array); | ||
| expect(new TextDecoder().decode(result)).toBe("Hello"); | ||
| }); | ||
|
|
||
| it("returns an empty array for an empty base64 data URL payload", async () => { | ||
| const dataUrl = "data:;base64,"; | ||
| const result = await toImageBytes(dataUrl); | ||
|
|
||
| expect(result).toBeInstanceOf(Uint8Array); | ||
| expect(result.length).toBe(0); | ||
| }); | ||
|
|
||
| it("throws a clear error for a data URL that is not base64-encoded", async () => { | ||
| // A non-base64 data URL cannot yield image bytes; it must fail loudly | ||
| // instead of being handed to the base64 decoder. | ||
| await expect(toImageBytes("data:text/plain,Hello")).rejects.toThrow( | ||
| "only base64-encoded payloads are supported", | ||
| ); | ||
| }); | ||
|
|
||
| it("throws a clear error for a data URL missing the payload delimiter", async () => { | ||
| await expect(toImageBytes("data:image/png;base64")).rejects.toThrow("missing ',' delimiter"); | ||
| }); | ||
|
|
||
| it("throws for unsupported input type", async () => { | ||
| await expect(toImageBytes(123 as unknown as Uint8Array)).rejects.toThrow( | ||
| "Unsupported image input type", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One behavior suggestion: data:;base64, now returns an empty Uint8Array and succeeds. That zero-byte "image" then goes out over the wire as { data: , format: "jpeg" } and fails somewhere in the server, far from the actual mistake. Empty payload should throw too.