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
16 changes: 13 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2381,27 +2381,37 @@ function writeFileSync(path, data, options) {
validateBoolean(flush, 'options.flush');

const flag = options.flag || 'w';
const isUserFd = isFd(path);

// C++ fast path for string data and UTF8 encoding
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
if (
typeof data === 'string' &&
(options.encoding === 'utf8' || options.encoding === 'utf-8') &&
!(flush && !isUserFd)
) {
if (!isInt32(path)) {
path = getValidatedPath(path);
}

return binding.writeFileUtf8(
binding.writeFileUtf8(
path,
data,
stringToFlags(flag),
parseFileMode(options.mode, 'mode', 0o666),
);

if (flush && isUserFd) {
fs.fsyncSync(path);
}

return;
}

if (!isArrayBufferView(data)) {
validateStringAfterArrayBufferView(data, 'data');
data = Buffer.from(data, options.encoding || 'utf8');
}

const isUserFd = isFd(path); // File descriptor ownership
const fd = isUserFd ? path : fs.openSync(path, flag, options.mode);

let offset = 0;
Expand Down
25 changes: 19 additions & 6 deletions test/parallel/test-fs-append-file-flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const fsp = require('node:fs/promises');
const test = require('node:test');
const data = 'foo';
const data2 = 'bar';
let cnt = 0;

function nextFile() {
Expand All @@ -25,15 +26,27 @@

await t.test('performs flush', (t) => {
const spy = t.mock.method(fs, 'fsyncSync');
const checkCalls = (expected) => {
const calls = spy.mock.calls;
assert.strictEqual(calls.length, expected);

Check failure on line 31 in test/parallel/test-fs-append-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
if (expected === 0) return;
assert.strictEqual(calls.at(-1).result, undefined);

Check failure on line 33 in test/parallel/test-fs-append-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
assert.strictEqual(calls.at(-1).error, undefined);

Check failure on line 34 in test/parallel/test-fs-append-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
assert.strictEqual(calls.at(-1).arguments.length, 1);

Check failure on line 35 in test/parallel/test-fs-append-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
assert.strictEqual(typeof calls.at(-1).arguments[0], 'number');

Check failure on line 36 in test/parallel/test-fs-append-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
};

const file = nextFile();

checkCalls(0);
fs.appendFileSync(file, data, { flush: true });
const calls = spy.mock.calls;
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].result, undefined);
assert.strictEqual(calls[0].error, undefined);
assert.strictEqual(calls[0].arguments.length, 1);
assert.strictEqual(typeof calls[0].arguments[0], 'number');
checkCalls(1);
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);

checkCalls(1);
fs.appendFileSync(file, data2, { flush: true });
checkCalls(2);
assert.strictEqual(fs.readFileSync(file, 'utf8'), data + data2);
});

await t.test('does not perform flush', (t) => {
Expand Down
25 changes: 19 additions & 6 deletions test/parallel/test-fs-write-file-flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const fsp = require('node:fs/promises');
const test = require('node:test');
const data = 'foo';
const data2 = 'bar';
let cnt = 0;

function nextFile() {
Expand All @@ -25,15 +26,27 @@

await t.test('performs flush', (t) => {
const spy = t.mock.method(fs, 'fsyncSync');
const checkCalls = (expected) => {
const calls = spy.mock.calls;
assert.strictEqual(calls.length, expected);

Check failure on line 31 in test/parallel/test-fs-write-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
if (expected === 0) return;
assert.strictEqual(calls.at(-1).result, undefined);

Check failure on line 33 in test/parallel/test-fs-write-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
assert.strictEqual(calls.at(-1).error, undefined);

Check failure on line 34 in test/parallel/test-fs-write-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
assert.strictEqual(calls.at(-1).arguments.length, 1);

Check failure on line 35 in test/parallel/test-fs-write-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
assert.strictEqual(typeof calls.at(-1).arguments[0], 'number');

Check failure on line 36 in test/parallel/test-fs-write-file-flush.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Assertions must be wrapped into `common.mustSucceed`, `common.mustCall` or `common.mustCallAtLeast`
};

const file = nextFile();

checkCalls(0);
fs.writeFileSync(file, data, { flush: true });
const calls = spy.mock.calls;
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].result, undefined);
assert.strictEqual(calls[0].error, undefined);
assert.strictEqual(calls[0].arguments.length, 1);
assert.strictEqual(typeof calls[0].arguments[0], 'number');
checkCalls(1);
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);

checkCalls(1);
fs.writeFileSync(file, data2, { flush: true, encoding: 'utf8' });
checkCalls(2);
assert.strictEqual(fs.readFileSync(file, 'utf8'), data2);
});

await t.test('does not perform flush', (t) => {
Expand Down
Loading