fs: accept uppercase UTF8 encoding in fast paths#63576
Closed
AliMahmoudDev wants to merge 1 commit into
Closed
Conversation
The readFileSync and writeFileSync UTF8 fast paths only accepted lowercase 'utf8' and 'utf-8' encodings. Accept 'UTF8' and 'UTF-8' as well, since they are valid encoding aliases per Buffer.isEncoding(). Refs: nodejs#49888
addaleax
reviewed
May 26, 2026
Member
addaleax
left a comment
There was a problem hiding this comment.
It might be fine to leave this as-is, since utf-8 and utf8 are the most commonly used labels and fast paths inherently focus on common cases, and the extra comparisons aren't completely free either
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.
Summary
Fixes #49888
The and UTF8 fast paths (introduced in #48658 and #49884) only accepted lowercase encoding strings
'utf8'and'utf-8'. The uppercase variants'UTF8'and'UTF-8'are valid encodings (accepted byBuffer.isEncoding()) but were not routed to the fast path, causing them to fall through to the slower generic code path.Changes
lib/fs.js: Extended the fast path checks inreadFileSyncandwriteFileSyncto also accept'UTF8'and'UTF-8'test/parallel/test-fs-utf8-fast-path-encoding.jsverifying all four encoding variants work correctly with both string and object form optionsTesting
New test covers:
writeFileSync(path, data, encoding)— string formreadFileSync(path, encoding)— string formreadFileSync(path, { encoding })— object formwriteFileSync(path, data, { encoding })— object formAll four encoding variants (
'utf8','utf-8','UTF8','UTF-8') are tested with Unicode characters (CJK, emoji) to verify correctness.