From 242013b252adb4aa12e9f27222323cd1751de55c Mon Sep 17 00:00:00 2001 From: Hamid Reza Ghavami Date: Tue, 7 Jul 2026 20:27:41 +0300 Subject: [PATCH] fs: accept all valid utf8 values in fast paths Signed-off-by: Hamid Reza Ghavami --- lib/fs.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 4436fa2df6e6..aea33cfac7c5 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -57,6 +57,13 @@ const { O_SYMLINK, } = constants; +function isUtf8Encoding(encoding) { + return encoding === 'utf8' || + encoding === 'utf-8' || + encoding === 'UTF8' || + encoding === 'UTF-8'; +} + const pathModule = require('path'); const { isArrayBufferView } = require('internal/util/types'); @@ -611,8 +618,7 @@ function readFileSync(path, options) { validateReadFileBufferOptions(options); const hasUserBuffer = options.buffer !== undefined; - if ((options.encoding === 'utf8' || options.encoding === 'utf-8') && - !hasUserBuffer) { + if (isUtf8Encoding(options.encoding) && !hasUserBuffer) { if (isInt32(path)) { // V8 does not report -0 as an int32, so it would reach the binding as a // path instead of a file descriptor. @@ -3015,7 +3021,7 @@ function writeFileSync(path, data, options) { const flag = options.flag || 'w'; // C++ fast path for string data and UTF8 encoding - if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) { + if (typeof data === 'string' && isUtf8Encoding(options.encoding)) { if (isInt32(path)) { // V8 does not report -0 as an int32, so it would reach the binding as a // path instead of a file descriptor. @@ -3320,7 +3326,7 @@ if (isWindows) { } function encodeRealpathResult(result, options) { - if (!options || !options.encoding || options.encoding === 'utf8') + if (!options || !options.encoding || isUtf8Encoding(options.encoding)) return result; const asBuffer = Buffer.from(result); if (options.encoding === 'buffer') {