ReadableStream.cpp 6.3 KB

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