/* * Copyright (c) 2022, Linus Groh * Copyright (c) 2023, Matthew Olsson * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Web::Streams { // https://streams.spec.whatwg.org/#acquire-readable-stream-reader WebIDL::ExceptionOr> acquire_readable_stream_default_reader(ReadableStream& stream) { auto& realm = stream.realm(); // 1. Let reader be a new ReadableStreamDefaultReader. auto reader = TRY(realm.heap().allocate(realm, realm)); // 2. Perform ? SetUpReadableStreamDefaultReader(reader, stream). TRY(set_up_readable_stream_default_reader(reader, stream)); // 3. Return reader. return reader; } // https://streams.spec.whatwg.org/#is-readable-stream-locked bool is_readable_stream_locked(ReadableStream const& stream) { // 1. If stream.[[reader]] is undefined, return false. if (!stream.reader()) return false; // 2. Return true. return true; } // https://streams.spec.whatwg.org/#readable-stream-cancel WebIDL::ExceptionOr> readable_stream_cancel(ReadableStream& stream, JS::Value reason) { auto& realm = stream.realm(); // 1. Set stream.[[disturbed]] to true. stream.set_disturbed(true); // 2. If stream.[[state]] is "closed", return a promise resolved with undefined. if (stream.is_closed()) return WebIDL::create_resolved_promise(realm, JS::js_undefined()); // 3. If stream.[[state]] is "errored", return a promise rejected with stream.[[storedError]]. if (stream.is_errored()) return WebIDL::create_rejected_promise(realm, stream.stored_error()); // 4. Perform ! ReadableStreamClose(stream). readable_stream_close(stream); // 5. Let reader be stream.[[reader]]. auto reader = stream.reader(); // FIXME: // 6. If reader is not undefined and reader implements ReadableStreamBYOBReader, // 1. Let readIntoRequests be reader.[[readIntoRequests]]. // 2. Set reader.[[readIntoRequests]] to an empty list. // 3. For each readIntoRequest of readIntoRequests, // 1. Perform readIntoRequest’s close steps, given undefined. (void)reader; // 7. Let sourceCancelPromise be ! stream.[[controller]].[[CancelSteps]](reason). auto source_cancel_promise = MUST(stream.controller()->cancel_steps(reason)); // 8. Return the result of reacting to sourceCancelPromise with a fulfillment step that returns undefined. auto react_result = WebIDL::react_to_promise(*source_cancel_promise, [](auto const&) -> WebIDL::ExceptionOr { return JS::js_undefined(); }, {}); return WebIDL::create_resolved_promise(realm, react_result); } // https://streams.spec.whatwg.org/#readable-stream-fulfill-read-request void readable_stream_fulfill_read_request(ReadableStream& stream, JS::Value chunk, bool done) { // 1. Assert: ! ReadableStreamHasDefaultReader(stream) is true. VERIFY(readable_stream_has_default_reader(stream)); // 2. Let reader be stream.[[reader]]. auto& reader = *stream.reader(); // 3. Assert: reader.[[readRequests]] is not empty. VERIFY(!reader.read_requests().is_empty()); // 4. Let readRequest be reader.[[readRequests]][0]. // 5. Remove readRequest from reader.[[readRequests]]. auto read_request = reader.read_requests().take_first(); // 6. If done is true, perform readRequest’s close steps. if (done) { read_request->on_close(); } // 7. Otherwise, perform readRequest’s chunk steps, given chunk. else { read_request->on_chunk(chunk); } } // https://streams.spec.whatwg.org/#readable-stream-get-num-read-requests size_t readable_stream_get_num_read_requests(ReadableStream& stream) { // 1. Assert: ! ReadableStreamHasDefaultReader(stream) is true. VERIFY(readable_stream_has_default_reader(stream)); // 2. Return stream.[[reader]].[[readRequests]]'s size. return stream.reader()->read_requests().size(); } // https://streams.spec.whatwg.org/#readable-stream-has-default-reader bool readable_stream_has_default_reader(ReadableStream& stream) { // 1. Let reader be stream.[[reader]]. auto reader = stream.reader(); // 2. If reader is undefined, return false. if (!reader) return false; // 3. If reader implements ReadableStreamDefaultReader, return true. if (reader->is_default_reader()) return true; // 4. Return false. return false; } // https://streams.spec.whatwg.org/#readable-stream-close void readable_stream_close(ReadableStream& stream) { auto& realm = stream.realm(); // 1. Assert: stream.[[state]] is "readable". VERIFY(stream.is_readable()); // 2. Set stream.[[state]] to "closed". stream.set_stream_state(ReadableStream::State::Closed); // 3. Let reader be stream.[[reader]]. auto reader = stream.reader(); // 4. If reader is undefined, return. if (!reader) return; // 5. Resolve reader.[[closedPromise]] with undefined. WebIDL::resolve_promise(realm, *reader->closed_promise_capability()); // 6. If reader implements ReadableStreamDefaultReader, if (reader->is_default_reader()) { // 1. Let readRequests be reader.[[readRequests]]. // 2. Set reader.[[readRequests]] to an empty list. auto read_requests = move(reader->read_requests()); // 3. For each readRequest of readRequests, for (auto& read_request : read_requests) { // 1. Perform readRequest’s close steps. read_request->on_close(); } } } // https://streams.spec.whatwg.org/#readable-stream-error void readable_stream_error(ReadableStream& stream, JS::Value error) { auto& realm = stream.realm(); // 1. Assert: stream.[[state]] is "readable". VERIFY(stream.is_readable()); // 2. Set stream.[[state]] to "errored". stream.set_stream_state(ReadableStream::State::Errored); // 3. Set stream.[[storedError]] to e. stream.set_stored_error(error); // 4. Let reader be stream.[[reader]]. auto reader = stream.reader(); // 5. If reader is undefined, return. if (!reader) return; // 6. Reject reader.[[closedPromise]] with e. WebIDL::reject_promise(realm, *reader->closed_promise_capability(), error); // 7. Set reader.[[closedPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*reader->closed_promise_capability()); // 8. If reader implements ReadableStreamDefaultReader, if (reader->is_default_reader()) { // 1. Perform ! ReadableStreamDefaultReaderErrorReadRequests(reader, e). readable_stream_default_reader_error_read_requests(*reader, error); } // 9. Otherwise, else { // 1. Assert: reader implements ReadableStreamBYOBReader. // 2. Perform ! ReadableStreamBYOBReaderErrorReadIntoRequests(reader, e). // FIXME: Handle BYOBReader TODO(); } } // https://streams.spec.whatwg.org/#readable-stream-add-read-request void readable_stream_add_read_request(ReadableStream& stream, ReadRequest const& read_request) { // FIXME: Check implementation type // 1. Assert: stream.[[reader]] implements ReadableStreamDefaultReader. VERIFY(stream.reader()); // 2. Assert: stream.[[state]] is "readable". VERIFY(stream.is_readable()); // 3. Append readRequest to stream.[[reader]].[[readRequests]]. stream.reader()->read_requests().append(read_request); } // https://streams.spec.whatwg.org/#readable-stream-reader-generic-cancel JS::NonnullGCPtr readable_stream_reader_generic_cancel(ReadableStreamGenericReaderMixin& reader, JS::Value reason) { // 1. Let stream be reader.[[stream]] auto stream = reader.stream(); // 2. Assert: stream is not undefined VERIFY(stream); // 3. Return ! ReadableStreamCancel(stream, reason) return MUST(readable_stream_cancel(*stream, reason)); } // https://streams.spec.whatwg.org/#readable-stream-reader-generic-initialize void readable_stream_reader_generic_initialize(ReadableStreamGenericReaderMixin& reader, ReadableStream& stream) { auto& realm = stream.realm(); // 1. Set reader.[[stream]] to stream. reader.set_stream(stream); // 2. Set stream.[[reader]] to reader. if (reader.is_default_reader()) { stream.set_reader(static_cast(reader)); } else { // FIXME: Handle other descendents of ReadableStreamGenericReaderMixin (i.e. BYOBReader) TODO(); } // 3. If stream.[[state]] is "readable", if (stream.is_readable()) { // 1. Set reader.[[closedPromise]] to a new promise. reader.set_closed_promise_capability(WebIDL::create_promise(realm)); } // 4. Otherwise, if stream.[[state]] is "closed", else if (stream.is_closed()) { // 1. Set reader.[[closedPromise]] to a promise resolved with undefined. reader.set_closed_promise_capability(WebIDL::create_resolved_promise(realm, JS::js_undefined())); } // 5. Otherwise, else { // 1. Assert: stream.[[state]] is "errored". VERIFY(stream.is_errored()); // 2. Set reader.[[closedPromise]] to a promise rejected with stream.[[storedError]]. reader.set_closed_promise_capability(WebIDL::create_rejected_promise(realm, stream.stored_error())); // 3. Set reader.[[closedPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*reader.closed_promise_capability()); } } // https://streams.spec.whatwg.org/#readable-stream-reader-generic-release WebIDL::ExceptionOr readable_stream_reader_generic_release(ReadableStreamGenericReaderMixin& reader) { // 1. Let stream be reader.[[stream]]. auto stream = reader.stream(); // 2. Assert: stream is not undefined. VERIFY(stream); // 3. Assert: stream.[[reader]] is reader. VERIFY(stream->reader().ptr() == &reader); // 4. If stream.[[state]] is "readable", reject reader.[[closedPromise]] with a TypeError exception. auto exception = TRY(JS::TypeError::create(stream->realm(), "Released readable stream"sv)); if (stream->is_readable()) { WebIDL::reject_promise(stream->realm(), *reader.closed_promise_capability(), exception); } // 5. Otherwise, set reader.[[closedPromise]] to a promise rejected with a TypeError exception. else { reader.set_closed_promise_capability(WebIDL::create_rejected_promise(stream->realm(), exception)); } // 6. Set reader.[[closedPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*reader.closed_promise_capability()); // 7. Perform ! stream.[[controller]].[[ReleaseSteps]](). stream->controller()->release_steps(); // 8. Set stream.[[reader]] to undefined. stream->set_reader({}); // 9. Set reader.[[stream]] to undefined. reader.set_stream({}); return {}; } // https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaultreadererrorreadrequests void readable_stream_default_reader_error_read_requests(ReadableStreamDefaultReader& reader, JS::Value error) { // 1. Let readRequests be reader.[[readRequests]]. // 2. Set reader.[[readRequests]] to a new empty list. auto read_requests = move(reader.read_requests()); // 3. For each readRequest of readRequests, for (auto& read_request : read_requests) { // 1. Perform readRequest’s error steps, given e. read_request->on_error(error); } } // https://streams.spec.whatwg.org/#readable-stream-default-reader-read void readable_stream_default_reader_read(ReadableStreamDefaultReader& reader, ReadRequest& read_request) { // 1. Let stream be reader.[[stream]]. auto stream = reader.stream(); // 2. Assert: stream is not undefined. VERIFY(stream); // 3. Set stream.[[disturbed]] to true. stream->set_disturbed(true); // 4. If stream.[[state]] is "closed", perform readRequest’s close steps. if (stream->is_closed()) { read_request.on_close(); } // 5. Otherwise, if stream.[[state]] is "errored", perform readRequest’s error steps given stream.[[storedError]]. else if (stream->is_errored()) { read_request.on_error(stream->stored_error()); } // 6. Otherwise, else { // 1. Assert: stream.[[state]] is "readable". VERIFY(stream->is_readable()); // 2. Perform ! stream.[[controller]].[[PullSteps]](readRequest). MUST(stream->controller()->pull_steps(read_request)); } } // https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaultreaderrelease WebIDL::ExceptionOr readable_stream_default_reader_release(ReadableStreamDefaultReader& reader) { // 1. Perform ! ReadableStreamReaderGenericRelease(reader). TRY(readable_stream_reader_generic_release(reader)); // 2. Let e be a new TypeError exception. auto e = TRY(JS::TypeError::create(reader.realm(), "Reader has been released"sv)); // 3. Perform ! ReadableStreamDefaultReaderErrorReadRequests(reader, e). readable_stream_default_reader_error_read_requests(reader, e); return {}; } // https://streams.spec.whatwg.org/#set-up-readable-stream-default-reader WebIDL::ExceptionOr set_up_readable_stream_default_reader(ReadableStreamDefaultReader& reader, ReadableStream& stream) { // 1. If ! IsReadableStreamLocked(stream) is true, throw a TypeError exception. if (is_readable_stream_locked(stream)) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot create stream reader for a locked stream"sv }; // 2. Perform ! ReadableStreamReaderGenericInitialize(reader, stream). // 3. Set reader.[[readRequests]] to a new empty list. readable_stream_reader_generic_initialize(reader, stream); return {}; } // https://streams.spec.whatwg.org/#readable-stream-default-controller-close void readable_stream_default_controller_close(ReadableStreamDefaultController& controller) { // 1. If ! ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) is false, return. if (!readable_stream_default_controller_can_close_or_enqueue(controller)) return; // 2. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 3. Set controller.[[closeRequested]] to true. controller.set_close_requested(true); // 4. If controller.[[queue]] is empty, if (controller.queue().is_empty()) { // 1. Perform ! ReadableStreamDefaultControllerClearAlgorithms(controller). readable_stream_default_controller_clear_algorithms(controller); // 2. Perform ! ReadableStreamClose(stream). readable_stream_close(*stream); } } // https://streams.spec.whatwg.org/#readable-stream-default-controller-enqueue WebIDL::ExceptionOr readable_stream_default_controller_enqueue(ReadableStreamDefaultController& controller, JS::Value chunk) { auto& vm = controller.vm(); // 1. If ! ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) is false, return. if (!readable_stream_default_controller_can_close_or_enqueue(controller)) return {}; // 2. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 3. If ! IsReadableStreamLocked(stream) is true and ! ReadableStreamGetNumReadRequests(stream) > 0, perform ! ReadableStreamFulfillReadRequest(stream, chunk, false). if (is_readable_stream_locked(*stream) && readable_stream_get_num_read_requests(*stream) > 0) { readable_stream_fulfill_read_request(*stream, chunk, false); } // 4. Otherwise, else { // 1. Let result be the result of performing controller.[[strategySizeAlgorithm]], passing in chunk, and interpreting the result as a completion record. auto result = (*controller.strategy_size_algorithm())(chunk); // 2. If result is an abrupt completion, if (result.is_abrupt()) { // 1. Perform ! ReadableStreamDefaultControllerError(controller, result.[[Value]]). readable_stream_default_controller_error(controller, result.value().value()); // 2. Return result. return result; } // 3. Let chunkSize be result.[[Value]]. auto chunk_size = TRY(result.release_value().release_value().to_double(vm)); // 4. Let enqueueResult be EnqueueValueWithSize(controller, chunk, chunkSize). auto enqueue_result = enqueue_value_with_size(controller, chunk, chunk_size); // 5. If enqueueResult is an abrupt completion, if (enqueue_result.is_error()) { auto throw_completion = Bindings::throw_dom_exception_if_needed(vm, [&] { return enqueue_result; }).throw_completion(); // 1. Perform ! ReadableStreamDefaultControllerError(controller, enqueueResult.[[Value]]). readable_stream_default_controller_error(controller, throw_completion.value().value()); // 2. Return enqueueResult. return enqueue_result; } } // 5. Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller). return readable_stream_default_controller_can_pull_if_needed(controller); } // https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed WebIDL::ExceptionOr readable_stream_default_controller_can_pull_if_needed(ReadableStreamDefaultController& controller) { // 1. Let shouldPull be ! ReadableStreamDefaultControllerShouldCallPull(controller). auto should_pull = readable_stream_default_controller_should_call_pull(controller); // 2. If shouldPull is false, return. if (!should_pull) return {}; // 3. If controller.[[pulling]] is true, if (controller.pulling()) { // 1. Set controller.[[pullAgain]] to true. controller.set_pull_again(true); // 2. Return. return {}; } // 4. Assert: controller.[[pullAgain]] is false. VERIFY(!controller.pull_again()); // 5. Set controller.[[pulling]] to true. controller.set_pulling(true); // 6. Let pullPromise be the result of performing controller.[[pullAlgorithm]]. auto pull_promise = TRY((*controller.pull_algorithm())()); // 7. Upon fulfillment of pullPromise, WebIDL::upon_fulfillment(*pull_promise, [&](auto const&) -> WebIDL::ExceptionOr { // 1. Set controller.[[pulling]] to false. controller.set_pulling(false); // 2. If controller.[[pullAgain]] is true, if (controller.pull_again()) { // 1. Set controller.[[pullAgain]] to false. controller.set_pull_again(false); // 2. Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller). TRY(readable_stream_default_controller_can_pull_if_needed(controller)); } return JS::js_undefined(); }); // 8. Upon rejection of pullPromise with reason e, WebIDL::upon_rejection(*pull_promise, [&](auto const& e) -> WebIDL::ExceptionOr { // 1. Perform ! ReadableStreamDefaultControllerError(controller, e). readable_stream_default_controller_error(controller, e); return JS::js_undefined(); }); return {}; } // https://streams.spec.whatwg.org/#readable-stream-default-controller-should-call-pull bool readable_stream_default_controller_should_call_pull(ReadableStreamDefaultController& controller) { // 1. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 2. If ! ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) is false, return false. if (!readable_stream_default_controller_can_close_or_enqueue(controller)) return false; // 3. If controller.[[started]] is false, return false. if (!controller.started()) return false; // 4. If ! IsReadableStreamLocked(stream) is true and ! ReadableStreamGetNumReadRequests(stream) > 0, return true. if (is_readable_stream_locked(*stream) && readable_stream_get_num_read_requests(*stream) > 0) return true; // 5. Let desiredSize be ! ReadableStreamDefaultControllerGetDesiredSize(controller). auto desired_size = readable_stream_default_controller_get_desired_size(controller); // 6. Assert: desiredSize is not null. VERIFY(desired_size.has_value()); // 7. If desiredSize > 0, return true. if (desired_size.release_value() > 0) return true; // 8. Return false. return false; } // https://streams.spec.whatwg.org/#readable-stream-default-controller-clear-algorithms void readable_stream_default_controller_clear_algorithms(ReadableStreamDefaultController& controller) { // 1. Set controller.[[pullAlgorithm]] to undefined. controller.set_pull_algorithm({}); // 2. Set controller.[[cancelAlgorithm]] to undefined. controller.set_cancel_algorithm({}); // 3. Set controller.[[strategySizeAlgorithm]] to undefined. controller.set_strategy_size_algorithm({}); } // https://streams.spec.whatwg.org/#readable-stream-default-controller-error void readable_stream_default_controller_error(ReadableStreamDefaultController& controller, JS::Value error) { // 1. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 2. If stream.[[state]] is not "readable", return. if (!stream->is_readable()) return; // 3. Perform ! ResetQueue(controller). reset_queue(controller); // 4. Perform ! ReadableStreamDefaultControllerClearAlgorithms(controller). readable_stream_default_controller_clear_algorithms(controller); // 5. Perform ! ReadableStreamError(stream, e). readable_stream_error(*stream, error); } // https://streams.spec.whatwg.org/#readable-stream-default-controller-get-desired-size Optional readable_stream_default_controller_get_desired_size(ReadableStreamDefaultController& controller) { auto stream = controller.stream(); // 1. Let state be controller.[[stream]].[[state]]. // 2. If state is "errored", return null. if (stream->is_errored()) return {}; // 3. If state is "closed", return 0. if (stream->is_closed()) return 0.0f; // 4. Return controller.[[strategyHWM]] − controller.[[queueTotalSize]]. return controller.strategy_hwm() - controller.queue_total_size(); } // https://streams.spec.whatwg.org/#readable-stream-default-controller-can-close-or-enqueue bool readable_stream_default_controller_can_close_or_enqueue(ReadableStreamDefaultController& controller) { // 1. Let state be controller.[[stream]].[[state]]. // 2. If controller.[[closeRequested]] is false and state is "readable", return true. // 3. Otherwise, return false. return !controller.close_requested() && controller.stream()->is_readable(); } // https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller WebIDL::ExceptionOr set_up_readable_stream_default_controller(ReadableStream& stream, ReadableStreamDefaultController& controller, StartAlgorithm&& start_algorithm, PullAlgorithm&& pull_algorithm, CancelAlgorithm&& cancel_algorithm, double high_water_mark, SizeAlgorithm&& size_algorithm) { auto& realm = stream.realm(); // 1. Assert: stream.[[controller]] is undefined. VERIFY(!stream.controller()); // 2. Set controller.[[stream]] to stream. controller.set_stream(stream); // 3. Perform ! ResetQueue(controller). reset_queue(controller); // 4. Set controller.[[started]], controller.[[closeRequested]], controller.[[pullAgain]], and controller.[[pulling]] to false. controller.set_started(false); controller.set_close_requested(false); controller.set_pull_again(false); controller.set_pulling(false); // 5. Set controller.[[strategySizeAlgorithm]] to sizeAlgorithm and controller.[[strategyHWM]] to highWaterMark. controller.set_strategy_size_algorithm(move(size_algorithm)); controller.set_strategy_hwm(high_water_mark); // 6. Set controller.[[pullAlgorithm]] to pullAlgorithm. controller.set_pull_algorithm(move(pull_algorithm)); // 7. Set controller.[[cancelAlgorithm]] to cancelAlgorithm. controller.set_cancel_algorithm(move(cancel_algorithm)); // 8. Set stream.[[controller]] to controller. stream.set_controller(controller); // 9. Let startResult be the result of performing startAlgorithm. (This might throw an exception.) auto start_result = TRY(start_algorithm()); // 10. Let startPromise be a promise resolved with startResult. auto start_promise = WebIDL::create_resolved_promise(realm, start_result ? start_result->promise() : JS::js_undefined()); // 11. Upon fulfillment of startPromise, WebIDL::upon_fulfillment(start_promise, [&](auto const&) -> WebIDL::ExceptionOr { // 1. Set controller.[[started]] to true. controller.set_started(true); // 2. Assert: controller.[[pulling]] is false. VERIFY(!controller.pulling()); // 3. Assert: controller.[[pullAgain]] is false. VERIFY(!controller.pull_again()); // 4. Perform ! ReadableStreamDefaultControllerCallPullIfNeeded(controller). TRY(readable_stream_default_controller_can_pull_if_needed(controller)); return JS::js_undefined(); }); // 12. Upon rejection of startPromise with reason r, WebIDL::upon_rejection(start_promise, [&](auto const& r) -> WebIDL::ExceptionOr { // 1. Perform ! ReadableStreamDefaultControllerError(controller, r). readable_stream_default_controller_error(controller, r); return JS::js_undefined(); }); return {}; } // https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller-from-underlying-source WebIDL::ExceptionOr set_up_readable_stream_default_controller_from_underlying_source(ReadableStream& stream, JS::Value underlying_source_value, UnderlyingSource underlying_source, double high_water_mark, SizeAlgorithm&& size_algorithm) { auto& realm = stream.realm(); // 1. Let controller be a new ReadableStreamDefaultController. auto controller = MUST_OR_THROW_OOM(stream.heap().allocate(realm, realm)); // 2. Let startAlgorithm be an algorithm that returns undefined. StartAlgorithm start_algorithm = [] { return JS::GCPtr {}; }; // 3. Let pullAlgorithm be an algorithm that returns a promise resolved with undefined. PullAlgorithm pull_algorithm = [&realm]() { return WebIDL::create_resolved_promise(realm, JS::js_undefined()); }; // 4. Let cancelAlgorithm be an algorithm that returns a promise resolved with undefined. CancelAlgorithm cancel_algorithm = [&realm](auto const&) { return WebIDL::create_resolved_promise(realm, JS::js_undefined()); }; // 5. If underlyingSourceDict["start"] exists, then set startAlgorithm to an algorithm which returns the result of invoking underlyingSourceDict["start"] with argument list « controller » and callback this value underlyingSource. if (underlying_source.start) { start_algorithm = [&, start = underlying_source.start]() -> WebIDL::ExceptionOr> { auto result = TRY(WebIDL::invoke_callback(*start, underlying_source_value, controller)).release_value(); return WebIDL::create_resolved_promise(realm, result); }; } // 6. If underlyingSourceDict["pull"] exists, then set pullAlgorithm to an algorithm which returns the result of invoking underlyingSourceDict["pull"] with argument list « controller » and callback this value underlyingSource. if (underlying_source.pull) { pull_algorithm = [&, pull = underlying_source.pull]() -> WebIDL::ExceptionOr> { auto result = TRY(WebIDL::invoke_callback(*pull, underlying_source_value, controller)).release_value(); return WebIDL::create_resolved_promise(realm, result); }; } // 7. If underlyingSourceDict["cancel"] exists, then set cancelAlgorithm to an algorithm which takes an argument reason and returns the result of invoking underlyingSourceDict["cancel"] with argument list « reason » and callback this value underlyingSource. if (underlying_source.cancel) { cancel_algorithm = [&, cancel = underlying_source.cancel](auto const& reason) -> WebIDL::ExceptionOr> { auto result = TRY(WebIDL::invoke_callback(*cancel, underlying_source_value, reason)).release_value(); return WebIDL::create_resolved_promise(realm, result); }; } // 8. Perform ? SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm). return set_up_readable_stream_default_controller(stream, controller, move(start_algorithm), move(pull_algorithm), move(cancel_algorithm), high_water_mark, move(size_algorithm)); } // https://streams.spec.whatwg.org/#acquire-writable-stream-default-writer WebIDL::ExceptionOr> acquire_writable_stream_default_writer(WritableStream& stream) { auto& realm = stream.realm(); // 1. Let writer be a new WritableStreamDefaultWriter. auto writer = MUST_OR_THROW_OOM(stream.heap().allocate(realm, realm)); // 2. Perform ? SetUpWritableStreamDefaultWriter(writer, stream). TRY(set_up_writable_stream_default_writer(*writer, stream)); // 3. Return writer. return writer; } // https://streams.spec.whatwg.org/#is-writable-stream-locked bool is_writable_stream_locked(WritableStream const& stream) { // 1. If stream.[[writer]] is undefined, return false. if (!stream.writer()) return false; // 2. Return true. return true; } // https://streams.spec.whatwg.org/#set-up-writable-stream-default-writer WebIDL::ExceptionOr set_up_writable_stream_default_writer(WritableStreamDefaultWriter& writer, WritableStream& stream) { auto& realm = writer.realm(); // 1. If ! IsWritableStreamLocked(stream) is true, throw a TypeError exception. if (is_writable_stream_locked(stream)) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Stream is locked"sv }; // 2. Set writer.[[stream]] to stream. writer.set_stream(stream); // 3. Set stream.[[writer]] to writer. stream.set_writer(writer); // 4. Let state be stream.[[state]]. auto state = stream.state(); // 5. If state is "writable", if (state == WritableStream::State::Writable) { // 1. If ! WritableStreamCloseQueuedOrInFlight(stream) is false and stream.[[backpressure]] is true, set writer.[[readyPromise]] to a new promise. if (!writable_stream_close_queued_or_in_flight(stream) && stream.backpressure()) { writer.set_ready_promise(WebIDL::create_promise(realm)); } // 2. Otherwise, set writer.[[readyPromise]] to a promise resolved with undefined. else { writer.set_ready_promise(WebIDL::create_resolved_promise(realm, JS::js_undefined())); } // 3. Set writer.[[closedPromise]] to a new promise. writer.set_closed_promise(WebIDL::create_promise(realm)); } // 6. Otherwise, if state is "erroring", else if (state == WritableStream::State::Erroring) { // 1. Set writer.[[readyPromise]] to a promise rejected with stream.[[storedError]]. writer.set_ready_promise(WebIDL::create_rejected_promise(realm, stream.stored_error())); // 2. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*writer.ready_promise()); // 3. Set writer.[[closedPromise]] to a new promise. writer.set_closed_promise(WebIDL::create_promise(realm)); } // 7. Otherwise, if state is "closed", else if (state == WritableStream::State::Closed) { // 1. Set writer.[[readyPromise]] to a promise resolved with undefined. writer.set_ready_promise(WebIDL::create_resolved_promise(realm, JS::js_undefined())); // 2. Set writer.[[closedPromise]] to a promise resolved with undefined. writer.set_closed_promise(WebIDL::create_resolved_promise(realm, JS::js_undefined())); } // 8. Otherwise, else { // 1. Assert: state is "errored". VERIFY(state == WritableStream::State::Errored); // 2. Let storedError be stream.[[storedError]]. auto stored_error = stream.stored_error(); // 3. Set writer.[[readyPromise]] to a promise rejected with storedError. writer.set_ready_promise(WebIDL::create_rejected_promise(realm, stored_error)); // 4. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*writer.ready_promise()); // 5. Set writer.[[closedPromise]] to a promise rejected with storedError. writer.set_closed_promise(WebIDL::create_rejected_promise(realm, stored_error)); // 6. Set writer.[[closedPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*writer.closed_promise()); } return {}; } // https://streams.spec.whatwg.org/#writable-stream-close WebIDL::ExceptionOr> writable_stream_close(WritableStream& stream) { auto& realm = stream.realm(); // 1. Let state be stream.[[state]]. auto state = stream.state(); // 2. If state is "closed" or "errored", return a promise rejected with a TypeError exception. if (state == WritableStream::State::Closed || state == WritableStream::State::Errored) { auto message = state == WritableStream::State::Closed ? "Cannot close a closed stream"sv : "Cannot close an errored stream"sv; auto exception = MUST_OR_THROW_OOM(JS::TypeError::create(realm, message)); return WebIDL::create_rejected_promise(realm, exception); } // 3. Assert: state is "writable" or "erroring". VERIFY(state == WritableStream::State::Writable || state == WritableStream::State::Erroring); // 4. Assert: ! WritableStreamCloseQueuedOrInFlight(stream) is false. VERIFY(!writable_stream_close_queued_or_in_flight(stream)); // 5. Let promise be a new promise. auto promise = WebIDL::create_promise(realm); // 6. Set stream.[[closeRequest]] to promise. stream.set_close_request(promise); // 7. Let writer be stream.[[writer]]. auto writer = stream.writer(); // 8. If writer is not undefined, and stream.[[backpressure]] is true, and state is "writable", resolve writer.[[readyPromise]] with undefined. if (writer && stream.backpressure() && state == WritableStream::State::Writable) WebIDL::resolve_promise(realm, *writer->ready_promise(), JS::js_undefined()); // 9. Perform ! WritableStreamDefaultControllerClose(stream.[[controller]]). TRY(writable_stream_default_controller_close(*stream.controller())); // 10. Return promise. return promise; } // https://streams.spec.whatwg.org/#writable-stream-close-queued-or-in-flight bool writable_stream_close_queued_or_in_flight(WritableStream const& stream) { // 1. If stream.[[closeRequest]] is undefined and stream.[[inFlightCloseRequest]] is undefined, return false. if (!stream.close_request() && !stream.in_flight_write_request()) return false; // 2. Return true. return true; } // https://streams.spec.whatwg.org/#writable-stream-deal-with-rejection WebIDL::ExceptionOr writable_stream_deal_with_rejection(WritableStream& stream, JS::Value error) { // 1. Let state be stream.[[state]]. auto state = stream.state(); // 2. If state is "writable", if (state == WritableStream::State::Writable) { // 1. Perform ! WritableStreamStartErroring(stream, error). // 2. Return. return writable_stream_start_erroring(stream, error); } // 3. Assert: state is "erroring". VERIFY(state == WritableStream::State::Erroring); // 4. Perform ! WritableStreamFinishErroring(stream). return writable_stream_finish_erroring(stream); } // https://streams.spec.whatwg.org/#writable-stream-finish-erroring WebIDL::ExceptionOr writable_stream_finish_erroring(WritableStream& stream) { auto& realm = stream.realm(); // 1. Assert: stream.[[state]] is "erroring". VERIFY(stream.state() == WritableStream::State::Erroring); // 2. Assert: ! WritableStreamHasOperationMarkedInFlight(stream) is false. VERIFY(!writable_stream_has_operation_marked_in_flight(stream)); // 3. Set stream.[[state]] to "errored". stream.set_state(WritableStream::State::Errored); // 4. Perform ! stream.[[controller]].[[ErrorSteps]](). stream.controller()->error_steps(); // 5. Let storedError be stream.[[storedError]]. auto stored_error = stream.stored_error(); // 6. For each writeRequest of stream.[[writeRequests]]: for (auto& write_request : stream.write_requests()) { // 1. Reject writeRequest with storedError. WebIDL::reject_promise(realm, *write_request, stored_error); } // 7. Set stream.[[writeRequests]] to an empty list. stream.write_requests().clear(); // 8. If stream.[[pendingAbortRequest]] is undefined, if (!stream.pending_abort_request().has_value()) { // 1. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). writable_stream_reject_close_and_closed_promise_if_needed(stream); // 2. Return. return {}; } // 9. Let abortRequest be stream.[[pendingAbortRequest]]. // 10. Set stream.[[pendingAbortRequest]] to undefined. auto abort_request = stream.pending_abort_request().release_value(); // 11. If abortRequest’s was already erroring is true, if (abort_request.was_already_erroring) { // 1. Reject abortRequest’s promise with storedError. WebIDL::reject_promise(realm, abort_request.promise, stored_error); // 2. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). writable_stream_reject_close_and_closed_promise_if_needed(stream); // 3. Return. return {}; } // 12. Let promise be ! stream.[[controller]].[[AbortSteps]](abortRequest’s reason). auto promise = TRY(stream.controller()->abort_steps(abort_request.reason)); // 13. Upon fulfillment of promise, WebIDL::upon_fulfillment(*promise, [&, abort_promise = abort_request.promise](auto const&) -> WebIDL::ExceptionOr { // 1. Resolve abortRequest’s promise with undefined. WebIDL::resolve_promise(realm, abort_promise, JS::js_undefined()); // 2. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). writable_stream_reject_close_and_closed_promise_if_needed(stream); return JS::js_undefined(); }); // 14. Upon rejection of promise with reason reason, WebIDL::upon_rejection(*promise, [&, abort_promise = abort_request.promise](auto const& reason) -> WebIDL::ExceptionOr { // 1. Reject abortRequest’s promise with reason. WebIDL::reject_promise(realm, abort_promise, reason); // 2. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). writable_stream_reject_close_and_closed_promise_if_needed(stream); return JS::js_undefined(); }); return {}; } // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-close void writable_stream_finish_in_flight_close(WritableStream& stream) { auto& realm = stream.realm(); // 1. Assert: stream.[[inFlightCloseRequest]] is not undefined. VERIFY(stream.in_flight_close_request()); // 2. Resolve stream.[[inFlightCloseRequest]] with undefined. WebIDL::resolve_promise(realm, *stream.in_flight_close_request(), JS::js_undefined()); // 3. Set stream.[[inFlightCloseRequest]] to undefined. stream.set_in_flight_close_request({}); // 4. Let state be stream.[[state]]. auto state = stream.state(); // 5. Assert: stream.[[state]] is "writable" or "erroring". VERIFY(state == WritableStream::State::Writable || state == WritableStream::State::Erroring); // 6. If state is "erroring", if (state == WritableStream::State::Erroring) { // 1. Set stream.[[storedError]] to undefined. stream.set_stored_error(JS::js_undefined()); // 2. If stream.[[pendingAbortRequest]] is not undefined, if (stream.pending_abort_request().has_value()) { // 1. Resolve stream.[[pendingAbortRequest]]'s promise with undefined. // 2. Set stream.[[pendingAbortRequest]] to undefined. WebIDL::resolve_promise(realm, stream.pending_abort_request().release_value().promise, JS::js_undefined()); } } // 7. Set stream.[[state]] to "closed". stream.set_state(WritableStream::State::Closed); // 8. Let writer be stream.[[writer]]. auto writer = stream.writer(); // 9. If writer is not undefined, resolve writer.[[closedPromise]] with undefined. if (writer) WebIDL::resolve_promise(realm, *writer->closed_promise(), JS::js_undefined()); // 10. Assert: stream.[[pendingAbortRequest]] is undefined. VERIFY(!stream.pending_abort_request().has_value()); // 11. Assert: stream.[[storedError]] is undefined. VERIFY(stream.stored_error().is_undefined()); } // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-close-with-error WebIDL::ExceptionOr writable_stream_finish_in_flight_close_with_error(WritableStream& stream, JS::Value error) { auto& realm = stream.realm(); // 1. Assert: stream.[[inFlightCloseRequest]] is not undefined. VERIFY(stream.in_flight_close_request()); // 2. Reject stream.[[inFlightCloseRequest]] with error. WebIDL::reject_promise(realm, *stream.in_flight_close_request(), error); // 3. Set stream.[[inFlightCloseRequest]] to undefined. stream.set_in_flight_close_request({}); // 4. Assert: stream.[[state]] is "writable" or "erroring". auto state = stream.state(); VERIFY(state == WritableStream::State::Writable || state == WritableStream::State::Erroring); // 5. If stream.[[pendingAbortRequest]] is not undefined, if (stream.pending_abort_request().has_value()) { // 1. Reject stream.[[pendingAbortRequest]]'s promise with error. // 2. Set stream.[[pendingAbortRequest]] to undefined. WebIDL::reject_promise(realm, stream.pending_abort_request().release_value().promise, error); } // 6. Perform ! WritableStreamDealWithRejection(stream, error). return writable_stream_deal_with_rejection(stream, error); } // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-write void writable_stream_finish_in_flight_write(WritableStream& stream) { auto& realm = stream.realm(); // 1. Assert: stream.[[inFlightWriteRequest]] is not undefined. VERIFY(stream.in_flight_write_request()); // 2. Resolve stream.[[inFlightWriteRequest]] with undefined. WebIDL::resolve_promise(realm, *stream.in_flight_write_request(), JS::js_undefined()); // 3. Set stream.[[inFlightWriteRequest]] to undefined. stream.set_in_flight_write_request({}); } // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-write-with-error WebIDL::ExceptionOr writable_stream_finish_in_flight_write_with_error(WritableStream& stream, JS::Value error) { auto& realm = stream.realm(); // 1. Assert: stream.[[inFlightWriteRequest]] is not undefined. VERIFY(stream.in_flight_write_request()); // 2. Reject stream.[[inFlightWriteRequest]] with error. WebIDL::reject_promise(realm, *stream.in_flight_write_request(), error); // 3. Set stream.[[inFlightWriteRequest]] to undefined. stream.set_in_flight_write_request({}); // 4. Assert: stream.[[state]] is "writable" or "erroring". auto state = stream.state(); VERIFY(state == WritableStream::State::Writable || state == WritableStream::State::Erroring); // 5. Perform ! WritableStreamDealWithRejection(stream, error). return writable_stream_deal_with_rejection(stream, error); } // https://streams.spec.whatwg.org/#writable-stream-has-operation-marked-in-flight bool writable_stream_has_operation_marked_in_flight(WritableStream const& stream) { // 1. If stream.[[inFlightWriteRequest]] is undefined and stream.[[inFlightCloseRequest]] is undefined, return false. if (!stream.in_flight_write_request() && !stream.in_flight_close_request()) return false; // 2. Return true. return true; } // https://streams.spec.whatwg.org/#writable-stream-mark-close-request-in-flight void writable_stream_mark_close_request_in_flight(WritableStream& stream) { // 1. Assert: stream.[[inFlightCloseRequest]] is undefined. VERIFY(!stream.in_flight_close_request()); // 2. Assert: stream.[[closeRequest]] is not undefined. VERIFY(stream.close_request()); // 3. Set stream.[[inFlightCloseRequest]] to stream.[[closeRequest]]. stream.set_in_flight_close_request(stream.close_request()); // 4. Set stream.[[closeRequest]] to undefined. stream.set_close_request({}); } // https://streams.spec.whatwg.org/#writable-stream-mark-first-write-request-in-flight void writable_stream_mark_first_write_request_in_flight(WritableStream& stream) { // 1. Assert: stream.[[inFlightWriteRequest]] is undefined. VERIFY(!stream.in_flight_write_request()); // 2. Assert: stream.[[writeRequests]] is not empty. VERIFY(!stream.write_requests().is_empty()); // 3. Let writeRequest be stream.[[writeRequests]][0]. // 4. Remove writeRequest from stream.[[writeRequests]]. auto write_request = stream.write_requests().take_first(); // 5. Set stream.[[inFlightWriteRequest]] to writeRequest. stream.set_in_flight_write_request(write_request); } // https://streams.spec.whatwg.org/#writable-stream-reject-close-and-closed-promise-if-needed void writable_stream_reject_close_and_closed_promise_if_needed(WritableStream& stream) { auto& realm = stream.realm(); // 1. Assert: stream.[[state]] is "errored". VERIFY(stream.state() == WritableStream::State::Errored); // 2. If stream.[[closeRequest]] is not undefined, if (stream.close_request()) { // 1. Assert: stream.[[inFlightCloseRequest]] is undefined. VERIFY(!stream.in_flight_close_request()); // 2. Reject stream.[[closeRequest]] with stream.[[storedError]]. WebIDL::reject_promise(realm, *stream.close_request(), stream.stored_error()); // 3. Set stream.[[closeRequest]] to undefined. stream.set_close_request({}); } // 3. Let writer be stream.[[writer]]. auto writer = stream.writer(); // 4. If writer is not undefined, if (writer) { // 1. Reject writer.[[closedPromise]] with stream.[[storedError]]. WebIDL::reject_promise(realm, *writer->closed_promise(), stream.stored_error()); // 2. Set writer.[[closedPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*writer->closed_promise()); } } // https://streams.spec.whatwg.org/#writable-stream-start-erroring WebIDL::ExceptionOr writable_stream_start_erroring(WritableStream& stream, JS::Value reason) { // 1. Assert: stream.[[storedError]] is undefined. VERIFY(stream.stored_error().is_undefined()); // 2. Assert: stream.[[state]] is "writable". VERIFY(stream.state() == WritableStream::State::Writable); // 3. Let controller be stream.[[controller]]. auto controller = stream.controller(); // 4. Assert: controller is not undefined. VERIFY(controller); // 5. Set stream.[[state]] to "erroring". stream.set_state(WritableStream::State::Erroring); // 6. Set stream.[[storedError]] to reason. stream.set_stored_error(reason); // 7. Let writer be stream.[[writer]]. auto writer = stream.writer(); // 8. If writer is not undefined, perform ! WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason). if (writer) writable_stream_default_writer_ensure_ready_promise_rejected(*writer, reason); // 9. If ! WritableStreamHasOperationMarkedInFlight(stream) is false and controller.[[started]] is true, perform ! WritableStreamFinishErroring(stream). if (!writable_stream_has_operation_marked_in_flight(stream) && controller->started()) TRY(writable_stream_finish_erroring(stream)); return {}; } // https://streams.spec.whatwg.org/#writable-stream-update-backpressure void writable_stream_update_backpressure(WritableStream& stream, bool backpressure) { auto& realm = stream.realm(); // 1. Assert: stream.[[state]] is "writable". VERIFY(stream.state() == WritableStream::State::Writable); // 2. Assert: ! WritableStreamCloseQueuedOrInFlight(stream) is false. VERIFY(!writable_stream_close_queued_or_in_flight(stream)); // 3. Let writer be stream.[[writer]]. auto writer = stream.writer(); // 4. If writer is not undefined and backpressure is not stream.[[backpressure]], if (writer && backpressure != stream.backpressure()) { // 1. If backpressure is true, set writer.[[readyPromise]] to a new promise. if (backpressure) { writer->set_ready_promise(WebIDL::create_promise(realm)); } // 2. Otherwise, else { // 1. Assert: backpressure is false. // 2. Resolve writer.[[readyPromise]] with undefined. WebIDL::resolve_promise(realm, *writer->ready_promise(), JS::js_undefined()); } } // 5. Set stream.[[backpressure]] to backpressure. stream.set_backpressure(backpressure); } // https://streams.spec.whatwg.org/#writable-stream-default-writer-ensure-ready-promise-rejected void writable_stream_default_writer_ensure_ready_promise_rejected(WritableStreamDefaultWriter& writer, JS::Value error) { auto& realm = writer.realm(); // 1. If writer.[[readyPromise]].[[PromiseState]] is "pending", reject writer.[[readyPromise]] with error. auto& ready_promise = verify_cast(*writer.ready_promise()->promise()); if (ready_promise.state() == JS::Promise::State::Pending) { WebIDL::reject_promise(realm, *writer.ready_promise(), error); } // 2. Otherwise, set writer.[[readyPromise]] to a promise rejected with error. else { writer.set_ready_promise(WebIDL::create_rejected_promise(realm, error)); } // 3. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. WebIDL::mark_promise_as_handled(*writer.ready_promise()); } // https://streams.spec.whatwg.org/#writable-stream-default-writer-get-desired-size Optional writable_stream_default_writer_get_desired_size(WritableStreamDefaultWriter const& writer) { // 1. Let stream be writer.[[stream]]. auto stream = writer.stream(); // 2. Let state be stream.[[state]]. auto state = stream->state(); // 3. If state is "errored" or "erroring", return null. if (state == WritableStream::State::Errored || state == WritableStream::State::Erroring) return {}; // 4. If state is "closed", return 0. if (state == WritableStream::State::Closed) return 0.0; // 5. Return ! WritableStreamDefaultControllerGetDesiredSize(stream.[[controller]]). return writable_stream_default_controller_get_desired_size(*stream->controller()); } // https://streams.spec.whatwg.org/#writable-stream-default-controller-advance-queue-if-needed WebIDL::ExceptionOr writable_stream_default_controller_advance_queue_if_needed(WritableStreamDefaultController& controller) { // 1. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 2. If controller.[[started]] is false, return. if (!controller.started()) return {}; // 3. If stream.[[inFlightWriteRequest]] is not undefined, return. if (stream->in_flight_write_request()) return {}; // 4. Let state be stream.[[state]]. auto state = stream->state(); // 5. Assert: state is not "closed" or "errored". VERIFY(state != WritableStream::State::Closed && state != WritableStream::State::Errored); // 6. If state is "erroring", if (state == WritableStream::State::Erroring) { // 1. Perform ! WritableStreamFinishErroring(stream). // 2. Return. return writable_stream_finish_erroring(*stream); } // 7. If controller.[[queue]] is empty, return. if (controller.queue().is_empty()) return {}; // 8. Let value be ! PeekQueueValue(controller). auto value = peek_queue_value(controller); // 9. If value is the close sentinel, perform ! WritableStreamDefaultControllerProcessClose(controller). if (is_close_sentinel(value)) { TRY(writable_stream_default_controller_process_close(controller)); } // 10. Otherwise, perform ! WritableStreamDefaultControllerProcessWrite(controller, value). else { TRY(writable_stream_default_controller_process_write(controller, value)); } return {}; } // https://streams.spec.whatwg.org/#writable-stream-default-controller-clear-algorithms void writable_stream_default_controller_clear_algorithms(WritableStreamDefaultController& controller) { // 1. Set controller.[[writeAlgorithm]] to undefined. controller.set_write_algorithm({}); // 2. Set controller.[[closeAlgorithm]] to undefined. controller.set_close_algorithm({}); // 3. Set controller.[[abortAlgorithm]] to undefined. controller.set_abort_algorithm({}); // 4. Set controller.[[strategySizeAlgorithm]] to undefined. controller.set_strategy_size_algorithm({}); } // https://streams.spec.whatwg.org/#writable-stream-default-controller-close WebIDL::ExceptionOr writable_stream_default_controller_close(WritableStreamDefaultController& controller) { // 1. Perform ! EnqueueValueWithSize(controller, close sentinel, 0). TRY(enqueue_value_with_size(controller, create_close_sentinel(), 0.0)); // 2. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller). TRY(writable_stream_default_controller_advance_queue_if_needed(controller)); return {}; } // https://streams.spec.whatwg.org/#writable-stream-default-controller-error WebIDL::ExceptionOr writable_stream_default_controller_error(WritableStreamDefaultController& controller, JS::Value error) { // 1. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 2. Assert: stream.[[state]] is "writable". VERIFY(stream->state() == WritableStream::State::Writable); // 3. Perform ! WritableStreamDefaultControllerClearAlgorithms(controller). writable_stream_default_controller_clear_algorithms(controller); // 4. Perform ! WritableStreamStartErroring(stream, error). return writable_stream_start_erroring(stream, error); } // https://streams.spec.whatwg.org/#writable-stream-default-controller-get-backpressure bool writable_stream_default_controller_get_backpressure(WritableStreamDefaultController const& controller) { // 1. Let desiredSize be ! WritableStreamDefaultControllerGetDesiredSize(controller). auto desired_size = writable_stream_default_controller_get_desired_size(controller); // 2. Return true if desiredSize ≤ 0, or false otherwise. return desired_size <= 0.0; } // https://streams.spec.whatwg.org/#writable-stream-default-controller-get-desired-size double writable_stream_default_controller_get_desired_size(WritableStreamDefaultController const& controller) { // 1. Return controller.[[strategyHWM]] − controller.[[queueTotalSize]]. return controller.strategy_hwm() - controller.queue_total_size(); } // https://streams.spec.whatwg.org/#writable-stream-default-controller-process-close WebIDL::ExceptionOr writable_stream_default_controller_process_close(WritableStreamDefaultController& controller) { // 1. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 2. Perform ! WritableStreamMarkCloseRequestInFlight(stream). writable_stream_mark_close_request_in_flight(*stream); // 3. Perform ! DequeueValue(controller). dequeue_value(controller); // 4. Assert: controller.[[queue]] is empty. VERIFY(controller.queue().is_empty()); // 5. Let sinkClosePromise be the result of performing controller.[[closeAlgorithm]]. auto sink_close_promise = TRY((*controller.close_algorithm())()); // 6. Perform ! WritableStreamDefaultControllerClearAlgorithms(controller). writable_stream_default_controller_clear_algorithms(controller); // 7. Upon fulfillment of sinkClosePromise, WebIDL::upon_fulfillment(*sink_close_promise, [&, stream = stream](auto const&) -> WebIDL::ExceptionOr { // 1. Perform ! WritableStreamFinishInFlightClose(stream). writable_stream_finish_in_flight_close(*stream); return JS::js_undefined(); }); // 8. Upon rejection of sinkClosePromise with reason reason, WebIDL::upon_rejection(*sink_close_promise, [&, stream = stream](auto const& reason) -> WebIDL::ExceptionOr { // 1. Perform ! WritableStreamFinishInFlightCloseWithError(stream, reason). TRY(writable_stream_finish_in_flight_close_with_error(*stream, reason)); return JS::js_undefined(); }); return {}; } // https://streams.spec.whatwg.org/#writable-stream-default-controller-process-write WebIDL::ExceptionOr writable_stream_default_controller_process_write(WritableStreamDefaultController& controller, JS::Value chunk) { // 1. Let stream be controller.[[stream]]. auto stream = controller.stream(); // 2. Perform ! WritableStreamMarkFirstWriteRequestInFlight(stream). writable_stream_mark_first_write_request_in_flight(*stream); // 3. Let sinkWritePromise be the result of performing controller.[[writeAlgorithm]], passing in chunk. auto sink_write_promise = TRY((*controller.write_algorithm())(chunk)); // 4. Upon fulfillment of sinkWritePromise, WebIDL::upon_fulfillment(*sink_write_promise, [&, stream = stream](auto const&) -> WebIDL::ExceptionOr { // 1. Perform ! WritableStreamFinishInFlightWrite(stream). writable_stream_finish_in_flight_write(*stream); // 2. Let state be stream.[[state]]. auto state = stream->state(); // 3. Assert: state is "writable" or "erroring". VERIFY(state == WritableStream::State::Writable || state == WritableStream::State::Erroring); // 4. Perform ! DequeueValue(controller). dequeue_value(controller); // 5. If ! WritableStreamCloseQueuedOrInFlight(stream) is false and state is "writable", if (!writable_stream_close_queued_or_in_flight(*stream) && state == WritableStream::State::Writable) { // 1. Let backpressure be ! WritableStreamDefaultControllerGetBackpressure(controller). auto backpressure = writable_stream_default_controller_get_backpressure(controller); // 2. Perform ! WritableStreamUpdateBackpressure(stream, backpressure). writable_stream_update_backpressure(*stream, backpressure); } // 6 .Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller). TRY(writable_stream_default_controller_advance_queue_if_needed(controller)); return JS::js_undefined(); }); // 5. Upon rejection of sinkWritePromise with reason, WebIDL::upon_rejection(*sink_write_promise, [&, stream = stream](auto const& reason) -> WebIDL::ExceptionOr { // 1. If stream.[[state]] is "writable", perform ! WritableStreamDefaultControllerClearAlgorithms(controller). if (stream->state() == WritableStream::State::Writable) writable_stream_default_controller_clear_algorithms(controller); // 2. Perform ! WritableStreamFinishInFlightWriteWithError(stream, reason). TRY(writable_stream_finish_in_flight_write_with_error(*stream, reason)); return JS::js_undefined(); }); return {}; } // https://streams.spec.whatwg.org/#close-sentinel // Non-standard function that implements the "close sentinel" value. JS::Value create_close_sentinel() { // The close sentinel is a unique value enqueued into [[queue]], in lieu of a chunk, to signal that the stream is closed. It is only used internally, and is never exposed to web developers. // Note: We use the empty Value to signal this as, similarly to the note above, the empty value is not exposed to nor creatable by web developers. return {}; } // https://streams.spec.whatwg.org/#close-sentinel // Non-standard function that implements the "If value is a close sentinel" check. bool is_close_sentinel(JS::Value value) { return value.is_empty(); } // Non-standard function to aid in converting a user-provided function into a WebIDL::Callback. This is essentially // what the Bindings generator would do at compile time, but at runtime instead. JS::ThrowCompletionOr> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key) { auto property = TRY(value.get(vm, property_key)); if (property.is_undefined()) return JS::Handle {}; if (!property.is_function()) return vm.throw_completion(JS::ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, property.to_string_without_side_effects())); return vm.heap().allocate_without_realm(property.as_object(), HTML::incumbent_settings_object()); } }