From 926f8084bb7542e42c5849b1e7c490880a69a1e2 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 3 May 2025 19:21:40 -0700 Subject: [PATCH] Explicitly assert that `byobRequest` and `view` are not null --- src/core.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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() { -- 2.47.3