Skip to content

fs: accept all valid utf8 values in fast paths#64341

Open
hamidrezaghavami wants to merge 1 commit into
nodejs:mainfrom
hamidrezaghavami:fix-fs-utf8-fastpath
Open

fs: accept all valid utf8 values in fast paths#64341
hamidrezaghavami wants to merge 1 commit into
nodejs:mainfrom
hamidrezaghavami:fix-fs-utf8-fastpath

Conversation

@hamidrezaghavami

Copy link
Copy Markdown
Contributor

Fixes: #49888

This PR introduces a lightweight isUtf8Encoding helper to ensure fs.readFileSync, fs.writeFileSync, and encodeRealpathResult use the fast C++ path for all valid variations of the utf8 encoding string (utf8, utf-8, UTF8, UTF-8).

@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. util Issues and PRs related to the built-in util module. labels Jul 7, 2026
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.23%. Comparing base (f4c85d5) to head (1d3f916).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64341      +/-   ##
==========================================
- Coverage   90.24%   90.23%   -0.02%     
==========================================
  Files         741      741              
  Lines      240976   240982       +6     
  Branches    45408    45401       -7     
==========================================
- Hits       217477   217448      -29     
- Misses      15070    15120      +50     
+ Partials     8429     8414      -15     
Files with missing lines Coverage Δ
lib/fs.js 98.36% <100.00%> (+<0.01%) ⬆️

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@anonrig

anonrig commented Jul 7, 2026

Copy link
Copy Markdown
Member

I don't think we should land this. The more checks we have, the more performance impact it would create. Do you have any examples out there that is impacted by this? Does people really pass UTF8? If not, let's close this.

@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

Hi @anonrig thanks for the review!

I completely understand the concern regarding the performance impact on the fast path. I picked this up to resolve issue #49888, as there seemed to be some inconsistency in how variations like utf-8 (which is common in HTTP headers/HTML meta tags) bypass the C++ fast path and fall back to the slower route.

That said, if the consensus is that the branch/check overhead for the 99% of users passing standard utf8 outweighs the benefit for the edge cases, I am perfectly happy to close this PR. Let me know what you think is best for the module!

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. util Issues and PRs related to the built-in util module.

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