From 641b9edd89328b780c027c8dc60cafaa0d5b21c3 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Fri, 7 Jul 2023 22:16:40 +0200 Subject: [PATCH] LibWeb: Add AO transform_stream_default_sink_close_algorithm() --- .../LibWeb/Streams/AbstractOperations.cpp | 44 +++++++++++++++++++ .../LibWeb/Streams/AbstractOperations.h | 1 + 2 files changed, 45 insertions(+) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 951c187218..b4e379d3ed 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -2857,6 +2857,50 @@ WebIDL::ExceptionOr> transform_stream_default_ return WebIDL::create_resolved_promise(realm, JS::js_undefined()); } +// https://streams.spec.whatwg.org/#transform-stream-default-sink-close-algorithm +WebIDL::ExceptionOr> transform_stream_default_sink_close_algorithm(TransformStream& stream) +{ + auto& realm = stream.realm(); + + // 1. Let readable be stream.[[readable]]. + auto readable = stream.readable(); + + // 2. Let controller be stream.[[controller]]. + auto controller = stream.controller(); + + // 3. Let flushPromise be the result of performing controller.[[flushAlgorithm]]. + auto flush_promise = TRY((*controller->flush_algorithm())()); + + // 4. Perform ! TransformStreamDefaultControllerClearAlgorithms(controller). + transform_stream_default_controller_clear_algorithms(*controller); + + // 5. Return the result of reacting to flushPromise: + auto react_result = WebIDL::react_to_promise( + *flush_promise, + // 1. If flushPromise was fulfilled, then: + [readable](auto const&) -> WebIDL::ExceptionOr { + // 1. If readable.[[state]] is "errored", throw readable.[[storedError]]. + if (readable->state() == ReadableStream::State::Errored) + return JS::throw_completion(readable->stored_error()); + + VERIFY(readable->controller().has_value() && readable->controller()->has>()); + // 2. Perform ! ReadableStreamDefaultControllerClose(readable.[[controller]]). + readable_stream_default_controller_close(readable->controller().value().get>()); + + return JS::js_undefined(); + }, + // 2. If flushPromise was rejected with reason r, then: + [&stream, readable](auto const& reason) -> WebIDL::ExceptionOr { + // 1. Perform ! TransformStreamError(stream, r). + TRY(transform_stream_error(stream, reason)); + + // 2. Throw readable.[[storedError]]. + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY(readable->stored_error().as_string().utf8_string_view()) }; + }); + + return WebIDL::create_resolved_promise(realm, react_result); +} + // https://streams.spec.whatwg.org/#transform-stream-default-sink-write-algorithm WebIDL::ExceptionOr> transform_stream_default_sink_write_algorithm(TransformStream& stream, JS::Value chunk) { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index 6590ea0f33..3484f4db86 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -139,6 +139,7 @@ WebIDL::ExceptionOr transform_stream_default_controller_error(TransformStr WebIDL::ExceptionOr transform_stream_default_controller_terminate(TransformStreamDefaultController&); WebIDL::ExceptionOr> transform_stream_default_controller_perform_transform(TransformStreamDefaultController&, JS::Value chunk); WebIDL::ExceptionOr> transform_stream_default_sink_abort_algorithm(TransformStream&, JS::Value reason); +WebIDL::ExceptionOr> transform_stream_default_sink_close_algorithm(TransformStream&); WebIDL::ExceptionOr> transform_stream_default_sink_write_algorithm(TransformStream&, JS::Value chunk); WebIDL::ExceptionOr transform_stream_error(TransformStream&, JS::Value error); WebIDL::ExceptionOr transform_stream_error_writable_and_unblock_write(TransformStream&, JS::Value error);