mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:08:12 +00:00
LibWeb/Fetch: Implement Body's "fully read" function from the spec
Required by XHR's reliance on Fetch.
This commit is contained in:
parent
9aca54091a
commit
ccdb1bcc4e
9 changed files with 121 additions and 42 deletions
|
@ -9,6 +9,8 @@
|
|||
#include <LibJS/Runtime/Completion.h>
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/PromiseCapability.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/Bindings/HostDefined.h>
|
||||
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||
#include <LibWeb/Fetch/Body.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||
|
@ -47,7 +49,7 @@ bool BodyMixin::body_used() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-arraybuffer
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::array_buffer() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::array_buffer() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -57,7 +59,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::array_buffer() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-blob
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::blob() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::blob() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -67,7 +69,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::blob() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-formdata
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::form_data() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::form_data() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -77,7 +79,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::form_data() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-json
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::json() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::json() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -87,7 +89,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::json() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-text
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::text() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::text() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -142,33 +144,74 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes,
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-body-consume-body
|
||||
JS::NonnullGCPtr<JS::Promise> consume_body(JS::Realm& realm, BodyMixin const& object, PackageDataType type)
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> consume_body(JS::Realm& realm, BodyMixin const& object, PackageDataType type)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
|
||||
// 1. If object is unusable, then return a promise rejected with a TypeError.
|
||||
if (object.is_unusable()) {
|
||||
auto promise_capability = WebIDL::create_rejected_promise(realm, JS::TypeError::create(realm, "Body is unusable"sv).release_allocated_value_but_fixme_should_propagate_errors());
|
||||
return verify_cast<JS::Promise>(*promise_capability->promise().ptr());
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise().ptr()) };
|
||||
}
|
||||
|
||||
// 2. Let promise be a promise resolved with an empty byte sequence.
|
||||
auto promise = WebIDL::create_resolved_promise(realm, JS::PrimitiveString::create(vm, String {}));
|
||||
// 2. Let promise be a new promise.
|
||||
auto promise = WebIDL::create_promise(realm);
|
||||
|
||||
// 3. If object’s body is non-null, then set promise to the result of fully reading body as promise given object’s body.
|
||||
auto const& body = object.body_impl();
|
||||
if (body.has_value())
|
||||
promise = body->fully_read_as_promise().release_value_but_fixme_should_propagate_errors();
|
||||
// 3. Let errorSteps given error be to reject promise with error.
|
||||
// NOTE: `promise` and `realm` is protected by JS::SafeFunction.
|
||||
auto error_steps = [promise, &realm](JS::Object& error) {
|
||||
// NOTE: Not part of the spec, but we need to have an execution context on the stack to call native functions.
|
||||
// (In this case, Promise's reject function)
|
||||
auto& environment_settings_object = Bindings::host_defined_environment_settings_object(realm);
|
||||
environment_settings_object.prepare_to_run_script();
|
||||
|
||||
// 4. Let steps be to return the result of package data with the first argument given, type, and object’s MIME type.
|
||||
auto steps = [&vm, &realm, &object, type](JS::Value value) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
VERIFY(value.is_string());
|
||||
auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(TRY(value.as_string().deprecated_string()).bytes()));
|
||||
return package_data(realm, move(bytes), type, object.mime_type_impl().release_value_but_fixme_should_propagate_errors());
|
||||
WebIDL::reject_promise(realm, promise, &error);
|
||||
|
||||
// See above NOTE.
|
||||
environment_settings_object.clean_up_after_running_script();
|
||||
};
|
||||
|
||||
// 5. Return the result of upon fulfillment of promise given steps.
|
||||
return WebIDL::upon_fulfillment(promise, move(steps));
|
||||
// 4. Let successSteps given a byte sequence data be to resolve promise with the result of running convertBytesToJSValue
|
||||
// with data. If that threw an exception, then run errorSteps with that exception.
|
||||
// NOTE: `promise`, `realm` and `object` is protected by JS::SafeFunction.
|
||||
// FIXME: Refactor this to the new version of the spec introduced with https://github.com/whatwg/fetch/commit/464326e8eb6a602122c030cd40042480a3c0e265
|
||||
auto success_steps = [promise, &realm, &object, type](ByteBuffer const& data) {
|
||||
auto& vm = realm.vm();
|
||||
|
||||
// NOTE: Not part of the spec, but we need to have an execution context on the stack to call native functions.
|
||||
// (In this case, Promise's reject function and JSON.parse)
|
||||
auto& environment_settings_object = Bindings::host_defined_environment_settings_object(realm);
|
||||
environment_settings_object.prepare_to_run_script();
|
||||
|
||||
ScopeGuard guard = [&]() {
|
||||
// See above NOTE.
|
||||
environment_settings_object.clean_up_after_running_script();
|
||||
};
|
||||
|
||||
auto value_or_error = Bindings::throw_dom_exception_if_needed(vm, [&]() -> WebIDL::ExceptionOr<JS::Value> {
|
||||
return package_data(realm, data, type, TRY_OR_THROW_OOM(vm, object.mime_type_impl()));
|
||||
});
|
||||
|
||||
if (value_or_error.is_error()) {
|
||||
// We can't call error_steps here without moving it into success_steps, causing a double move when we pause error_steps
|
||||
// to fully_read, so just reject the promise like error_steps does.
|
||||
WebIDL::reject_promise(realm, promise, value_or_error.release_error().value().value());
|
||||
return;
|
||||
}
|
||||
|
||||
WebIDL::resolve_promise(realm, promise, value_or_error.release_value());
|
||||
};
|
||||
|
||||
// 5. If object’s body is null, then run successSteps with an empty byte sequence.
|
||||
auto const& body = object.body_impl();
|
||||
if (!body.has_value()) {
|
||||
success_steps(ByteBuffer {});
|
||||
}
|
||||
// 6. Otherwise, fully read object’s body given successSteps, errorSteps, and object’s relevant global object.
|
||||
else {
|
||||
TRY(body->fully_read(realm, move(success_steps), move(error_steps), JS::NonnullGCPtr { HTML::relevant_global_object(object.as_platform_object()) }));
|
||||
}
|
||||
|
||||
// 7. Return promise.
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise->promise().ptr()) };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue