diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp index 32737dc9ca..6de71b696f 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp @@ -4,8 +4,10 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include +#include namespace Web::Fetch::Infrastructure { @@ -38,4 +40,19 @@ WebIDL::ExceptionOr Body::clone() const return Body { JS::make_handle(out2), m_source, m_length }; } +// https://fetch.spec.whatwg.org/#fully-reading-body-as-promise +JS::PromiseCapability Body::fully_read_as_promise() const +{ + auto& vm = Bindings::main_thread_vm(); + auto& realm = *vm.current_realm(); + + // FIXME: Implement the streams spec - this is completely made up for now :^) + if (auto const* byte_buffer = m_source.get_pointer()) { + auto result = String::copy(*byte_buffer); + return WebIDL::create_resolved_promise(realm, JS::js_string(vm, move(result))); + } + // Empty, Blob, FormData + return WebIDL::create_rejected_promise(realm, JS::InternalError::create(realm, "Reading body isn't fully implemented"sv)); +} + } diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h index 9c5a742902..2d7a3221f2 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h @@ -31,6 +31,8 @@ public: [[nodiscard]] WebIDL::ExceptionOr clone() const; + [[nodiscard]] JS::PromiseCapability fully_read_as_promise() const; + private: // https://fetch.spec.whatwg.org/#concept-body-stream // A stream (a ReadableStream object).