ReadableStream.cpp 5.5 KB

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