Explorar o código

LibWeb: Add ReadableStreamDefaultReader::read_all_bytes_deprecated

This is not to the specification, but as the FIXME comment for the
function states, we need it to be able to properly interface with the
FileAPI spcification, which seems to have not been updated to the non
promise based API.
Shannon Booth %!s(int64=2) %!d(string=hai) anos
pai
achega
b0bc8f2282

+ 23 - 0
Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp

@@ -185,6 +185,29 @@ WebIDL::ExceptionOr<void> ReadableStreamDefaultReader::read_all_bytes(ReadLoopRe
     return {};
     return {};
 }
 }
 
 
+// FIXME: This function is a promise-based wrapper around "read all bytes". The spec changed this function to not use promises
+//        in https://github.com/whatwg/streams/commit/f894acdd417926a2121710803cef593e15127964 - however, it seems that the
+//        FileAPI blob specification has not been updated to match, see: https://github.com/w3c/FileAPI/issues/187.
+WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> ReadableStreamDefaultReader::read_all_bytes_deprecated()
+{
+    auto& realm = this->realm();
+
+    auto promise = WebIDL::create_promise(realm);
+
+    auto success_steps = [promise, &realm](ByteBuffer bytes) {
+        auto buffer = JS::ArrayBuffer::create(realm, move(bytes));
+        WebIDL::resolve_promise(realm, promise, buffer);
+    };
+
+    auto failure_steps = [promise, &realm](JS::Value error) {
+        WebIDL::reject_promise(realm, promise, error);
+    };
+
+    TRY(read_all_bytes(move(success_steps), move(failure_steps)));
+
+    return promise;
+}
+
 // https://streams.spec.whatwg.org/#default-reader-release-lock
 // https://streams.spec.whatwg.org/#default-reader-release-lock
 WebIDL::ExceptionOr<void> ReadableStreamDefaultReader::release_lock()
 WebIDL::ExceptionOr<void> ReadableStreamDefaultReader::release_lock()
 {
 {

+ 2 - 0
Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.h

@@ -68,6 +68,8 @@ public:
     WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> read();
     WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> read();
 
 
     WebIDL::ExceptionOr<void> read_all_bytes(ReadLoopReadRequest::SuccessSteps, ReadLoopReadRequest::FailureSteps);
     WebIDL::ExceptionOr<void> read_all_bytes(ReadLoopReadRequest::SuccessSteps, ReadLoopReadRequest::FailureSteps);
+    WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> read_all_bytes_deprecated();
+
     WebIDL::ExceptionOr<void> release_lock();
     WebIDL::ExceptionOr<void> release_lock();
 
 
     SinglyLinkedList<NonnullRefPtr<ReadRequest>>& read_requests() { return m_read_requests; }
     SinglyLinkedList<NonnullRefPtr<ReadRequest>>& read_requests() { return m_read_requests; }