mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-22 02:26:11 +00:00
LibWeb: Use the stream teeing methods for cloning Fetch response bodies
This commit is contained in:
parent
6981ddfe13
commit
66959bcc09
|
@ -4,7 +4,6 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibJS/Heap/HeapFunction.h>
|
|
||||||
#include <LibJS/Runtime/PromiseCapability.h>
|
#include <LibJS/Runtime/PromiseCapability.h>
|
||||||
#include <LibWeb/Bindings/MainThreadVM.h>
|
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||||
#include <LibWeb/Fetch/BodyInit.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
|
// 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 };
|
HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
|
||||||
|
|
||||||
// To clone a body body, run these steps:
|
// To clone a body body, run these steps:
|
||||||
// FIXME: 1. Let « out1, out2 » be the result of teeing body’s stream.
|
// 1. Let « out1, out2 » be the result of teeing body’s stream.
|
||||||
// FIXME: 2. Set body’s stream to out1.
|
auto [out1, out2] = m_stream->tee().release_value_but_fixme_should_propagate_errors();
|
||||||
JS::GCPtr<Streams::ReadableStream> out2;
|
|
||||||
|
|
||||||
auto start_algorithm = JS::create_heap_function(realm.heap(), []() -> WebIDL::ExceptionOr<JS::Value> {
|
// 2. Set body’s stream to out1.
|
||||||
return JS::js_undefined();
|
m_stream = out1;
|
||||||
});
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Return a body whose stream is out2 and other members are copied from body.
|
// 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);
|
return Body::create(realm.vm(), *out2, m_source, m_length);
|
||||||
|
|
|
@ -39,7 +39,7 @@ public:
|
||||||
[[nodiscard]] SourceType const& source() const { return m_source; }
|
[[nodiscard]] SourceType const& source() const { return m_source; }
|
||||||
[[nodiscard]] Optional<u64> const& length() const { return m_length; }
|
[[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;
|
WebIDL::ExceptionOr<void> fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue