Skip to content

Commit 9143536

Browse files
committed
lib: call normalizeEncoding in fs.writeFileSync and fs.readFileSync
1 parent f46152f commit 9143536

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

benchmark/fs/bench-writeFileSync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tmpdir.refresh();
77

88
// Some variants are commented out as they do not show a change and just slow
99
const bench = common.createBenchmark(main, {
10-
encoding: ['utf8'],
10+
encoding: ['utf8', 'UTF8'],
1111
useFd: ['true', 'false'],
1212
length: [1024, 102400, 1024 * 1024],
1313

lib/fs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const {
6060

6161
const pathModule = require('path');
6262
const { isAbsolute } = pathModule;
63+
6364
const { isArrayBufferView } = require('internal/util/types');
6465

6566
const binding = internalBinding('fs');
@@ -92,6 +93,7 @@ const {
9293
custom: kCustomPromisifiedSymbol,
9394
},
9495
SideEffectFreeRegExpPrototypeExec,
96+
normalizeEncoding,
9597
defineLazyProperties,
9698
isWindows,
9799
isMacOS,
@@ -428,7 +430,7 @@ function tryReadSync(fd, isUserFd, buffer, pos, len) {
428430
function readFileSync(path, options) {
429431
options = getOptions(options, { flag: 'r' });
430432

431-
if (options.encoding === 'utf8' || options.encoding === 'utf-8') {
433+
if (normalizeEncoding(options.encoding) === 'utf8') {
432434
if (!isInt32(path)) {
433435
path = getValidatedPath(path);
434436
}
@@ -2383,7 +2385,7 @@ function writeFileSync(path, data, options) {
23832385
const flag = options.flag || 'w';
23842386

23852387
// C++ fast path for string data and UTF8 encoding
2386-
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
2388+
if (typeof data === 'string' && normalizeEncoding(options.encoding) === 'utf8') {
23872389
if (!isInt32(path)) {
23882390
path = getValidatedPath(path);
23892391
}

0 commit comments

Comments
 (0)