Skip to content

Commit 7fe0fa8

Browse files
committed
fs: use fast paths for UTF-8 aliases
Signed-off-by: Shaurya Saria <sariashaurya09@gmail.com>
1 parent 84654b9 commit 7fe0fa8

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

lib/fs.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,7 @@ function readFileSync(path, options) {
531531
validateReadFileBufferOptions(options);
532532
const hasUserBuffer = options.buffer !== undefined;
533533

534-
if ((options.encoding === 'utf8' || options.encoding === 'utf-8') &&
535-
!hasUserBuffer) {
534+
if (isUtf8Encoding(options.encoding) && !hasUserBuffer) {
536535
if (!isInt32(path)) {
537536
path = getValidatedPath(path);
538537
}
@@ -2906,7 +2905,7 @@ function writeFileSync(path, data, options) {
29062905
const flag = options.flag || 'w';
29072906

29082907
// C++ fast path for string data and UTF8 encoding
2909-
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
2908+
if (typeof data === 'string' && isUtf8Encoding(options.encoding)) {
29102909
if (!isInt32(path)) {
29112910
path = getValidatedPath(path);
29122911
}
@@ -3196,8 +3195,15 @@ if (isWindows) {
31963195
};
31973196
}
31983197

3198+
function isUtf8Encoding(encoding) {
3199+
return encoding === 'utf8' ||
3200+
encoding === 'utf-8' ||
3201+
encoding === 'UTF8' ||
3202+
encoding === 'UTF-8';
3203+
}
3204+
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') {

test/parallel/test-fs-readfile-utf8-fast-path.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ describe('fs.readFileSync utf8 simdutf dispatch', () => {
2828
assert.strictEqual(fs.readFileSync(p, 'utf8'), '');
2929
});
3030

31+
it('UTF-8 encoding aliases', () => {
32+
const buf = Buffer.from('hello 中文 — 🚀', 'utf8');
33+
const p = writeFile('encoding-aliases.txt', buf);
34+
for (const encoding of ['utf8', 'utf-8', 'UTF8', 'UTF-8']) {
35+
assert.strictEqual(fs.readFileSync(p, encoding), buf.toString('utf8'));
36+
}
37+
});
38+
3139
it('ascii small', () => {
3240
const buf = Buffer.from('hello');
3341
expectMatches(writeFile('tiny-ascii.txt', buf), buf);

test/parallel/test-fs-realpath-buffer-encoding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const string_dir = fs.realpathSync(fixtures.fixturesDir);
88
const buffer_dir = Buffer.from(string_dir);
99

1010
const encodings = ['ascii', 'utf8', 'utf16le', 'ucs2',
11-
'base64', 'binary', 'hex'];
11+
'base64', 'binary', 'hex', 'UTF8', 'UTF-8'];
1212
const expected = {};
1313
for (const encoding of encodings) {
1414
expected[encoding] = buffer_dir.toString(encoding);

test/parallel/test-fs-write-file-sync.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ tmpdir.refresh();
119119
}
120120
}
121121

122+
// Test writeFileSync with UTF-8 encoding aliases
123+
{
124+
const utf8Data = 'hello world! 中文 — 🚀';
125+
for (const encoding of ['utf8', 'utf-8', 'UTF8', 'UTF-8']) {
126+
const file = tmpdir.resolve(`testWriteFileSyncEncoding_${encoding}.txt`);
127+
fs.writeFileSync(file, utf8Data, { encoding });
128+
assert.strictEqual(fs.readFileSync(file, 'utf8'), utf8Data);
129+
}
130+
}
131+
122132
// Test writeFileSync with an invalid input
123133
{
124134
const file = tmpdir.resolve('testWriteFileSyncInvalid.txt');

0 commit comments

Comments
 (0)