Skip to content
Open
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
6 changes: 4 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2844,8 +2844,10 @@ 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')) {
// C++ fast path for string data and UTF8 encoding. The fast path does
// not perform an fsync, so it cannot be used when flush is requested.
if (typeof data === 'string' && !flush &&
(options.encoding === 'utf8' || options.encoding === 'utf-8')) {
if (!isInt32(path)) {
path = getValidatedPath(path);
}
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-fs-append-file-flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ test('synchronous version', async (t) => {

assert.strictEqual(spy.mock.calls.length, 0);
});

await t.test('performs flush with explicit utf8 encoding', (t) => {
// Refs: the C++ utf8 fast path must not skip the flush.
const spy = t.mock.method(fs, 'fsyncSync');

for (const encoding of ['utf8', 'utf-8']) {
const file = nextFile();
fs.appendFileSync(file, data, { encoding, flush: true });
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);
}

assert.strictEqual(spy.mock.calls.length, 2);
});
});

test('callback version', async (t) => {
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-fs-write-file-flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ test('synchronous version', async (t) => {

assert.strictEqual(spy.mock.calls.length, 0);
});

await t.test('performs flush with explicit utf8 encoding', (t) => {
// Refs: the C++ utf8 fast path must not skip the flush.
const spy = t.mock.method(fs, 'fsyncSync');

for (const encoding of ['utf8', 'utf-8']) {
const file = nextFile();
fs.writeFileSync(file, data, { encoding, flush: true });
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);
}

assert.strictEqual(spy.mock.calls.length, 2);
});
});

test('callback version', async (t) => {
Expand Down
Loading