소스 검색

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.
Shannon Booth 6 달 전
부모
커밋
da408cb09a

+ 1 - 1
Libraries/LibWeb/Fetch/Fetching/Fetching.cpp

@@ -744,7 +744,7 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const
         transform_stream->set_up(identity_transform_algorithm, flush_algorithm);
         transform_stream->set_up(identity_transform_algorithm, flush_algorithm);
 
 
         // 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s stream piped through transformStream.
         // 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s 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);
         WebIDL::mark_promise_as_handled(*promise);
         internal_response->body()->set_stream(transform_stream->readable());
         internal_response->body()->set_stream(transform_stream->readable());
     }
     }

+ 3 - 4
Libraries/LibWeb/Streams/AbstractOperations.cpp

@@ -290,7 +290,7 @@ bool readable_stream_has_default_reader(ReadableStream const& stream)
 }
 }
 
 
 // https://streams.spec.whatwg.org/#readable-stream-pipe-to
 // 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();
     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.
     // 3. Assert: preventClose, preventAbort, and preventCancel are all booleans.
 
 
     // 4. If signal was not given, let signal be undefined.
     // 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.
     // 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.
     // 6. Assert: ! IsReadableStreamLocked(source) is false.
     VERIFY(!is_readable_stream_locked(source));
     VERIFY(!is_readable_stream_locked(source));

+ 1 - 1
Libraries/LibWeb/Streams/AbstractOperations.h

@@ -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_byob_reader(ReadableStream const&);
 bool readable_stream_has_default_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_tee(JS::Realm&, ReadableStream&, bool clone_for_branch2);
 WebIDL::ExceptionOr<ReadableStreamPair> readable_stream_default_tee(JS::Realm& realm, ReadableStream& stream, bool clone_for_branch2);
 WebIDL::ExceptionOr<ReadableStreamPair> readable_stream_default_tee(JS::Realm& realm, ReadableStream& stream, bool clone_for_branch2);

+ 16 - 0
Libraries/LibWeb/Streams/ReadableStream.cpp

@@ -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()));
     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);
+}
+
 }
 }

+ 1 - 0
Libraries/LibWeb/Streams/ReadableStream.h

@@ -108,6 +108,7 @@ public:
     WebIDL::ExceptionOr<void> pull_from_bytes(ByteBuffer);
     WebIDL::ExceptionOr<void> pull_from_bytes(ByteBuffer);
     WebIDL::ExceptionOr<void> enqueue(JS::Value chunk);
     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);
     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:
 private:
     explicit ReadableStream(JS::Realm&);
     explicit ReadableStream(JS::Realm&);