fix(tool): decode webfetch bodies using declared charset via iconv-lite - #35838
Open
C0d3N1nja97342 wants to merge 1 commit into
Open
fix(tool): decode webfetch bodies using declared charset via iconv-lite#35838C0d3N1nja97342 wants to merge 1 commit into
C0d3N1nja97342 wants to merge 1 commit into
Conversation
webfetch decoded every response as UTF-8 (`new TextDecoder().decode(body)`), so pages served as windows-1251 (common for Russian legal sites like garant.ru/consultant.ru) came back garbled. The Content-Type charset was already being read but discarded. Parse the charset from Content-Type and decode with it. Bun's TextDecoder only supports a handful of labels (utf-8, shift_jis, gb18030, big5, euc-kr) and throws on common legacy encodings like windows-1251, so use iconv-lite for the long tail; utf-8 stays on the fast native TextDecoder path. Unknown charset labels fall back to UTF-8 (previous behavior) rather than failing the fetch. Adds iconv-lite as a direct dependency. It was already present transitively (several versions resolved in the lockfile); this promotes it to a declared dep of the package that actually uses it. Fixes anomalyco#35752
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Author
|
Friendly ping - this PR has been open for ~3 weeks with all CI checks green. The fix is small (iconv-lite decode for non-UTF-8 webfetch responses) and addresses a real user-facing bug (#35752). Happy to address any feedback. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue for this PR
Closes #35752
Type of change
What does this PR do?
webfetchdecoded every response body as UTF-8 (new TextDecoder().decode(body)), ignoring thecharsetin theContent-Typeheader even though it was already being read. Pages served aswindows-1251(common for Russian legal sites such asbase.garant.ru) came back as replacement characters.This parses the charset from
Content-Typeand decodes with it. Because Bun'sTextDecoderonly supports a handful of labels (utf-8, shift_jis, gb18030, big5, euc-kr) and throws on common legacy encodings likewindows-1251, the non-UTF-8 path usesiconv-lite. utf-8 stays on the fast nativeTextDecoderpath; unknown/unsupported charset labels fall back to UTF-8 (the previous behavior) rather than failing the fetch.I understand why this works: the charset was being read but discarded (
contentType.split(";")[0]), so the fix is to keep it and feed it to a decoder that actually supports it.iconv-liteis needed specifically because Bun's built-inTextDecodercannot decodewindows-1251— I verifiednew TextDecoder("windows-1251")throwsERR_ENCODING_NOT_SUPPORTEDinoven/bun:1.3.14, whileiconv.decode(buf, "win1251")decodes correctly.Adds
iconv-liteas a direct dependency ofpackages/opencode. It was already present transitively (several versions resolve in the lockfile); this promotes it to a declared dep of the package that uses it.How did you verify your code works?
bun test test/tool/webfetch.test.tsinoven/bun:1.3.14— 6/6 pass, including two new cases: decodeswindows-1251bytes (Привет) correctly via iconv-lite, and falls back to UTF-8 for an unsupported charset label.bunx tsgo --noEmit(the package'stypecheckscript) — 0 errors.bun installupdatesbun.lockfor the new dep.Screenshots / recordings
N/A — no UI change.
Checklist