From 20c7300412bdb487fc758577d6256bbcf96efd12 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 30 Sep 2022 07:54:12 +0200 Subject: refactor(ext/http): remove op_http_read (#16096) We can use Resource::read_return & op_read instead. This allows HTTP request bodies to participate in FastStream. To make this work, `readableStreamForRid` required a change to allow non auto-closing resources to be handled. This required some minor changes in our FastStream paths in ext/http and ext/flash. --- ext/web/06_streams.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'ext/web') diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index cbf781b53..412c58c3c 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -654,11 +654,12 @@ * read directly from the underlying resource if they so choose (FastStream). * * @param {number} rid The resource ID to read from. + * @param {boolean=} autoClose If the resource should be auto-closed when the stream closes. Defaults to true. * @returns {ReadableStream} */ - function readableStreamForRid(rid) { + function readableStreamForRid(rid, autoClose = true) { const stream = webidl.createBranded(ReadableStream); - stream[_maybeRid] = rid; + stream[_resourceBacking] = { rid, autoClose }; const underlyingSource = { type: "bytes", async pull(controller) { @@ -666,7 +667,7 @@ try { const bytesRead = await core.read(rid, v); if (bytesRead === 0) { - core.tryClose(rid); + if (autoClose) core.tryClose(rid); controller.close(); controller.byobRequest.respond(0); } else { @@ -674,11 +675,11 @@ } } catch (e) { controller.error(e); - core.tryClose(rid); + if (autoClose) core.tryClose(rid); } }, cancel() { - core.tryClose(rid); + if (autoClose) core.tryClose(rid); }, autoAllocateChunkSize: DEFAULT_CHUNK_SIZE, }; @@ -761,8 +762,8 @@ } } - function getReadableStreamRid(stream) { - return stream[_maybeRid]; + function getReadableStreamResourceBacking(stream) { + return stream[_resourceBacking]; } /** @@ -4424,7 +4425,7 @@ WeakMapPrototypeSet(countSizeFunctionWeakMap, globalObject, size); } - const _maybeRid = Symbol("[[maybeRid]]"); + const _resourceBacking = Symbol("[[resourceBacking]]"); /** @template R */ class ReadableStream { /** @type {ReadableStreamDefaultController | ReadableByteStreamController} */ @@ -4439,8 +4440,8 @@ [_state]; /** @type {any} */ [_storedError]; - /** @type {number | null} */ - [_maybeRid] = null; + /** @type {{ rid: number, autoClose: boolean } | null} */ + [_resourceBacking] = null; /** * @param {UnderlyingSource=} underlyingSource @@ -5986,7 +5987,7 @@ readableStreamForRidUnrefable, readableStreamForRidUnrefableRef, readableStreamForRidUnrefableUnref, - getReadableStreamRid, + getReadableStreamResourceBacking, Deferred, // Exposed in global runtime scope ByteLengthQueuingStrategy, -- cgit v1.2.3