Skip to content

fs: accept uppercase UTF8 encoding in fast paths#63576

Closed
AliMahmoudDev wants to merge 1 commit into
nodejs:mainfrom
AliMahmoudDev:fix/fs-utf8-fast-path-case-insensitive
Closed

fs: accept uppercase UTF8 encoding in fast paths#63576
AliMahmoudDev wants to merge 1 commit into
nodejs:mainfrom
AliMahmoudDev:fix/fs-utf8-fast-path-case-insensitive

Conversation

@AliMahmoudDev
Copy link
Copy Markdown

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 by Buffer.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 in readFileSync and writeFileSync to also accept 'UTF8' and 'UTF-8'
  • Added test file test/parallel/test-fs-utf8-fast-path-encoding.js verifying all four encoding variants work correctly with both string and object form options

Testing

New test covers:

  • writeFileSync(path, data, encoding) — string form
  • readFileSync(path, encoding) — string form
  • readFileSync(path, { encoding }) — object form
  • writeFileSync(path, data, { encoding }) — object form

All four encoding variants ('utf8', 'utf-8', 'UTF8', 'UTF-8') are tested with Unicode characters (CJK, emoji) to verify correctness.

node test/parallel/test-fs-utf8-fast-path-encoding.js

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
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels May 26, 2026
Copy link
Copy Markdown
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs: utf8 fast paths don't accept all valid utf8 values

3 participants