mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-01 16:02:53 +00:00
LibWeb: Implement Blob::bytes()
Implements https://w3c.github.io/FileAPI/#dom-blob-bytes.
This commit is contained in:
parent
1ce9bbdd6d
commit
c5f1e47883
Notes:
github-actions[bot]
2024-07-26 08:22:29 +00:00
Author: https://github.com/kemzeb
Commit: c5f1e47883
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/802
Reviewed-by: https://github.com/kennethmyhra ✅
Reviewed-by: https://github.com/shannonbooth
12 changed files with 52 additions and 10 deletions
|
@ -107,7 +107,7 @@ ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optio
|
|||
},
|
||||
// 3. If element is a Blob, append the bytes it represents to bytes.
|
||||
[&](JS::Handle<Blob> const& blob) -> ErrorOr<void> {
|
||||
return bytes.try_append(blob->bytes());
|
||||
return bytes.try_append(blob->raw_bytes());
|
||||
}));
|
||||
}
|
||||
// 3. Return bytes.
|
||||
|
@ -413,4 +413,30 @@ JS::NonnullGCPtr<JS::Promise> Blob::array_buffer()
|
|||
}));
|
||||
}
|
||||
|
||||
// https://w3c.github.io/FileAPI/#dom-blob-bytes
|
||||
JS::NonnullGCPtr<JS::Promise> Blob::bytes()
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
|
||||
// 1. Let stream be the result of calling get stream on this.
|
||||
auto stream = get_stream();
|
||||
|
||||
// 2. Let reader be the result of getting a reader from stream. If that threw an exception, return a new promise rejected with that exception.
|
||||
auto reader_or_exception = acquire_readable_stream_default_reader(*stream);
|
||||
if (reader_or_exception.is_exception())
|
||||
return WebIDL::create_rejected_promise_from_exception(realm, reader_or_exception.release_error());
|
||||
auto reader = reader_or_exception.release_value();
|
||||
|
||||
// 3. Let promise be the result of reading all bytes from stream with reader.
|
||||
auto promise = reader->read_all_bytes_deprecated();
|
||||
|
||||
// 4. Return the result of transforming promise by a fulfillment handler that returns a new Uint8Array wrapping an ArrayBuffer containing its first argument.
|
||||
return WebIDL::upon_fulfillment(*promise, JS::create_heap_function(heap(), [&realm](JS::Value first_argument) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto& object = first_argument.as_object();
|
||||
VERIFY(is<JS::ArrayBuffer>(object));
|
||||
auto& array_buffer = static_cast<JS::ArrayBuffer&>(object);
|
||||
return JS::Uint8Array::create(realm, array_buffer.byte_length(), array_buffer);
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue