ReadableStream.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/Bindings/ReadableStreamPrototype.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/Streams/Algorithms.h>
  14. #include <LibWeb/Streams/QueuingStrategy.h>
  15. namespace Web::Streams {
  16. // https://streams.spec.whatwg.org/#typedefdef-readablestreamreader
  17. using ReadableStreamReader = Variant<GC::Ref<ReadableStreamDefaultReader>, GC::Ref<ReadableStreamBYOBReader>>;
  18. // https://streams.spec.whatwg.org/#typedefdef-readablestreamcontroller
  19. using ReadableStreamController = Variant<GC::Ref<ReadableStreamDefaultController>, GC::Ref<ReadableByteStreamController>>;
  20. // https://streams.spec.whatwg.org/#dictdef-readablestreamgetreaderoptions
  21. struct ReadableStreamGetReaderOptions {
  22. Optional<Bindings::ReadableStreamReaderMode> mode;
  23. };
  24. struct ReadableWritablePair {
  25. GC::Ptr<ReadableStream> readable;
  26. GC::Ptr<WritableStream> writable;
  27. };
  28. struct StreamPipeOptions {
  29. bool prevent_close { false };
  30. bool prevent_abort { false };
  31. bool prevent_cancel { false };
  32. GC::Ptr<DOM::AbortSignal> signal;
  33. };
  34. struct ReadableStreamPair {
  35. // Define a couple container-like methods so this type may be used as the return type of the IDL `tee` implementation.
  36. size_t size() const { return 2; }
  37. GC::Ref<ReadableStream>& at(size_t index)
  38. {
  39. if (index == 0)
  40. return first;
  41. if (index == 1)
  42. return second;
  43. VERIFY_NOT_REACHED();
  44. }
  45. GC::Ref<ReadableStream> first;
  46. GC::Ref<ReadableStream> second;
  47. };
  48. // https://streams.spec.whatwg.org/#readablestream
  49. class ReadableStream final : public Bindings::PlatformObject {
  50. WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
  51. GC_DECLARE_ALLOCATOR(ReadableStream);
  52. public:
  53. enum class State {
  54. Readable,
  55. Closed,
  56. Errored,
  57. };
  58. static WebIDL::ExceptionOr<GC::Ref<ReadableStream>> construct_impl(JS::Realm&, Optional<GC::Root<JS::Object>> const& underlying_source, QueuingStrategy const& = {});
  59. static WebIDL::ExceptionOr<GC::Ref<ReadableStream>> from(JS::VM& vm, JS::Value async_iterable);
  60. virtual ~ReadableStream() override;
  61. bool locked() const;
  62. GC::Ref<WebIDL::Promise> cancel(JS::Value reason);
  63. WebIDL::ExceptionOr<ReadableStreamReader> get_reader(ReadableStreamGetReaderOptions const& = {});
  64. WebIDL::ExceptionOr<GC::Ref<ReadableStream>> pipe_through(ReadableWritablePair transform, StreamPipeOptions const& = {});
  65. GC::Ref<WebIDL::Promise> pipe_to(WritableStream& destination, StreamPipeOptions const& = {});
  66. WebIDL::ExceptionOr<ReadableStreamPair> tee();
  67. void close();
  68. void error(JS::Value);
  69. Optional<ReadableStreamController>& controller() { return m_controller; }
  70. void set_controller(Optional<ReadableStreamController> value) { m_controller = move(value); }
  71. JS::Value stored_error() const { return m_stored_error; }
  72. void set_stored_error(JS::Value value) { m_stored_error = value; }
  73. Optional<ReadableStreamReader> const& reader() const { return m_reader; }
  74. void set_reader(Optional<ReadableStreamReader> value) { m_reader = move(value); }
  75. bool is_disturbed() const;
  76. void set_disturbed(bool value) { m_disturbed = value; }
  77. bool is_readable() const;
  78. bool is_closed() const;
  79. bool is_errored() const;
  80. bool is_locked() const;
  81. State state() const { return m_state; }
  82. void set_state(State value) { m_state = value; }
  83. WebIDL::ExceptionOr<void> pull_from_bytes(ByteBuffer);
  84. WebIDL::ExceptionOr<void> enqueue(JS::Value chunk);
  85. void set_up_with_byte_reading_support(GC::Ptr<PullAlgorithm> = {}, GC::Ptr<CancelAlgorithm> = {}, double high_water_mark = 0);
  86. GC::Ref<WebIDL::Promise> piped_through(GC::Ref<WritableStream>, bool prevent_close = false, bool prevent_abort = false, bool prevent_cancel = false, JS::Value signal = JS::js_undefined());
  87. private:
  88. explicit ReadableStream(JS::Realm&);
  89. virtual void initialize(JS::Realm&) override;
  90. virtual void visit_edges(Cell::Visitor&) override;
  91. // https://streams.spec.whatwg.org/#readablestream-controller
  92. // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
  93. Optional<ReadableStreamController> m_controller;
  94. // https://streams.spec.whatwg.org/#readablestream-detached
  95. // A boolean flag set to true when the stream is transferred
  96. bool m_detached { false };
  97. // https://streams.spec.whatwg.org/#readablestream-disturbed
  98. // A boolean flag set to true when the stream has been read from or canceled
  99. bool m_disturbed { false };
  100. // https://streams.spec.whatwg.org/#readablestream-reader
  101. // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
  102. Optional<ReadableStreamReader> m_reader;
  103. // https://streams.spec.whatwg.org/#readablestream-state
  104. // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
  105. State m_state { State::Readable };
  106. // https://streams.spec.whatwg.org/#readablestream-storederror
  107. // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
  108. JS::Value m_stored_error { JS::js_undefined() };
  109. };
  110. }