Skip to content

Commit bfaade2

Browse files
fs: accept all valid utf8 values in fast paths
1 parent 54166e5 commit bfaade2

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

lib/fs.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ const {
5959
O_SYMLINK,
6060
} = constants;
6161

62+
function isUtf8Encoding(encoding) {
63+
return encoding === 'utf8' ||
64+
encoding === 'utf-8' ||
65+
encoding === 'UTF8' ||
66+
encoding === 'UTF-8';
67+
}
68+
6269
const pathModule = require('path');
6370
const { isArrayBufferView } = require('internal/util/types');
6471

@@ -531,8 +538,7 @@ function readFileSync(path, options) {
531538
validateReadFileBufferOptions(options);
532539
const hasUserBuffer = options.buffer !== undefined;
533540

534-
if ((options.encoding === 'utf8' || options.encoding === 'utf-8') &&
535-
!hasUserBuffer) {
541+
if (isUtf8Encoding(options.encoding) && !hasUserBuffer) {
536542
if (!isInt32(path)) {
537543
path = getValidatedPath(path);
538544
}
@@ -2906,7 +2912,7 @@ function writeFileSync(path, data, options) {
29062912
const flag = options.flag || 'w';
29072913

29082914
// C++ fast path for string data and UTF8 encoding
2909-
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
2915+
if (typeof data === 'string' && isUtf8Encoding(options.encoding)) {
29102916
if (!isInt32(path)) {
29112917
path = getValidatedPath(path);
29122918
}
@@ -3197,7 +3203,7 @@ if (isWindows) {
31973203
}
31983204

31993205
function encodeRealpathResult(result, options) {
3200-
if (!options || !options.encoding || options.encoding === 'utf8')
3206+
if (!options || !options.encoding || isUtf8Encoding(options.encoding))
32013207
return result;
32023208
const asBuffer = Buffer.from(result);
32033209
if (options.encoding === 'buffer') {

0 commit comments

Comments
 (0)