LibWeb: Add Transformer API

This adds the Transformer API which the TransformStream() constructor
accepts as its first argument.
This commit is contained in:
Kenneth Myhra 2023-07-02 21:42:02 +02:00 committed by Andreas Kling
parent 3ec9fd0aae
commit 12ff48047f
Notes: sideshowbarker 2024-07-17 03:27:40 +09:00
4 changed files with 72 additions and 0 deletions

View File

@ -513,6 +513,7 @@ set(SOURCES
Streams/ReadableStreamDefaultController.cpp
Streams/ReadableStreamDefaultReader.cpp
Streams/ReadableStreamGenericReader.cpp
Streams/Transformer.cpp
Streams/UnderlyingSink.cpp
Streams/UnderlyingSource.cpp
Streams/WritableStream.cpp

View File

@ -564,6 +564,7 @@ struct PullIntoDescriptor;
struct QueuingStrategy;
struct QueuingStrategyInit;
struct ReadableStreamGetReaderOptions;
struct Transformer;
struct UnderlyingSink;
struct UnderlyingSource;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/Streams/Transformer.h>
#include <LibWeb/WebIDL/CallbackType.h>
namespace Web::Streams {
JS::ThrowCompletionOr<Transformer> Transformer::from_value(JS::VM& vm, JS::Value value)
{
if (!value.is_object())
return Transformer {};
auto& object = value.as_object();
Transformer transformer {
.start = TRY(property_to_callback(vm, value, "start", WebIDL::OperationReturnsPromise::No)),
.transform = TRY(property_to_callback(vm, value, "transform", WebIDL::OperationReturnsPromise::Yes)),
.flush = TRY(property_to_callback(vm, value, "flush", WebIDL::OperationReturnsPromise::Yes)),
.readable_type = {},
.writable_type = {},
};
if (TRY(object.has_property("readableType")))
transformer.readable_type = TRY(object.get("readableType"));
if (TRY(object.has_property("writableType")))
transformer.writable_type = TRY(object.get("writableType"));
return transformer;
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Forward.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/Forward.h>
namespace Web::Streams {
// https://streams.spec.whatwg.org/#dictdef-transformer
struct Transformer {
// https://streams.spec.whatwg.org/#dom-transformer-start
JS::Handle<WebIDL::CallbackType> start;
// https://streams.spec.whatwg.org/#dom-transformer-transform
JS::Handle<WebIDL::CallbackType> transform;
// https://streams.spec.whatwg.org/#dom-transformer-flush
JS::Handle<WebIDL::CallbackType> flush;
// https://streams.spec.whatwg.org/#dom-transformer-readabletype
Optional<JS::Value> readable_type;
// https://streams.spec.whatwg.org/#dom-transformer-writabletype
Optional<JS::Value> writable_type;
static JS::ThrowCompletionOr<Transformer> from_value(JS::VM&, JS::Value);
};
}