ReadableStream.cpp 5.4 KB

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