Browse Source

LibWeb: Implement 'Safely extract BodyInit' AO

Linus Groh 2 years ago
parent
commit
dcded8d39f

+ 13 - 0
Userland/Libraries/LibWeb/Fetch/BodyInit.cpp

@@ -13,6 +13,19 @@
 
 
 namespace Web::Fetch {
 namespace Web::Fetch {
 
 
+// https://fetch.spec.whatwg.org/#bodyinit-safely-extract
+WebIDL::ExceptionOr<Infrastructure::BodyWithType> safely_extract_body(JS::Realm& realm, BodyInitOrReadbleBytes const& object)
+{
+    // 1. If object is a ReadableStream object, then:
+    if (auto const* stream = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
+        // 1. Assert: object is neither disturbed nor locked.
+        VERIFY(!((*stream)->is_disturbed() || (*stream)->is_locked()));
+    }
+
+    // 2. Return the result of extracting object.
+    return extract_body(realm, object);
+}
+
 // 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, BodyInitOrReadbleBytes const& object, bool keepalive)
 WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadbleBytes const& object, bool keepalive)
 {
 {

+ 1 - 0
Userland/Libraries/LibWeb/Fetch/BodyInit.h

@@ -20,6 +20,7 @@ using XMLHttpRequestBodyInit = Variant<JS::Handle<FileAPI::Blob>, JS::Handle<JS:
 using BodyInit = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String>;
 using BodyInit = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String>;
 
 
 using BodyInitOrReadbleBytes = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String, ReadonlyBytes>;
 using BodyInitOrReadbleBytes = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String, ReadonlyBytes>;
+WebIDL::ExceptionOr<Infrastructure::BodyWithType> safely_extract_body(JS::Realm&, BodyInitOrReadbleBytes const&);
 WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, BodyInitOrReadbleBytes const&, bool keepalive = false);
 WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, BodyInitOrReadbleBytes const&, bool keepalive = false);
 
 
 }
 }