Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nullish coalescing to respect zero positional reads #40716

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ async function read(handle, bufferOrOptions, offset, length, position) {
}
offset = bufferOrOptions.offset || 0;
length = buffer.byteLength;
position = bufferOrOptions.position || null;
position = bufferOrOptions.position ?? null;
}

if (offset == null) {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ async function validateReadNoParams() {
await fileHandle.read();
}

// Validates that the zero position is respected after the position has been
// moved. The test iterates over the xyz chars twice making sure that the values
// are read from the correct position.
async function validateReadWithPositionZero() {
const opts = { useConf: true };
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
const expectedSequence = ['x', 'y', 'z'];

for (let i = 0; i < expectedSequence.length * 2; i++) {
const len = 1;
const pos = i % 3;
const buf = Buffer.alloc(len);
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
assert.strictEqual(bytesRead, len);
assert.strictEqual(buf.toString(), expectedSequence[pos]);
}
}


(async function() {
tmpdir.refresh();
Expand All @@ -78,4 +97,5 @@ async function validateReadNoParams() {
await validateLargeRead({ useConf: false });
await validateLargeRead({ useConf: true });
await validateReadNoParams();
await validateReadWithPositionZero();
})().then(common.mustCall());