| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-04-06 19:02:28 +02:00
										 |  |  |  |  * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #pragma once
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include <AK/Forward.h>
 | 
					
						
							|  |  |  |  | #include <LibJS/Forward.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/Bindings/PlatformObject.h>
 | 
					
						
							| 
									
										
										
										
											2023-06-29 20:53:19 +12:00
										 |  |  |  | #include <LibWeb/Bindings/ReadableStreamPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-08 17:28:44 +13:00
										 |  |  |  | #include <LibWeb/Streams/Algorithms.h>
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:57:07 +12:00
										 |  |  |  | #include <LibWeb/Streams/QueuingStrategy.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::Streams { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  |  | // https://streams.spec.whatwg.org/#typedefdef-readablestreamreader
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | using ReadableStreamReader = Variant<GC::Ref<ReadableStreamDefaultReader>, GC::Ref<ReadableStreamBYOBReader>>; | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:56:11 -07:00
										 |  |  |  | // https://streams.spec.whatwg.org/#typedefdef-readablestreamcontroller
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | using ReadableStreamController = Variant<GC::Ref<ReadableStreamDefaultController>, GC::Ref<ReadableByteStreamController>>; | 
					
						
							| 
									
										
										
										
											2023-03-28 18:56:11 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-29 20:53:19 +12:00
										 |  |  |  | // https://streams.spec.whatwg.org/#dictdef-readablestreamgetreaderoptions
 | 
					
						
							|  |  |  |  | struct ReadableStreamGetReaderOptions { | 
					
						
							|  |  |  |  |     Optional<Bindings::ReadableStreamReaderMode> mode; | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-07 13:01:33 +02:00
										 |  |  |  | struct ReadableWritablePair { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ptr<ReadableStream> readable; | 
					
						
							|  |  |  |  |     GC::Ptr<WritableStream> writable; | 
					
						
							| 
									
										
										
										
											2024-04-07 13:01:33 +02:00
										 |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-06 19:02:28 +02:00
										 |  |  |  | struct StreamPipeOptions { | 
					
						
							|  |  |  |  |     bool prevent_close { false }; | 
					
						
							|  |  |  |  |     bool prevent_abort { false }; | 
					
						
							|  |  |  |  |     bool prevent_cancel { false }; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ptr<DOM::AbortSignal> signal; | 
					
						
							| 
									
										
										
										
											2024-04-06 19:02:28 +02:00
										 |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-28 10:28:59 -05:00
										 |  |  |  | struct ReadableStreamPair { | 
					
						
							|  |  |  |  |     // Define a couple container-like methods so this type may be used as the return type of the IDL `tee` implementation.
 | 
					
						
							|  |  |  |  |     size_t size() const { return 2; } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ref<ReadableStream>& at(size_t index) | 
					
						
							| 
									
										
										
										
											2024-01-28 10:28:59 -05:00
										 |  |  |  |     { | 
					
						
							|  |  |  |  |         if (index == 0) | 
					
						
							|  |  |  |  |             return first; | 
					
						
							|  |  |  |  |         if (index == 1) | 
					
						
							|  |  |  |  |             return second; | 
					
						
							|  |  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ref<ReadableStream> first; | 
					
						
							|  |  |  |  |     GC::Ref<ReadableStream> second; | 
					
						
							| 
									
										
										
										
											2024-01-28 10:28:59 -05:00
										 |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | // https://streams.spec.whatwg.org/#readablestream
 | 
					
						
							|  |  |  |  | class ReadableStream final : public Bindings::PlatformObject { | 
					
						
							| 
									
										
										
										
											2022-10-04 18:02:54 +01:00
										 |  |  |  |     WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC_DECLARE_ALLOCATOR(ReadableStream); | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | public: | 
					
						
							|  |  |  |  |     enum class State { | 
					
						
							|  |  |  |  |         Readable, | 
					
						
							|  |  |  |  |         Closed, | 
					
						
							|  |  |  |  |         Errored, | 
					
						
							|  |  |  |  |     }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     static WebIDL::ExceptionOr<GC::Ref<ReadableStream>> construct_impl(JS::Realm&, Optional<GC::Root<JS::Object>> const& underlying_source, QueuingStrategy const& = {}); | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     static WebIDL::ExceptionOr<GC::Ref<ReadableStream>> from(JS::VM& vm, JS::Value async_iterable); | 
					
						
							| 
									
										
										
										
											2024-06-08 10:44:35 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  |     virtual ~ReadableStream() override; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:34:41 +12:00
										 |  |  |  |     bool locked() const; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ref<WebIDL::Promise> cancel(JS::Value reason); | 
					
						
							| 
									
										
										
										
											2023-06-29 20:53:19 +12:00
										 |  |  |  |     WebIDL::ExceptionOr<ReadableStreamReader> get_reader(ReadableStreamGetReaderOptions const& = {}); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     WebIDL::ExceptionOr<GC::Ref<ReadableStream>> pipe_through(ReadableWritablePair transform, StreamPipeOptions const& = {}); | 
					
						
							|  |  |  |  |     GC::Ref<WebIDL::Promise> pipe_to(WritableStream& destination, StreamPipeOptions const& = {}); | 
					
						
							| 
									
										
										
										
											2024-01-28 10:28:59 -05:00
										 |  |  |  |     WebIDL::ExceptionOr<ReadableStreamPair> tee(); | 
					
						
							| 
									
										
										
										
											2023-03-28 20:01:04 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-20 14:28:17 +02:00
										 |  |  |  |     void close(); | 
					
						
							| 
									
										
										
										
											2024-05-25 16:55:38 -04:00
										 |  |  |  |     void error(JS::Value); | 
					
						
							| 
									
										
										
										
											2024-05-20 14:28:17 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 12:25:32 -07:00
										 |  |  |  |     Optional<ReadableStreamController>& controller() { return m_controller; } | 
					
						
							|  |  |  |  |     void set_controller(Optional<ReadableStreamController> value) { m_controller = move(value); } | 
					
						
							| 
									
										
										
										
											2023-03-28 18:56:11 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  |     JS::Value stored_error() const { return m_stored_error; } | 
					
						
							| 
									
										
										
										
											2023-03-28 18:56:11 -07:00
										 |  |  |  |     void set_stored_error(JS::Value value) { m_stored_error = value; } | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-09 01:26:48 -07:00
										 |  |  |  |     Optional<ReadableStreamReader> const& reader() const { return m_reader; } | 
					
						
							|  |  |  |  |     void set_reader(Optional<ReadableStreamReader> value) { m_reader = move(value); } | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:56:11 -07:00
										 |  |  |  |     bool is_disturbed() const; | 
					
						
							|  |  |  |  |     void set_disturbed(bool value) { m_disturbed = value; } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  |     bool is_readable() const; | 
					
						
							|  |  |  |  |     bool is_closed() const; | 
					
						
							|  |  |  |  |     bool is_errored() const; | 
					
						
							|  |  |  |  |     bool is_locked() const; | 
					
						
							| 
									
										
										
										
											2023-04-09 09:42:03 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     State state() const { return m_state; } | 
					
						
							|  |  |  |  |     void set_state(State value) { m_state = value; } | 
					
						
							| 
									
										
										
										
											2023-03-28 17:43:34 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 12:38:25 +13:00
										 |  |  |  |     WebIDL::ExceptionOr<GC::Ref<ReadableStreamDefaultReader>> get_a_reader(); | 
					
						
							| 
									
										
										
										
											2024-12-08 17:28:44 +13:00
										 |  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2024-12-26 12:02:07 +13:00
										 |  |  |  |     GC::Ref<ReadableStream> piped_through(GC::Ref<TransformStream>, bool prevent_close = false, bool prevent_abort = false, bool prevent_cancel = false, JS::Value signal = JS::js_undefined()); | 
					
						
							| 
									
										
										
										
											2024-12-08 17:28:44 +13:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 16:20:02 +13:00
										 |  |  |  |     GC::Ptr<WebIDL::ArrayBufferView> current_byob_request_view(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | private: | 
					
						
							| 
									
										
										
											
												LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
											
										 
											2022-09-25 18:11:21 -06:00
										 |  |  |  |     explicit ReadableStream(JS::Realm&); | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://streams.spec.whatwg.org/#readablestream-controller
 | 
					
						
							|  |  |  |  |     // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
 | 
					
						
							| 
									
										
										
										
											2023-04-08 12:25:32 -07:00
										 |  |  |  |     Optional<ReadableStreamController> m_controller; | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://streams.spec.whatwg.org/#readablestream-detached
 | 
					
						
							|  |  |  |  |     // A boolean flag set to true when the stream is transferred
 | 
					
						
							|  |  |  |  |     bool m_detached { false }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://streams.spec.whatwg.org/#readablestream-disturbed
 | 
					
						
							|  |  |  |  |     // A boolean flag set to true when the stream has been read from or canceled
 | 
					
						
							|  |  |  |  |     bool m_disturbed { false }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://streams.spec.whatwg.org/#readablestream-reader
 | 
					
						
							|  |  |  |  |     // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
 | 
					
						
							| 
									
										
										
										
											2023-04-09 01:26:48 -07:00
										 |  |  |  |     Optional<ReadableStreamReader> m_reader; | 
					
						
							| 
									
										
										
										
											2022-09-21 23:27:13 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://streams.spec.whatwg.org/#readablestream-state
 | 
					
						
							|  |  |  |  |     // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
 | 
					
						
							|  |  |  |  |     State m_state { State::Readable }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://streams.spec.whatwg.org/#readablestream-storederror
 | 
					
						
							|  |  |  |  |     // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
 | 
					
						
							|  |  |  |  |     JS::Value m_stored_error { JS::js_undefined() }; | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |