ladybird/Libraries/LibWeb/Streams/TransformStream.h
Timothy Flynn 64abc6101d LibWeb+WebWorker: Use IPC mechanics for structured serialization
Our structured serialization implementation had its own bespoke encoder
and decoder to serialize JS values. It also used a u32 buffer under the
hood, which made using its structures a bit awkward. We had previously
worked around its data structures in transferable streams, which nested
transfers of MessagePort instances. We basically had to add hooks into
the MessagePort to route to the correct transfer receiving steps, and
we could not invoke the correct AOs directly as the spec dictates.

We now use IPC mechanics to encode and decode data. This works because,
although we are encoding JS values, we are only ultimately encoding
primitive and basic AK types. The resulting data structures actually
enforce that we implement transferable streams exactly as the spec is
worded (I had planned to do that in a separate commit, but the fallout
of this patch actually required that change).
2025-07-18 10:09:02 -04:00

83 lines
3.4 KiB
C++

/*
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Bindings/Transferable.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Streams/Algorithms.h>
#include <LibWeb/Streams/QueuingStrategy.h>
#include <LibWeb/WebIDL/Promise.h>
namespace Web::Streams {
class TransformStream final
: public Bindings::PlatformObject
, public Bindings::Transferable {
WEB_PLATFORM_OBJECT(TransformStream, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(TransformStream);
public:
virtual ~TransformStream() override;
static WebIDL::ExceptionOr<GC::Ref<TransformStream>> construct_impl(JS::Realm&, Optional<GC::Root<JS::Object>> transformer_object = {}, QueuingStrategy const& writable_strategy = {}, QueuingStrategy const& readable_strategy = {});
// https://streams.spec.whatwg.org/#ts-readable
GC::Ref<ReadableStream> readable() { return *m_readable; }
void set_readable(ReadableStream& readable) { m_readable = readable; }
// https://streams.spec.whatwg.org/#ts-writable
GC::Ref<WritableStream> writable() { return *m_writable; }
void set_writable(WritableStream& writable) { m_writable = writable; }
Optional<bool> backpressure() const { return m_backpressure; }
void set_backpressure(Optional<bool> value) { m_backpressure = move(value); }
GC::Ptr<WebIDL::Promise> backpressure_change_promise() const { return m_backpressure_change_promise; }
void set_backpressure_change_promise(GC::Ptr<WebIDL::Promise> value) { m_backpressure_change_promise = value; }
GC::Ptr<TransformStreamDefaultController> controller() const { return m_controller; }
void set_controller(GC::Ptr<TransformStreamDefaultController> value) { m_controller = value; }
void set_up(GC::Ref<TransformAlgorithm>, GC::Ptr<FlushAlgorithm> = {}, GC::Ptr<CancelAlgorithm> = {});
void enqueue(JS::Value chunk);
// ^Transferable
virtual WebIDL::ExceptionOr<void> transfer_steps(HTML::TransferDataEncoder&) override;
virtual WebIDL::ExceptionOr<void> transfer_receiving_steps(HTML::TransferDataDecoder&) override;
virtual HTML::TransferType primary_interface() const override { return HTML::TransferType::TransformStream; }
private:
explicit TransformStream(JS::Realm& realm);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
// https://streams.spec.whatwg.org/#transformstream-backpressure
// Whether there was backpressure on [[readable]] the last time it was observed
Optional<bool> m_backpressure { false };
// https://streams.spec.whatwg.org/#transformstream-backpressurechangepromise
// A promise which is fulfilled and replaced every time the value of [[backpressure]] changes
GC::Ptr<WebIDL::Promise> m_backpressure_change_promise;
// https://streams.spec.whatwg.org/#transformstream-controller
// A TransformStreamDefaultController created with the ability to control [[readable]] and [[writable]]
GC::Ptr<TransformStreamDefaultController> m_controller;
// https://streams.spec.whatwg.org/#transformstream-readable
// The ReadableStream instance controlled by this object
GC::Ptr<ReadableStream> m_readable;
// https://streams.spec.whatwg.org/#transformstream-writable
// The WritableStream instance controlled by this object
GC::Ptr<WritableStream> m_writable;
};
}