Skip to content

fix(tool): decode webfetch bodies using declared charset via iconv-lite - #35838

Open
C0d3N1nja97342 wants to merge 1 commit into
anomalyco:devfrom
C0d3N1nja97342:fix/webfetch-charset-dev
Open

fix(tool): decode webfetch bodies using declared charset via iconv-lite#35838
C0d3N1nja97342 wants to merge 1 commit into
anomalyco:devfrom
C0d3N1nja97342:fix/webfetch-charset-dev

Conversation

@C0d3N1nja97342

@C0d3N1nja97342 C0d3N1nja97342 commented Jul 8, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #35752

Type of change

  • Bug fix

What does this PR do?

webfetch decoded every response body as UTF-8 (new TextDecoder().decode(body)), ignoring the charset in the Content-Type header even though it was already being read. Pages served as windows-1251 (common for Russian legal sites such as base.garant.ru) came back as replacement characters.

This parses the charset from Content-Type and decodes with it. Because 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, the non-UTF-8 path uses iconv-lite. utf-8 stays on the fast native TextDecoder path; 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-lite is needed specifically because Bun's built-in TextDecoder cannot decode windows-1251 — I verified new TextDecoder("windows-1251") throws ERR_ENCODING_NOT_SUPPORTED in oven/bun:1.3.14, while iconv.decode(buf, "win1251") decodes correctly.

Adds iconv-lite as a direct dependency of packages/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.ts in oven/bun:1.3.14 — 6/6 pass, including two new cases: decodes windows-1251 bytes (Привет) correctly via iconv-lite, and falls back to UTF-8 for an unsupported charset label.
  • bunx tsgo --noEmit (the package's typecheck script) — 0 errors.
  • bun install updates bun.lock for the new dep.

Screenshots / recordings

N/A — no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Jul 8, 2026
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@C0d3N1nja97342

Copy link
Copy Markdown
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.

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.

webfetch: Windows-1251 encoding not handled (garbled output for garant.ru, consultant.ru)

1 participant