ReadableStream.cpp 6.5 KB

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