Skip to content

fix(ts-sdk): decode base64 data URLs with media-type parameters - #247

Open
rohitsux wants to merge 1 commit into
superlinked:mainfrom
rohitsux:fix/data-url-media-type-params
Open

fix(ts-sdk): decode base64 data URLs with media-type parameters#247
rohitsux wants to merge 1 commit into
superlinked:mainfrom
rohitsux:fix/data-url-media-type-params

Conversation

@rohitsux

@rohitsux rohitsux commented Aug 31, 2026

Copy link
Copy Markdown

Problem

toImageBytes() in packages/sie_ts_sdk/src/images.ts detects base64 data URLs with the regex:

/^data:[^;]+;base64,(.+)$/

This requires the media type segment to contain no ;. Per RFC 2397, though, the media type may carry parameters (e.g. ;charset=utf-8) or be omitted entirely. Data URLs like:

  • data:image/svg+xml;charset=utf-8;base64,...
  • data:;base64,...

don't match this pattern and fall through to the plain base64 branch, which hands the entire data URL string (including the data:...;base64, prefix) to the base64 decoder. That decoder then either throws InvalidCharacterError under atob (browser) or silently produces corrupted bytes under Buffer.from (Node) — neither of which surfaces as a clear "unsupported input" error.

Fix

Match up to the ;base64, marker instead of requiring a ;-free segment:

/^data:[^,]*;base64,(.+)$/

Base64 payloads never contain a comma, so the capture group still correctly isolates just the payload. Behavior for all previously-matching data URLs is unchanged.

Tests

Added two regression tests to packages/sie_ts_sdk/tests/images.test.ts:

  • a data URL whose media type carries a parameter (image/svg+xml;charset=utf-8)
  • a data URL with an omitted media type (data:;base64,...)

Verified locally: fails-before (both new tests throw on the unpatched regex) / passes-after. tests/images.test.ts 17/17; full @superlinked/sie-sdk suite 484/484; biome check clean; tsc --noEmit (typecheck) clean.

Summary by CodeRabbit

  • Bug Fixes

    • Improved image decoding for base64 data URLs that include media type parameters or omit the media type.
    • Prevented valid data URLs from being incorrectly passed to the decoder as raw URL text.
  • Tests

    • Added coverage for parameterized and media-type-free data URLs.

toImageBytes()'s regex ^data:[^;]+;base64, requires a ;-free media
type, so valid RFC 2397 data URLs with a media-type parameter (e.g.
data:image/svg+xml;charset=utf-8;base64,...) or an omitted media type
(data:;base64,...) don't match and fall through, handing the entire
data URL to the base64 decoder. That decoder then throws
InvalidCharacterError under atob (browser) or silently corrupts bytes
under Buffer.from (Node).

Fix matches up to the ;base64, marker ([^,]*) instead. Base64 payloads
never contain a comma, so the payload is still captured correctly. No
change in behavior for existing inputs.

Added two regression tests covering a media type with a parameter and
an omitted media type.
@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 241c62a7-3c9b-4da5-b952-30bb73ac7553

📥 Commits

Reviewing files that changed from the base of the PR and between 9a7e8d3 and 9de9d6c.

📒 Files selected for processing (2)
  • packages/sie_ts_sdk/src/images.ts
  • packages/sie_ts_sdk/tests/images.test.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Changes

Image data URL decoding

Layer / File(s) Summary
Data URL detection and validation
packages/sie_ts_sdk/src/images.ts, packages/sie_ts_sdk/tests/images.test.ts
toImageBytes now recognizes base64 data URLs with media-type parameters or no media type. Tests verify that both formats decode to Uint8Array bytes representing “Hello”.

Merge Risk: ⚪ Minimal · up to 9de9d

This localized change correctly handles base64 data URLs with media-type parameters or omitted media types, with regression coverage and passing checks. No actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: decoding base64 data URLs that contain media-type parameters.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant