From e6e708e46c51f3154a81ed99cd35c3d5569930f9 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Fri, 1 Dec 2023 08:56:10 -0700 Subject: refactor: use resourceForReadableStream for fetch (#20217) Switch `ext/fetch` over to `resourceForReadableStream` to simplify and unify implementation with `ext/serve`. This allows us to work in Rust with resources only. Two additional changes made to `resourceForReadableStream` were required: - Add an optional length to `resourceForReadableStream` which translates to `size_hint` - Fix a bug where writing to a closed stream that was full would panic --- ext/web/06_streams.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'ext/web/06_streams.js') diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index 4f472984d..9fc15d1ad 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -12,6 +12,7 @@ const { op_arraybuffer_was_detached, op_transfer_arraybuffer, op_readable_stream_resource_allocate, + op_readable_stream_resource_allocate_sized, op_readable_stream_resource_get_sink, op_readable_stream_resource_write_error, op_readable_stream_resource_write_buf, @@ -863,13 +864,16 @@ function readableStreamReadFn(reader, sink) { * read operations, and those read operations will be fed by the output of the * ReadableStream source. * @param {ReadableStream} stream + * @param {number | undefined} length * @returns {number} */ -function resourceForReadableStream(stream) { +function resourceForReadableStream(stream, length) { const reader = acquireReadableStreamDefaultReader(stream); // Allocate the resource - const rid = op_readable_stream_resource_allocate(); + const rid = typeof length == "number" + ? op_readable_stream_resource_allocate_sized(length) + : op_readable_stream_resource_allocate(); // Close the Reader we get from the ReadableStream when the resource is closed, ignoring any errors PromisePrototypeCatch( -- cgit v1.2.3