Repository navigation
Conversation
FanWu-ai
marked this pull request as ready for review
October 12, 2026 21:51
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.
Problem
head()decodes each file-read buffer as a complete UTF-8 string. When a multibyte character crosses the default 64 KiB boundary, it is replaced with invalid-character markers even though the source file contains valid UTF-8. For example, a first line containing 65,535 ASCII characters followed byéis returned with��at the end.Change
Use Node's built-in
StringDecoderfor each file's incremental reads and flush it when reading finishes. This preserves characters split across buffers and retains replacement behavior for incomplete UTF-8 at EOF. The decoder is local to each file, and reading still stops once enough lines have been read. No dependency or API changes.Verification
Node.js 24.19.0 on Linux x86_64:
npm test, including posttest ESLint: 656 passed, 6 pre-existing explicit skips.npm run test-with-coverage: 656 passed, 6 skipped; 95.63% statement and 96.45% line coverage overall;src/head.jshas 100% statement, branch, function and line coverage.git diff --checkpassed. Regenerating documentation leaves README unchanged.head, covering eight buffer sizes, UTF-8, combining marks, CRLF, invalid bytes, EOF and line counts. An early-stop control verifies only one buffer is read when the requested first line is already available.The six reported skips are existing
test/cmd.jscases: five asynchronous-command cases and the missing-execPath case. Other operating systems and Node versions have not been run locally.AI assistance: OpenAI Codex assisted the investigation, implementation and tests.