diff --git a/Userland/Libraries/LibWeb/Fetch/Body.cpp b/Userland/Libraries/LibWeb/Fetch/Body.cpp index 36118545ef3..29d060c0212 100644 --- a/Userland/Libraries/LibWeb/Fetch/Body.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Body.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022-2023, Linus Groh + * Copyright (c) 2024, Jamie Mansfield * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -70,6 +72,16 @@ WebIDL::ExceptionOr> BodyMixin::blob() const return consume_body(realm, *this, PackageDataType::Blob); } +// https://fetch.spec.whatwg.org/#dom-body-bytes +WebIDL::ExceptionOr> 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> BodyMixin::form_data() const { @@ -115,6 +127,12 @@ WebIDL::ExceptionOr 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 this’s 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 mimeType’s essence is "multipart/form-data", then: if (mime_type.has_value() && mime_type->essence() == "multipart/form-data"sv) { diff --git a/Userland/Libraries/LibWeb/Fetch/Body.h b/Userland/Libraries/LibWeb/Fetch/Body.h index dc072f44176..c33256f22c1 100644 --- a/Userland/Libraries/LibWeb/Fetch/Body.h +++ b/Userland/Libraries/LibWeb/Fetch/Body.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2022-2023, Linus Groh + * Copyright (c) 2024, Jamie Mansfield * * 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> array_buffer() const; [[nodiscard]] WebIDL::ExceptionOr> blob() const; + [[nodiscard]] WebIDL::ExceptionOr> bytes() const; [[nodiscard]] WebIDL::ExceptionOr> form_data() const; [[nodiscard]] WebIDL::ExceptionOr> json() const; [[nodiscard]] WebIDL::ExceptionOr> text() const; diff --git a/Userland/Libraries/LibWeb/Fetch/Body.idl b/Userland/Libraries/LibWeb/Fetch/Body.idl index b91f7b1f213..b22da8e6e74 100644 --- a/Userland/Libraries/LibWeb/Fetch/Body.idl +++ b/Userland/Libraries/LibWeb/Fetch/Body.idl @@ -8,6 +8,7 @@ interface mixin Body { readonly attribute boolean bodyUsed; [NewObject] Promise arrayBuffer(); [NewObject] Promise blob(); + [NewObject] Promise bytes(); [NewObject] Promise formData(); [NewObject] Promise json(); [NewObject] Promise text();