ReadableStream.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
  4. * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/Runtime/PromiseCapability.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/DOM/AbortSignal.h>
  11. #include <LibWeb/Streams/AbstractOperations.h>
  12. #include <LibWeb/Streams/ReadableByteStreamController.h>
  13. #include <LibWeb/Streams/ReadableStream.h>
  14. #include <LibWeb/Streams/ReadableStreamBYOBReader.h>
  15. #include <LibWeb/Streams/ReadableStreamDefaultController.h>
  16. #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
  17. #include <LibWeb/Streams/UnderlyingSource.h>
  18. #include <LibWeb/WebIDL/ExceptionOr.h>
  19. namespace Web::Streams {
  20. JS_DEFINE_ALLOCATOR(ReadableStream);
  21. // https://streams.spec.whatwg.org/#rs-constructor
  22. WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_impl(JS::Realm& realm, Optional<JS::Handle<JS::Object>> const& underlying_source_object, QueuingStrategy const& strategy)
  23. {
  24. auto& vm = realm.vm();
  25. auto readable_stream = realm.heap().allocate<ReadableStream>(realm, realm);
  26. // 1. If underlyingSource is missing, set it to null.
  27. auto underlying_source = underlying_source_object.has_value() ? JS::Value(underlying_source_object.value()) : JS::js_null();
  28. // 2. Let underlyingSourceDict be underlyingSource, converted to an IDL value of type UnderlyingSource.
  29. auto underlying_source_dict = TRY(UnderlyingSource::from_value(vm, underlying_source));
  30. // 3. Perform ! InitializeReadableStream(this).
  31. // 4. If underlyingSourceDict["type"] is "bytes":
  32. if (underlying_source_dict.type.has_value() && underlying_source_dict.type.value() == ReadableStreamType::Bytes) {
  33. // 1. If strategy["size"] exists, throw a RangeError exception.
  34. if (strategy.size)
  35. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Size strategy not allowed for byte stream"sv };
  36. // 2. Let highWaterMark be ? ExtractHighWaterMark(strategy, 0).
  37. auto high_water_mark = TRY(extract_high_water_mark(strategy, 0));
  38. // 3. Perform ? SetUpReadableByteStreamControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark).
  39. TRY(set_up_readable_byte_stream_controller_from_underlying_source(*readable_stream, underlying_source, underlying_source_dict, high_water_mark));
  40. }
  41. // 5. Otherwise,
  42. else {
  43. // 1. Assert: underlyingSourceDict["type"] does not exist.
  44. VERIFY(!underlying_source_dict.type.has_value());
  45. // 2. Let sizeAlgorithm be ! ExtractSizeAlgorithm(strategy).
  46. auto size_algorithm = extract_size_algorithm(vm, strategy);
  47. // 3. Let highWaterMark be ? ExtractHighWaterMark(strategy, 1).
  48. auto high_water_mark = TRY(extract_high_water_mark(strategy, 1));
  49. // 4. Perform ? SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark, sizeAlgorithm).
  50. TRY(set_up_readable_stream_default_controller_from_underlying_source(*readable_stream, underlying_source, underlying_source_dict, high_water_mark, size_algorithm));
  51. }
  52. return readable_stream;
  53. }
  54. ReadableStream::ReadableStream(JS::Realm& realm)
  55. : PlatformObject(realm)
  56. {
  57. }
  58. ReadableStream::~ReadableStream() = default;
  59. // https://streams.spec.whatwg.org/#rs-locked
  60. bool ReadableStream::locked() const
  61. {
  62. // 1. Return ! IsReadableStreamLocked(this).
  63. return is_readable_stream_locked(*this);
  64. }
  65. // https://streams.spec.whatwg.org/#rs-cancel
  66. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> ReadableStream::cancel(JS::Value reason)
  67. {
  68. auto& realm = this->realm();
  69. // 1. If ! IsReadableStreamLocked(this) is true, return a promise rejected with a TypeError exception.
  70. if (is_readable_stream_locked(*this)) {
  71. auto exception = JS::TypeError::create(realm, "Cannot cancel a locked stream"sv);
  72. return WebIDL::create_rejected_promise(realm, JS::Value { exception })->promise();
  73. }
  74. // 2. Return ! ReadableStreamCancel(this, reason).
  75. return TRY(readable_stream_cancel(*this, reason))->promise();
  76. }
  77. // https://streams.spec.whatwg.org/#rs-get-reader
  78. WebIDL::ExceptionOr<ReadableStreamReader> ReadableStream::get_reader(ReadableStreamGetReaderOptions const& options)
  79. {
  80. // 1. If options["mode"] does not exist, return ? AcquireReadableStreamDefaultReader(this).
  81. if (!options.mode.has_value())
  82. return ReadableStreamReader { TRY(acquire_readable_stream_default_reader(*this)) };
  83. // 2. Assert: options["mode"] is "byob".
  84. VERIFY(*options.mode == Bindings::ReadableStreamReaderMode::Byob);
  85. // 3. Return ? AcquireReadableStreamBYOBReader(this).
  86. return ReadableStreamReader { TRY(acquire_readable_stream_byob_reader(*this)) };
  87. }
  88. WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::pipe_through(ReadableWritablePair transform, StreamPipeOptions const& options)
  89. {
  90. // 1. If ! IsReadableStreamLocked(this) is true, throw a TypeError exception.
  91. if (is_readable_stream_locked(*this))
  92. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Failed to execute 'pipeThrough' on 'ReadableStream': Cannot pipe a locked stream"sv };
  93. // 2. If ! IsWritableStreamLocked(transform["writable"]) is true, throw a TypeError exception.
  94. if (is_writable_stream_locked(*transform.writable))
  95. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Failed to execute 'pipeThrough' on 'ReadableStream': parameter 1's 'writable' is locked"sv };
  96. // 3. Let signal be options["signal"] if it exists, or undefined otherwise.
  97. auto signal = options.signal.has_value() ? JS::Value(options.signal.value().ptr()) : JS::js_undefined();
  98. // 4. Let promise be ! ReadableStreamPipeTo(this, transform["writable"], options["preventClose"], options["preventAbort"], options["preventCancel"], signal).
  99. auto promise = MUST(readable_stream_pipe_to(*this, *transform.writable, options.prevent_close, options.prevent_abort, options.prevent_cancel, signal));
  100. // 5. Set promise.[[PromiseIsHandled]] to true.
  101. WebIDL::mark_promise_as_handled(*promise);
  102. // 6. Return transform["readable"].
  103. return JS::NonnullGCPtr { *transform.readable };
  104. }
  105. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> ReadableStream::pipe_to(WritableStream& destination, StreamPipeOptions const& options)
  106. {
  107. auto& realm = this->realm();
  108. // 1. If ! IsReadableStreamLocked(this) is true, return a promise rejected with a TypeError exception.
  109. if (is_readable_stream_locked(*this)) {
  110. auto promise = WebIDL::create_promise(realm);
  111. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "Failed to execute 'pipeTo' on 'ReadableStream': Cannot pipe a locked stream"sv));
  112. return promise->promise();
  113. }
  114. // 2. If ! IsWritableStreamLocked(destination) is true, return a promise rejected with a TypeError exception.
  115. if (is_writable_stream_locked(destination)) {
  116. auto promise = WebIDL::create_promise(realm);
  117. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "Failed to execute 'pipeTo' on 'ReadableStream': Cannot pipe to a locked stream"sv));
  118. return promise->promise();
  119. }
  120. // 3. Let signal be options["signal"] if it exists, or undefined otherwise.
  121. auto signal = options.signal.has_value() ? JS::Value(options.signal.value().ptr()) : JS::js_undefined();
  122. // 4. Return ! ReadableStreamPipeTo(this, destination, options["preventClose"], options["preventAbort"], options["preventCancel"], signal).
  123. return MUST(readable_stream_pipe_to(*this, destination, options.prevent_close, options.prevent_abort, options.prevent_cancel, signal))->promise();
  124. }
  125. // https://streams.spec.whatwg.org/#readablestream-tee
  126. WebIDL::ExceptionOr<ReadableStreamPair> ReadableStream::tee()
  127. {
  128. // To tee a ReadableStream stream, return ? ReadableStreamTee(stream, true).
  129. return TRY(readable_stream_tee(realm(), *this, true));
  130. }
  131. void ReadableStream::initialize(JS::Realm& realm)
  132. {
  133. Base::initialize(realm);
  134. WEB_SET_PROTOTYPE_FOR_INTERFACE(ReadableStream);
  135. }
  136. void ReadableStream::visit_edges(Cell::Visitor& visitor)
  137. {
  138. Base::visit_edges(visitor);
  139. if (m_controller.has_value())
  140. m_controller->visit([&](auto& controller) { visitor.visit(controller); });
  141. visitor.visit(m_stored_error);
  142. if (m_reader.has_value())
  143. m_reader->visit([&](auto& reader) { visitor.visit(reader); });
  144. }
  145. // https://streams.spec.whatwg.org/#readablestream-locked
  146. bool ReadableStream::is_readable() const
  147. {
  148. // A ReadableStream stream is readable if stream.[[state]] is "readable".
  149. return m_state == State::Readable;
  150. }
  151. // https://streams.spec.whatwg.org/#readablestream-closed
  152. bool ReadableStream::is_closed() const
  153. {
  154. // A ReadableStream stream is closed if stream.[[state]] is "closed".
  155. return m_state == State::Closed;
  156. }
  157. // https://streams.spec.whatwg.org/#readablestream-errored
  158. bool ReadableStream::is_errored() const
  159. {
  160. // A ReadableStream stream is errored if stream.[[state]] is "errored".
  161. return m_state == State::Errored;
  162. }
  163. // https://streams.spec.whatwg.org/#readablestream-locked
  164. bool ReadableStream::is_locked() const
  165. {
  166. // A ReadableStream stream is locked if ! IsReadableStreamLocked(stream) returns true.
  167. return is_readable_stream_locked(*this);
  168. }
  169. // https://streams.spec.whatwg.org/#is-readable-stream-disturbed
  170. bool ReadableStream::is_disturbed() const
  171. {
  172. // A ReadableStream stream is disturbed if stream.[[disturbed]] is true.
  173. return m_disturbed;
  174. }
  175. }