From: Ardis Lu Date: Sun, 13 Jul 2025 05:30:53 +0000 (-0700) Subject: Simplify implementation X-Git-Tag: v0.1.4~3 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=96cb9f6fd0dfff6e240bae7d98d7fe19738c9a7b;p=csvio Simplify implementation --- diff --git a/src/core.js b/src/core.js index cfd4114..948eefb 100644 --- a/src/core.js +++ b/src/core.js @@ -38,14 +38,14 @@ export function parsePathLike(path) { /** * Creates a readable byte stream from a local file. * - * Most code was copied from https://streams.spec.whatwg.org/#example-rbs-pull + * @see {@link https://streams.spec.whatwg.org/#example-rbs-pull} + * @see {@link https://github.com/nodejs/node/blob/049664bbdc421c63b2145c85a18c64d184b40aa5/lib/internal/fs/promises.js#L277-L346} * @param {PathLike} path A `string`, `Buffer`, or `URL` representing a path to a local file. * @returns {ReadableStream>} */ export function createFileStream(path) { /** @type {FileHandle} */ let handle; - let position = 0; // Can replace with Promise.withResolvers after Node.js v20 is no longer LTS /** @type {() => void} */ @@ -56,7 +56,7 @@ export function createFileStream(path) { type: 'bytes', autoAllocateChunkSize: 65536, // 64 KiB async start() { - handle = await open(path, "r"); + handle = await open(path, 'r'); resolveReady(); }, async pull(controller) { @@ -68,16 +68,12 @@ 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, 0, view.byteLength, position); + const { bytesRead } = await handle.read(view, view.byteOffset, view.byteLength); if (bytesRead === 0) { await handle.close(); controller.close(); - byobRequest.respond(0); - } - else { - position += bytesRead; - byobRequest.respond(bytesRead); } + byobRequest.respond(bytesRead); }, async cancel() { await ready; // If cancel() is called immediately after stream instantiation, start() may not have finished yet.