fix: read content encoding hints from <meta> tags#3780
Merged
Conversation
janbuchar
reviewed
Jun 30, 2026
| */ | ||
| export function extractCharsetFromHtmlBytes(bytes: Buffer): string | undefined { | ||
| // latin1 preserves byte values for ASCII-compatible encodings, making the meta tags readable | ||
| const prescan = bytes.subarray(0, 1024).toString('latin1'); |
Contributor
There was a problem hiding this comment.
Are you sure that 1024 bytes will do it?
Member
Author
There was a problem hiding this comment.
See 13.2.3.2 Determining the character encoding paragraph 5:
Optionally, prescan the byte stream to determine its encoding, with the end condition being when the user agent decides that scanning further bytes would not be efficient. User agents are encouraged to only prescan the first 1024 bytes. User agents may decide that scanning any bytes is not efficient, in which case these substeps are entirely skipped.
Contributor
There was a problem hiding this comment.
Okay, lawyered. Approving.
janbuchar
approved these changes
Jun 30, 2026
1 task
barjin
added a commit
that referenced
this pull request
Jul 20, 2026
Utilizes the byte-stream prescan (https://html.spec.whatwg.org/multipage/parsing.html#prescan-a-byte-stream-to-determine-its-encoding) to determine the HTML document encoding, if not specified in HTTP headers (or explicitly forced by user). Closes #2317
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.
Utilizes the byte-stream prescan (https://html.spec.whatwg.org/multipage/parsing.html#prescan-a-byte-stream-to-determine-its-encoding) to determine the HTML document encoding, if not specified in HTTP headers (or explicitly forced by user).
Closes #2317