ソースを参照

LibWeb: Stub out Fetch::Infrastructure::Body::fully_read_as_promise()

Linus Groh 2 年 前
コミット
924d7721f0

+ 17 - 0
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp

@@ -4,8 +4,10 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <LibJS/Runtime/PromiseReaction.h>
 #include <LibWeb/Bindings/MainThreadVM.h>
 #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
+#include <LibWeb/WebIDL/Promise.h>
 
 namespace Web::Fetch::Infrastructure {
 
@@ -38,4 +40,19 @@ WebIDL::ExceptionOr<Body> 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<ByteBuffer>()) {
+        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));
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h

@@ -31,6 +31,8 @@ public:
 
     [[nodiscard]] WebIDL::ExceptionOr<Body> clone() const;
 
+    [[nodiscard]] JS::PromiseCapability fully_read_as_promise() const;
+
 private:
     // https://fetch.spec.whatwg.org/#concept-body-stream
     // A stream (a ReadableStream object).