1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:44:58 +00:00

LibWeb: Implement ReadableByteStreamController.enqueue

This commit is contained in:
Shannon Booth 2023-12-02 21:45:40 +13:00 committed by Andreas Kling
parent feb7fbb95d
commit 48aa9fbe07
3 changed files with 23 additions and 1 deletions

View file

@ -11,6 +11,7 @@
#include <LibWeb/Streams/ReadableStream.h>
#include <LibWeb/Streams/ReadableStreamBYOBRequest.h>
#include <LibWeb/Streams/ReadableStreamDefaultReader.h>
#include <LibWeb/WebIDL/Buffers.h>
namespace Web::Streams {
@ -67,6 +68,26 @@ void ReadableByteStreamController::initialize(JS::Realm& realm)
set_prototype(&Bindings::ensure_web_prototype<Bindings::ReadableByteStreamControllerPrototype>(realm, "ReadableByteStreamController"_fly_string));
}
// https://streams.spec.whatwg.org/#rbs-controller-enqueue
WebIDL::ExceptionOr<void> ReadableByteStreamController::enqueue(JS::Handle<WebIDL::ArrayBufferView>& 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<JS::NonnullGCPtr<WebIDL::Promise>> ReadableByteStreamController::cancel_steps(JS::Value reason)
{

View file

@ -89,6 +89,7 @@ public:
Optional<double> desired_size() const;
WebIDL::ExceptionOr<void> close();
void error(JS::Value error);
WebIDL::ExceptionOr<void> enqueue(JS::Handle<WebIDL::ArrayBufferView>&);
Optional<u64> const& auto_allocate_chunk_size() { return m_auto_allocate_chunk_size; }
void set_auto_allocate_chunk_size(Optional<u64> value) { m_auto_allocate_chunk_size = value; }

View file

@ -8,5 +8,5 @@ interface ReadableByteStreamController {
undefined close();
undefined error(optional any e);
// FIXME: undefined enqueue(ArrayBufferView chunk);
undefined enqueue(ArrayBufferView chunk);
};