From: Ardis Lu Date: Sun, 4 May 2025 02:21:40 +0000 (-0700) Subject: Explicitly assert that `byobRequest` and `view` are not null X-Git-Tag: v0.1.0~22 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=926f8084bb7542e42c5849b1e7c490880a69a1e2;p=csvio Explicitly assert that `byobRequest` and `view` are not null --- diff --git a/src/core.js b/src/core.js index 37b3636..4756db7 100644 --- a/src/core.js +++ b/src/core.js @@ -48,16 +48,23 @@ export function createFileStream(path) { resolveReady(); }, async pull(controller) { - const v = controller.byobRequest.view; - const { bytesRead } = await handle.read(v, 0, v.byteLength, position); + // byobRequest can never be null if autoAllocateChunkSize is set. + // https://streams.spec.whatwg.org/#dom-underlyingsource-autoallocatechunksize + const byobRequest = /** @type {ReadableStreamBYOBRequest} */(controller.byobRequest); + + // view can never be null since this is the first reference to the view within the response (i.e., it is + // 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); if (bytesRead === 0) { await handle.close(); controller.close(); - controller.byobRequest.respond(0); + byobRequest.respond(0); } else { position += bytesRead; - controller.byobRequest.respond(bytesRead); + byobRequest.respond(bytesRead); } }, async cancel() {