Переглянути джерело

LibWeb: Set up the Fetch response's body with the appropriate stream

Timothy Flynn 1 рік тому
батько
коміт
09124fc3a5

+ 1 - 0
Tests/LibWeb/Text/expected/Streams/init-from-fetch.txt

@@ -0,0 +1 @@
+Was able to create a reader from a Fetch response!

+ 14 - 0
Tests/LibWeb/Text/input/Streams/init-from-fetch.html

@@ -0,0 +1,14 @@
+<script src="../include.js"></script>
+<script>
+    asyncTest(async done => {
+        fetch("./../basic.html", { mode: "no-cors" })
+            .then(response => response.body)
+            .then(body => {
+                const reader = body.getReader();
+                reader.read();
+
+                println("Was able to create a reader from a Fetch response!");
+                done();
+            });
+    });
+</script>

+ 8 - 5
Userland/Libraries/LibWeb/Fetch/BodyInit.cpp

@@ -8,7 +8,10 @@
 #include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/Completion.h>
 #include <LibWeb/Fetch/BodyInit.h>
 #include <LibWeb/Fetch/BodyInit.h>
 #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
 #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
+#include <LibWeb/FileAPI/Blob.h>
 #include <LibWeb/HTML/FormControlInfrastructure.h>
 #include <LibWeb/HTML/FormControlInfrastructure.h>
+#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
+#include <LibWeb/Streams/AbstractOperations.h>
 #include <LibWeb/URL/URLSearchParams.h>
 #include <LibWeb/URL/URLSearchParams.h>
 #include <LibWeb/WebIDL/AbstractOperations.h>
 #include <LibWeb/WebIDL/AbstractOperations.h>
 #include <LibWeb/WebIDL/Buffers.h>
 #include <LibWeb/WebIDL/Buffers.h>
@@ -33,6 +36,8 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> safely_extract_body(JS::Realm&
 // https://fetch.spec.whatwg.org/#concept-bodyinit-extract
 // https://fetch.spec.whatwg.org/#concept-bodyinit-extract
 WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadableBytes const& object, bool keepalive)
 WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadableBytes const& object, bool keepalive)
 {
 {
+    HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
+
     auto& vm = realm.vm();
     auto& vm = realm.vm();
 
 
     // 1. Let stream be null.
     // 1. Let stream be null.
@@ -44,14 +49,12 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
     }
     }
     // 3. Otherwise, if object is a Blob object, set stream to the result of running object’s get stream.
     // 3. Otherwise, if object is a Blob object, set stream to the result of running object’s get stream.
     else if (auto const* blob_handle = object.get_pointer<JS::Handle<FileAPI::Blob>>()) {
     else if (auto const* blob_handle = object.get_pointer<JS::Handle<FileAPI::Blob>>()) {
-        // FIXME: "set stream to the result of running object’s get stream"
-        (void)blob_handle;
-        stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
+        stream = TRY(blob_handle->cell()->get_stream());
     }
     }
-    // 4. Otherwise, set stream to a new ReadableStream object, and set up stream.
+    // 4. Otherwise, set stream to a new ReadableStream object, and set up stream with byte reading support.
     else {
     else {
-        // FIXME: "set up stream"
         stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
         stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
+        TRY(Streams::set_up_readable_stream_controller_with_byte_reading_support(*stream));
     }
     }
 
 
     // 5. Assert: stream is a ReadableStream object.
     // 5. Assert: stream is a ReadableStream object.