LibWeb: Add a 'piped through' helper method on ReadableStream

This reads a bit nicer, and follows the streams spec pattern on
performing operations on a stream outside of the streams spec.
This commit is contained in:
Shannon Booth 2024-12-24 12:25:01 +13:00 committed by Andreas Kling
parent 79a2b96d1c
commit da408cb09a
Notes: github-actions[bot] 2024-12-25 11:03:22 +00:00
5 changed files with 22 additions and 6 deletions

View file

@ -744,7 +744,7 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const
transform_stream->set_up(identity_transform_algorithm, flush_algorithm);
// 4. Set internalResponses bodys stream to the result of internalResponses bodys stream piped through transformStream.
auto promise = Streams::readable_stream_pipe_to(internal_response->body()->stream(), transform_stream->writable(), false, false, false, {});
auto promise = internal_response->body()->stream()->piped_through(transform_stream->writable());
WebIDL::mark_promise_as_handled(*promise);
internal_response->body()->set_stream(transform_stream->readable());
}

View file

@ -290,7 +290,7 @@ bool readable_stream_has_default_reader(ReadableStream const& stream)
}
// https://streams.spec.whatwg.org/#readable-stream-pipe-to
GC::Ref<WebIDL::Promise> readable_stream_pipe_to(ReadableStream& source, WritableStream& dest, bool, bool, bool, Optional<JS::Value> signal)
GC::Ref<WebIDL::Promise> readable_stream_pipe_to(ReadableStream& source, WritableStream& dest, bool, bool, bool, JS::Value signal)
{
auto& realm = source.realm();
@ -299,11 +299,10 @@ GC::Ref<WebIDL::Promise> readable_stream_pipe_to(ReadableStream& source, Writabl
// 3. Assert: preventClose, preventAbort, and preventCancel are all booleans.
// 4. If signal was not given, let signal be undefined.
if (!signal.has_value())
signal = JS::js_undefined();
// NOTE: Done by default argument
// 5. Assert: either signal is undefined, or signal implements AbortSignal.
VERIFY(signal->is_undefined() || (signal->is_object() && is<DOM::AbortSignal>(signal->as_object())));
VERIFY(signal.is_undefined() || (signal.is_object() && is<DOM::AbortSignal>(signal.as_object())));
// 6. Assert: ! IsReadableStreamLocked(source) is false.
VERIFY(!is_readable_stream_locked(source));

View file

@ -40,7 +40,7 @@ size_t readable_stream_get_num_read_requests(ReadableStream const&);
bool readable_stream_has_byob_reader(ReadableStream const&);
bool readable_stream_has_default_reader(ReadableStream const&);
GC::Ref<WebIDL::Promise> readable_stream_pipe_to(ReadableStream& source, WritableStream& dest, bool prevent_close, bool prevent_abort, bool prevent_cancel, Optional<JS::Value> signal);
GC::Ref<WebIDL::Promise> readable_stream_pipe_to(ReadableStream& source, WritableStream& dest, bool prevent_close, bool prevent_abort, bool prevent_cancel, JS::Value signal = JS::js_undefined());
WebIDL::ExceptionOr<ReadableStreamPair> readable_stream_tee(JS::Realm&, ReadableStream&, bool clone_for_branch2);
WebIDL::ExceptionOr<ReadableStreamPair> readable_stream_default_tee(JS::Realm& realm, ReadableStream& stream, bool clone_for_branch2);

View file

@ -383,4 +383,20 @@ void ReadableStream::set_up_with_byte_reading_support(GC::Ptr<PullAlgorithm> pul
MUST(set_up_readable_byte_stream_controller(*this, controller, start_algorithm, pull_algorithm_wrapper, cancel_algorithm_wrapper, high_water_mark, JS::js_undefined()));
}
// https://streams.spec.whatwg.org/#readablestream-pipe-through
GC::Ref<WebIDL::Promise> ReadableStream::piped_through(GC::Ref<WritableStream> writable, bool prevent_close, bool prevent_abort, bool prevent_cancel, JS::Value signal)
{
// 1. Assert: ! IsReadableStreamLocked(readable) is false.
VERIFY(!is_readable_stream_locked(*this));
// 2. Assert: ! IsWritableStreamLocked(writable) is false.
VERIFY(!is_writable_stream_locked(writable));
// 3. Let signalArg be signal if signal was given, or undefined otherwise.
// NOTE: Done by default arguments.
// 4. Return ! ReadableStreamPipeTo(readable, writable, preventClose, preventAbort, preventCancel, signalArg).
return readable_stream_pipe_to(*this, writable, prevent_close, prevent_abort, prevent_cancel, signal);
}
}

View file

@ -108,6 +108,7 @@ public:
WebIDL::ExceptionOr<void> pull_from_bytes(ByteBuffer);
WebIDL::ExceptionOr<void> enqueue(JS::Value chunk);
void set_up_with_byte_reading_support(GC::Ptr<PullAlgorithm> = {}, GC::Ptr<CancelAlgorithm> = {}, double high_water_mark = 0);
GC::Ref<WebIDL::Promise> piped_through(GC::Ref<WritableStream>, bool prevent_close = false, bool prevent_abort = false, bool prevent_cancel = false, JS::Value signal = JS::js_undefined());
private:
explicit ReadableStream(JS::Realm&);