1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 05:00:46 +00:00

LibWeb: Use the stream teeing methods for cloning Fetch response bodies

This commit is contained in:
Timothy Flynn 2024-01-28 14:21:32 -05:00 committed by Andreas Kling
parent 6981ddfe13
commit 66959bcc09
2 changed files with 6 additions and 23 deletions

View File

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/HeapFunction.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Fetch/BodyInit.h>
@ -47,32 +46,16 @@ void Body::visit_edges(Cell::Visitor& visitor)
}
// https://fetch.spec.whatwg.org/#concept-body-clone
JS::NonnullGCPtr<Body> Body::clone(JS::Realm& realm) const
JS::NonnullGCPtr<Body> Body::clone(JS::Realm& realm)
{
HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
// To clone a body body, run these steps:
// FIXME: 1. Let « out1, out2 » be the result of teeing bodys stream.
// FIXME: 2. Set bodys stream to out1.
JS::GCPtr<Streams::ReadableStream> out2;
// 1. Let « out1, out2 » be the result of teeing bodys stream.
auto [out1, out2] = m_stream->tee().release_value_but_fixme_should_propagate_errors();
auto start_algorithm = JS::create_heap_function(realm.heap(), []() -> WebIDL::ExceptionOr<JS::Value> {
return JS::js_undefined();
});
auto pull_algorithm = JS::create_heap_function(realm.heap(), [&realm]() -> WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> {
return WebIDL::create_resolved_promise(realm, JS::js_undefined());
});
auto cancel_algorithm = JS::create_heap_function(realm.heap(), [&realm](JS::Value) -> WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> {
return WebIDL::create_resolved_promise(realm, JS::js_undefined());
});
if (m_stream->controller()->has<JS::NonnullGCPtr<Streams::ReadableStreamDefaultController>>()) {
out2 = Streams::create_readable_stream(realm, start_algorithm, pull_algorithm, cancel_algorithm).release_value_but_fixme_should_propagate_errors();
} else {
out2 = Streams::create_readable_byte_stream(realm, start_algorithm, pull_algorithm, cancel_algorithm).release_value_but_fixme_should_propagate_errors();
}
// 2. Set bodys stream to out1.
m_stream = out1;
// 3. Return a body whose stream is out2 and other members are copied from body.
return Body::create(realm.vm(), *out2, m_source, m_length);

View File

@ -39,7 +39,7 @@ public:
[[nodiscard]] SourceType const& source() const { return m_source; }
[[nodiscard]] Optional<u64> const& length() const { return m_length; }
[[nodiscard]] JS::NonnullGCPtr<Body> clone(JS::Realm&) const;
[[nodiscard]] JS::NonnullGCPtr<Body> clone(JS::Realm&);
WebIDL::ExceptionOr<void> fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;