diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 776dddb412..a6eb47dc77 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -187,6 +187,20 @@ bool readable_stream_has_default_reader(ReadableStream const& stream) return false; } +// https://streams.spec.whatwg.org/#make-size-algorithm-from-size-function +SizeAlgorithm extract_size_algorithm(QueuingStrategy const& strategy) +{ + // 1. If strategy["size"] does not exist, return an algorithm that returns 1. + if (!strategy.size) + return [](auto const&) { return JS::normal_completion(JS::Value(1)); }; + + // 2. Return an algorithm that performs the following steps, taking a chunk argument: + return [strategy](auto const& chunk) { + // 1. Return the result of invoking strategy["size"] with argument list « chunk ». + return WebIDL::invoke_callback(*strategy.size, JS::js_undefined(), chunk); + }; +} + // https://streams.spec.whatwg.org/#validate-and-normalize-high-water-mark WebIDL::ExceptionOr extract_high_water_mark(QueuingStrategy const& strategy, double default_hwm) { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index 1e1e1281a5..5b9949a746 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -28,6 +28,7 @@ using WriteAlgorithm = JS::SafeFunction> acquire_readable_stream_default_reader(ReadableStream&); bool is_readable_stream_locked(ReadableStream const&); +SizeAlgorithm extract_size_algorithm(QueuingStrategy const&); WebIDL::ExceptionOr extract_high_water_mark(QueuingStrategy const&, double default_hwm); void readable_stream_close(ReadableStream&);