summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Fetch
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-10-30 14:37:38 +0000
committerLinus Groh <mail@linusgroh.de>2022-10-30 14:48:32 +0000
commit793a1f5994d92760d6a929c5668584e561e589a5 (patch)
tree28f375866c309b7b4d0e0d937151f6b26d51d21d /Userland/Libraries/LibWeb/Fetch
parentb1968b8bedbe4266c3a488589e64492955023799 (diff)
downloadserenity-793a1f5994d92760d6a929c5668584e561e589a5.zip
LibWeb: Actually define Blob-to-ReadableStream conversion
This is a change in the Fetch spec. See: https://github.com/whatwg/fetch/commit/4cd70cf
Diffstat (limited to 'Userland/Libraries/LibWeb/Fetch')
-rw-r--r--Userland/Libraries/LibWeb/Fetch/BodyInit.cpp49
1 files changed, 33 insertions, 16 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp b/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp
index fdb1e6fb32..bbe2457887 100644
--- a/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp
+++ b/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp
@@ -16,27 +16,43 @@ namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadbleBytes const& object, bool keepalive)
{
- // 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream.
- Streams::ReadableStream* stream;
- if (auto const* handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
- stream = const_cast<Streams::ReadableStream*>(handle->cell());
- } else {
+ // 1. Let stream be null.
+ JS::GCPtr<Streams::ReadableStream> stream;
+
+ // 2. If object is a ReadableStream object, then set stream to object.
+ if (auto const* stream_handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
+ stream = const_cast<Streams::ReadableStream*>(stream_handle->cell());
+ }
+ // 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>>()) {
+ // FIXME: "set stream to the result of running object’s get stream"
+ (void)blob_handle;
stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
}
+ // 4. Otherwise, set stream to a new ReadableStream object, and set up stream.
+ else {
+ // FIXME: "set up stream"
+ stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
+ }
+
+ // 5. Assert: stream is a ReadableStream object.
+ VERIFY(stream);
- // FIXME: 2. Let action be null.
- // 3. Let source be null.
+ // FIXME: 6. Let action be null.
+
+ // 7. Let source be null.
Infrastructure::Body::SourceType source {};
- // 4. Let length be null.
+
+ // 8. Let length be null.
Optional<u64> length {};
- // 5. Let type be null.
+
+ // 9. Let type be null.
Optional<ByteBuffer> type {};
- // 6. Switch on object.
+ // 10. Switch on object.
// FIXME: Still need to support BufferSource and FormData
TRY(object.visit(
[&](JS::Handle<FileAPI::Blob> const& blob) -> WebIDL::ExceptionOr<void> {
- // FIXME: Set action to this step: read object.
// Set source to object.
source = blob;
// Set length to object’s size.
@@ -83,12 +99,13 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
return {};
}));
- // FIXME: 7. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
- // FIXME: 8. If action is non-null, then run these steps in in parallel:
+ // FIXME: 11. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
+ // FIXME: 12. If action is non-null, then run these steps in in parallel:
+
+ // 13. Let body be a body whose stream is stream, source is source, and length is length.
+ auto body = Infrastructure::Body { JS::make_handle(*stream), move(source), move(length) };
- // 9. Let body be a body whose stream is stream, source is source, and length is length.
- auto body = Infrastructure::Body { JS::make_handle(stream), move(source), move(length) };
- // 10. Return (body, type).
+ // 14. Return (body, type).
return Infrastructure::BodyWithType { .body = move(body), .type = move(type) };
}