Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion benchmark/fs/bench-writeFileSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ tmpdir.refresh();

// Some variants are commented out as they do not show a change and just slow
const bench = common.createBenchmark(main, {
encoding: ['utf8'],
// Include valid case variants to measure the fast-path trade-off.
encoding: [
'utf8', 'utf-8', 'UTF8', 'UTF-8', 'Utf8', 'Utf-8',
],
useFd: ['true', 'false'],
length: [1024, 102400, 1024 * 1024],

Expand Down
5 changes: 4 additions & 1 deletion benchmark/fs/readFileSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const common = require('../common.js');
const fs = require('fs');

const bench = common.createBenchmark(main, {
encoding: ['undefined', 'utf8', 'ascii'],
// Include valid case variants to measure the fast-path trade-off.
encoding: [
'undefined', 'utf8', 'utf-8', 'UTF8', 'UTF-8', 'Utf8', 'Utf-8', 'ascii',
],
path: ['existing', 'non-existing'],
hasFileDescriptor: ['true', 'false'],
n: [1e4],
Expand Down
14 changes: 10 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,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)) {
path = getValidatedPath(path);
}
Expand Down Expand Up @@ -2906,7 +2905,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)) {
path = getValidatedPath(path);
}
Expand Down Expand Up @@ -3196,8 +3195,15 @@ if (isWindows) {
};
}

function isUtf8Encoding(encoding) {
return encoding === 'utf8' ||
encoding === 'utf-8' ||
encoding === 'UTF8' ||
encoding === 'UTF-8';
}

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') {
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-fs-readfile-utf8-fast-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ describe('fs.readFileSync utf8 simdutf dispatch', () => {
assert.strictEqual(fs.readFileSync(p, 'utf8'), '');
});

it('UTF-8 encoding aliases', () => {
const buf = Buffer.from('hello 中文 — 🚀', 'utf8');
const p = writeFile('encoding-aliases.txt', buf);
for (const encoding of ['utf8', 'utf-8', 'UTF8', 'UTF-8']) {
assert.strictEqual(fs.readFileSync(p, encoding), buf.toString('utf8'));
}
});

it('ascii small', () => {
const buf = Buffer.from('hello');
expectMatches(writeFile('tiny-ascii.txt', buf), buf);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-realpath-buffer-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const string_dir = fs.realpathSync(fixtures.fixturesDir);
const buffer_dir = Buffer.from(string_dir);

const encodings = ['ascii', 'utf8', 'utf16le', 'ucs2',
'base64', 'binary', 'hex'];
'base64', 'binary', 'hex', 'UTF8', 'UTF-8'];
const expected = {};
for (const encoding of encodings) {
expected[encoding] = buffer_dir.toString(encoding);
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-fs-write-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ tmpdir.refresh();
}
}

// Test writeFileSync with UTF-8 encoding aliases
{
const utf8Data = 'hello world! 中文 — 🚀';
for (const encoding of ['utf8', 'utf-8', 'UTF8', 'UTF-8']) {
const file = tmpdir.resolve(`testWriteFileSyncEncoding_${encoding}.txt`);
fs.writeFileSync(file, utf8Data, { encoding });
assert.strictEqual(fs.readFileSync(file, 'utf8'), utf8Data);
}
}

// Test writeFileSync with an invalid input
{
const file = tmpdir.resolve('testWriteFileSyncInvalid.txt');
Expand Down
Loading