From 28da9a93796b8c814c4f0dc6312e53591298767f Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 5 Aug 2025 21:38:51 -0700 Subject: [PATCH] Fix bug where it was not possible to read into an ArrayBuffer with offset The code was originally correct because it was copied from the Streams specification [1], but turned incorrect after copying from Node.js's implementation [2], and now reverted back to the original correct implementation. [1] https://streams.spec.whatwg.org/#example-rbs-pull [2] https://github.com/nodejs/node/blob/134625d76139b4b3630d5baaf2efccae01ede564/lib/internal/fs/promises.js#L322 --- src/core.js | 2 +- test/core.test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index 9d833d6..68eb006 100644 --- a/src/core.js +++ b/src/core.js @@ -68,7 +68,7 @@ export function createFileStream(path) { // impossible to respond to the byobRequest and set view to null before this moment). const view = /** @type {ArrayBufferView} */(byobRequest.view); - const { bytesRead } = await handle.read(view, view.byteOffset, view.byteLength); + const { bytesRead } = await handle.read(view, 0, view.byteLength); if (bytesRead === 0) { await handle.close(); controller.close(); diff --git a/test/core.test.js b/test/core.test.js index c7a97bb..c4c8eb1 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -126,6 +126,16 @@ suite('createFileStream', { concurrency: true }, () => { deepStrictEqual(c?.[0], 99); deepStrictEqual(c2?.[0], 111); }); + test('can read bytes into buffer', { concurrency: true }, async () => { + const path = new URL('./data/simple.csv', import.meta.url); + const s = createFileStream(path); + const r = s.getReader({ mode: 'byob' }); + const b = new ArrayBuffer(3); + const v = new Uint8Array(b, 2, 1); + const res = await r.read(v); + deepStrictEqual(res.value?.[0], 99); + await r.cancel(); + }); }); suite('CSVReader', { concurrency: true }, () => { -- 2.47.3