diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index be8d35e13e97e4..db2e54bad68342 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -399,7 +399,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) { diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index 74383a02e42fd9..106e0bc4eb2b78 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -68,6 +68,24 @@ async function validateReadNoParams() { } let useConf = false; +// 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() { for (const value of [false, true]) { @@ -80,4 +98,5 @@ let useConf = false; .then(common.mustCall()); } await validateReadNoParams(); + await validateReadWithPositionZero(); })().then(common.mustCall());