]> git.ardis.lu - csvio/commitdiff
Fix bug where it was not possible to read into an ArrayBuffer with offset
authorArdis Lu <ardisglu@gmail.com>
Wed, 6 Aug 2025 04:38:51 +0000 (21:38 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 6 Aug 2025 04:38:51 +0000 (21:38 -0700)
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
test/core.test.js

index 9d833d660c55e0911da752d5e86cc590b342b5f6..68eb00645fb97db5ddd0043160c88d2964761bf5 100644 (file)
@@ -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<ArrayBufferLike>} */(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();
index c7a97bb385152eba1d870216896197dbf6fde3ba..c4c8eb1507af43bd6f2c967785e45dd953fe2284 100644 (file)
@@ -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 }, () => {
This page took 0.041452 seconds and 4 git commands to generate.