LibWeb/Fetch: Implement Body::bytes()

See:
- https://github.com/whatwg/fetch/commit/1085f4f
This commit is contained in:
Jamie Mansfield 2024-05-19 12:29:26 +01:00 committed by Andreas Kling
parent 4fe0cbcf85
commit 08e4cf1f3b
Notes: sideshowbarker 2024-07-16 22:22:13 +09:00
3 changed files with 22 additions and 0 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -9,6 +10,7 @@
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibTextCodec/Decoder.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/HostDefined.h>
@ -70,6 +72,16 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::blob() const
return consume_body(realm, *this, PackageDataType::Blob);
}
// https://fetch.spec.whatwg.org/#dom-body-bytes
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::bytes() const
{
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
// The bytes() method steps are to return the result of running consume body with this and Uint8Array.
return consume_body(realm, *this, PackageDataType::Uint8Array);
}
// https://fetch.spec.whatwg.org/#dom-body-formdata
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::form_data() const
{
@ -115,6 +127,12 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes,
auto mime_type_string = mime_type.has_value() ? MUST(mime_type->serialized()) : String {};
return FileAPI::Blob::create(realm, move(bytes), move(mime_type_string));
}
case PackageDataType::Uint8Array: {
// Return the result of creating a Uint8Array from bytes in thiss relevant realm.
auto bytes_length = bytes.size();
auto array_buffer = JS::ArrayBuffer::create(realm, move(bytes));
return JS::Uint8Array::create(realm, bytes_length, *array_buffer);
}
case PackageDataType::FormData:
// If mimeTypes essence is "multipart/form-data", then:
if (mime_type.has_value() && mime_type->essence() == "multipart/form-data"sv) {

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,6 +17,7 @@ namespace Web::Fetch {
enum class PackageDataType {
ArrayBuffer,
Blob,
Uint8Array,
FormData,
JSON,
Text,
@ -39,6 +41,7 @@ public:
// JS API functions
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> array_buffer() const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> blob() const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> bytes() const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> form_data() const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> json() const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> text() const;

View file

@ -8,6 +8,7 @@ interface mixin Body {
readonly attribute boolean bodyUsed;
[NewObject] Promise<ArrayBuffer> arrayBuffer();
[NewObject] Promise<Blob> blob();
[NewObject] Promise<Uint8Array> bytes();
[NewObject] Promise<FormData> formData();
[NewObject] Promise<any> json();
[NewObject] Promise<USVString> text();