-
Notifications
You must be signed in to change notification settings - Fork 304
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,25 @@ 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("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.
Could we avoid encoding the data-URL grammar in a regex here? Since the format has a well-defined comma delimiter, a small helper could find the first comma and inspect the semicolon-delimited metadata before it for the final
base64marker. That would be easier to follow and give us a natural place to handle the scheme and marker case-insensitively, percent-decode the payload, and return a clear error for malformeddata:input.As written, inputs such as
DATA:image/png;BASE64,SGVsbG8%3Dstill miss the match and are passed to the raw-base64 decoder. Would you be open to extracting something likeparseBase64DataUrl(input): string | undefined, leavingbase64ToBytesresponsible only for decoding the extracted payload?