diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index a6b9a1b2dc35e6..4d101d115adaa6 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -469,7 +469,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 44ef2c6b815a3d..b79d65e9484a1f 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -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(); @@ -78,4 +97,5 @@ async function validateReadNoParams() { await validateLargeRead({ useConf: false }); await validateLargeRead({ useConf: true }); await validateReadNoParams(); + await validateReadWithPositionZero(); })().then(common.mustCall());