diff --git a/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.cpp b/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.cpp index e8875d1c28..2b40215cbc 100644 --- a/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.cpp +++ b/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.cpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace Web::Streams { @@ -67,6 +68,26 @@ void ReadableByteStreamController::initialize(JS::Realm& realm) set_prototype(&Bindings::ensure_web_prototype(realm, "ReadableByteStreamController"_fly_string)); } +// https://streams.spec.whatwg.org/#rbs-controller-enqueue +WebIDL::ExceptionOr ReadableByteStreamController::enqueue(JS::Handle& chunk) +{ + // 1. If chunk.[[ByteLength]] is 0, throw a TypeError exception. + // 2. If chunk.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is 0, throw a TypeError exception. + if (chunk->byte_length() == 0 || chunk->viewed_array_buffer()->byte_length() == 0) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot enqueue chunk with byte length of zero"sv }; + + // 3. If this.[[closeRequested]] is true, throw a TypeError exception. + if (m_close_requested) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Close is requested for controller"sv }; + + // 4. If this.[[stream]].[[state]] is not "readable", throw a TypeError exception. + if (!m_stream->is_readable()) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Stream is not readable"sv }; + + // 5. Return ? ReadableByteStreamControllerEnqueue(this, chunk). + return readable_byte_stream_controller_enqueue(*this, chunk->raw_object()); +} + // https://streams.spec.whatwg.org/#rbs-controller-private-cancel WebIDL::ExceptionOr> ReadableByteStreamController::cancel_steps(JS::Value reason) { diff --git a/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.h b/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.h index 70485eb3e3..b2aa4c95f9 100644 --- a/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.h +++ b/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.h @@ -89,6 +89,7 @@ public: Optional desired_size() const; WebIDL::ExceptionOr close(); void error(JS::Value error); + WebIDL::ExceptionOr enqueue(JS::Handle&); Optional const& auto_allocate_chunk_size() { return m_auto_allocate_chunk_size; } void set_auto_allocate_chunk_size(Optional value) { m_auto_allocate_chunk_size = value; } diff --git a/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.idl b/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.idl index 006bb27363..fcb9736b78 100644 --- a/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.idl +++ b/Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.idl @@ -8,5 +8,5 @@ interface ReadableByteStreamController { undefined close(); undefined error(optional any e); - // FIXME: undefined enqueue(ArrayBufferView chunk); + undefined enqueue(ArrayBufferView chunk); };