ReadableStream.cpp 5.4 KB

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