| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-07-15 22:33:22 +12:00
										 |  |  |  * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | #include <LibGC/Heap.h>
 | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | #include <LibJS/Runtime/ArrayBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | #include <LibJS/Runtime/Error.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-19 06:54:48 -04:00
										 |  |  | #include <LibJS/Runtime/Iterator.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | #include <LibJS/Runtime/PromiseCapability.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | #include <LibJS/Runtime/TypedArray.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/ExceptionOrUtils.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/ReadableStreamDefaultReaderPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2024-05-20 21:09:27 +02:00
										 |  |  | #include <LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | #include <LibWeb/Streams/AbstractOperations.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Streams/ReadableStream.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebIDL/Promise.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Streams { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(ReadableStreamDefaultReader); | 
					
						
							|  |  |  | GC_DEFINE_ALLOCATOR(ReadLoopReadRequest); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:17:19 +02:00
										 |  |  | void ReadLoopReadRequest::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_realm); | 
					
						
							|  |  |  |     visitor.visit(m_reader); | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |     visitor.visit(m_success_steps); | 
					
						
							|  |  |  |     visitor.visit(m_failure_steps); | 
					
						
							|  |  |  |     visitor.visit(m_chunk_steps); | 
					
						
							| 
									
										
										
										
											2023-08-09 18:17:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | // https://streams.spec.whatwg.org/#default-reader-constructor
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<GC::Ref<ReadableStreamDefaultReader>> ReadableStreamDefaultReader::construct_impl(JS::Realm& realm, GC::Ref<ReadableStream> stream) | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     auto reader = realm.create<ReadableStreamDefaultReader>(realm); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 1. Perform ? SetUpReadableStreamDefaultReader(this, stream);
 | 
					
						
							|  |  |  |     TRY(set_up_readable_stream_default_reader(reader, *stream)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return reader; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ReadableStreamDefaultReader::ReadableStreamDefaultReader(JS::Realm& realm) | 
					
						
							|  |  |  |     : Bindings::PlatformObject(realm) | 
					
						
							| 
									
										
										
										
											2023-04-25 18:29:13 -07:00
										 |  |  |     , ReadableStreamGenericReaderMixin(realm) | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void ReadableStreamDefaultReader::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(ReadableStreamDefaultReader); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ReadableStreamDefaultReader::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     ReadableStreamGenericReaderMixin::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2023-08-09 18:17:19 +02:00
										 |  |  |     for (auto& request : m_read_requests) | 
					
						
							|  |  |  |         visitor.visit(request); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | // https://streams.spec.whatwg.org/#read-loop
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | ReadLoopReadRequest::ReadLoopReadRequest(JS::VM& vm, JS::Realm& realm, ReadableStreamDefaultReader& reader, GC::Ref<SuccessSteps> success_steps, GC::Ref<FailureSteps> failure_steps, GC::Ptr<ChunkSteps> chunk_steps) | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  |     : m_vm(vm) | 
					
						
							|  |  |  |     , m_realm(realm) | 
					
						
							|  |  |  |     , m_reader(reader) | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |     , m_success_steps(success_steps) | 
					
						
							|  |  |  |     , m_failure_steps(failure_steps) | 
					
						
							|  |  |  |     , m_chunk_steps(chunk_steps) | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // chunk steps, given chunk
 | 
					
						
							|  |  |  | void ReadLoopReadRequest::on_chunk(JS::Value chunk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. If chunk is not a Uint8Array object, call failureSteps with a TypeError and abort these steps.
 | 
					
						
							|  |  |  |     if (!chunk.is_object() || !is<JS::Uint8Array>(chunk.as_object())) { | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |         m_failure_steps->function()(JS::TypeError::create(m_realm, "Chunk data is not Uint8Array"sv)); | 
					
						
							| 
									
										
										
										
											2023-09-06 08:11:03 -04:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto const& array = static_cast<JS::Uint8Array const&>(chunk.as_object()); | 
					
						
							|  |  |  |     auto const& buffer = array.viewed_array_buffer()->buffer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Append the bytes represented by chunk to bytes.
 | 
					
						
							| 
									
										
										
										
											2024-04-30 06:57:52 -04:00
										 |  |  |     m_bytes.append(buffer); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 06:52:29 -04:00
										 |  |  |     if (m_chunk_steps) { | 
					
						
							|  |  |  |         // FIXME: Can we move the buffer out of the `chunk`? Unclear if that is safe.
 | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |         m_chunk_steps->function()(MUST(ByteBuffer::copy(buffer))); | 
					
						
							| 
									
										
										
										
											2024-04-30 06:52:29 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  |     // FIXME: As the spec suggests, implement this non-recursively - instead of directly. It is not too big of a deal currently
 | 
					
						
							|  |  |  |     //        as we enqueue the entire blob buffer in one go, meaning that we only recurse a single time. Once we begin queuing
 | 
					
						
							|  |  |  |     //        up more than one chunk at a time, we may run into stack overflow problems.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // 3. Read-loop given reader, bytes, successSteps, and failureSteps.
 | 
					
						
							| 
									
										
										
										
											2024-04-29 16:45:50 -04:00
										 |  |  |     readable_stream_default_reader_read(m_reader, *this); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // close steps
 | 
					
						
							|  |  |  | void ReadLoopReadRequest::on_close() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. Call successSteps with bytes.
 | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |     m_success_steps->function()(move(m_bytes)); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // error steps, given e
 | 
					
						
							|  |  |  | void ReadLoopReadRequest::on_error(JS::Value error) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. Call failureSteps with e.
 | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |     m_failure_steps->function()(error); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:24:33 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:17:19 +02:00
										 |  |  | class DefaultReaderReadRequest final : public ReadRequest { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_CELL(DefaultReaderReadRequest, ReadRequest); | 
					
						
							|  |  |  |     GC_DECLARE_ALLOCATOR(DefaultReaderReadRequest); | 
					
						
							| 
									
										
										
										
											2023-08-09 18:17:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | public: | 
					
						
							|  |  |  |     DefaultReaderReadRequest(JS::Realm& realm, WebIDL::Promise& promise) | 
					
						
							|  |  |  |         : m_realm(realm) | 
					
						
							|  |  |  |         , m_promise(promise) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void on_chunk(JS::Value chunk) override | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-11 12:23:04 +01:00
										 |  |  |         WebIDL::resolve_promise(m_realm, m_promise, JS::create_iterator_result_object(m_realm->vm(), chunk, false)); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void on_close() override | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-11 12:23:04 +01:00
										 |  |  |         WebIDL::resolve_promise(m_realm, m_promise, JS::create_iterator_result_object(m_realm->vm(), JS::js_undefined(), true)); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void on_error(JS::Value error) override | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         WebIDL::reject_promise(m_realm, m_promise, error); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-08-09 18:17:19 +02:00
										 |  |  |     virtual void visit_edges(Visitor& visitor) override | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Base::visit_edges(visitor); | 
					
						
							|  |  |  |         visitor.visit(m_realm); | 
					
						
							|  |  |  |         visitor.visit(m_promise); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ref<JS::Realm> m_realm; | 
					
						
							|  |  |  |     GC::Ref<WebIDL::Promise> m_promise; | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(DefaultReaderReadRequest); | 
					
						
							| 
									
										
										
										
											2024-04-06 10:16:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | // https://streams.spec.whatwg.org/#default-reader-read
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<WebIDL::Promise> ReadableStreamDefaultReader::read() | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. If this.[[stream]] is undefined, return a promise rejected with a TypeError exception.
 | 
					
						
							|  |  |  |     if (!m_stream) { | 
					
						
							| 
									
										
										
										
											2024-03-08 15:37:28 +00:00
										 |  |  |         WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "Cannot read from an empty stream"sv }; | 
					
						
							|  |  |  |         return WebIDL::create_rejected_promise_from_exception(realm, move(exception)); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let promise be a new promise.
 | 
					
						
							|  |  |  |     auto promise_capability = WebIDL::create_promise(realm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Let readRequest be a new read request with the following items:
 | 
					
						
							|  |  |  |     //    chunk steps, given chunk
 | 
					
						
							|  |  |  |     //        Resolve promise with «[ "value" → chunk, "done" → false ]».
 | 
					
						
							|  |  |  |     //    close steps
 | 
					
						
							|  |  |  |     //        Resolve promise with «[ "value" → undefined, "done" → true ]».
 | 
					
						
							|  |  |  |     //    error steps, given e
 | 
					
						
							|  |  |  |     //        Reject promise with e.
 | 
					
						
							| 
									
										
										
										
											2024-11-14 06:13:46 +13:00
										 |  |  |     auto read_request = heap().allocate<DefaultReaderReadRequest>(realm, promise_capability); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. Perform ! ReadableStreamDefaultReaderRead(this, readRequest).
 | 
					
						
							| 
									
										
										
										
											2024-04-29 16:45:50 -04:00
										 |  |  |     readable_stream_default_reader_read(*this, read_request); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 5. Return promise.
 | 
					
						
							| 
									
										
										
										
											2024-10-25 12:38:19 -06:00
										 |  |  |     return promise_capability; | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-20 21:09:27 +02:00
										 |  |  | void ReadableStreamDefaultReader::read_a_chunk(Fetch::Infrastructure::IncrementalReadLoopReadRequest& read_request) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // To read a chunk from a ReadableStreamDefaultReader reader, given a read request readRequest,
 | 
					
						
							|  |  |  |     // perform ! ReadableStreamDefaultReaderRead(reader, readRequest).
 | 
					
						
							|  |  |  |     readable_stream_default_reader_read(*this, read_request); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 15:26:20 +12:00
										 |  |  | // https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | void ReadableStreamDefaultReader::read_all_bytes(GC::Ref<ReadLoopReadRequest::SuccessSteps> success_steps, GC::Ref<ReadLoopReadRequest::FailureSteps> failure_steps) | 
					
						
							| 
									
										
										
										
											2023-06-17 15:26:20 +12:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |     auto& vm = realm.vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Let readRequest be a new read request with the following items:
 | 
					
						
							|  |  |  |     //    NOTE: items and steps in ReadLoopReadRequest.
 | 
					
						
							| 
									
										
										
										
											2024-11-14 06:13:46 +13:00
										 |  |  |     auto read_request = heap().allocate<ReadLoopReadRequest>(vm, realm, *this, success_steps, failure_steps); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:26:20 +12:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Perform ! ReadableStreamDefaultReaderRead(this, readRequest).
 | 
					
						
							| 
									
										
										
										
											2024-04-29 16:45:50 -04:00
										 |  |  |     readable_stream_default_reader_read(*this, read_request); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:26:20 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | void ReadableStreamDefaultReader::read_all_chunks(GC::Ref<ReadLoopReadRequest::ChunkSteps> chunk_steps, GC::Ref<ReadLoopReadRequest::SuccessSteps> success_steps, GC::Ref<ReadLoopReadRequest::FailureSteps> failure_steps) | 
					
						
							| 
									
										
										
										
											2024-04-30 06:52:29 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     // AD-HOC: Some spec steps direct us to "read all chunks" from a stream, but there isn't an AO defined to do that.
 | 
					
						
							|  |  |  |     //         We implement those steps by using the "read all bytes" definition, with a custom callback to receive
 | 
					
						
							|  |  |  |     //         each chunk that is read.
 | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |     auto& vm = realm.vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Let readRequest be a new read request with the following items:
 | 
					
						
							|  |  |  |     //    NOTE: items and steps in ReadLoopReadRequest.
 | 
					
						
							| 
									
										
										
										
											2024-11-14 06:13:46 +13:00
										 |  |  |     auto read_request = heap().allocate<ReadLoopReadRequest>(vm, realm, *this, success_steps, failure_steps, chunk_steps); | 
					
						
							| 
									
										
										
										
											2024-04-30 06:52:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Perform ! ReadableStreamDefaultReaderRead(this, readRequest).
 | 
					
						
							|  |  |  |     readable_stream_default_reader_read(*this, read_request); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 15:27:14 +12:00
										 |  |  | // 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.
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<WebIDL::Promise> ReadableStreamDefaultReader::read_all_bytes_deprecated() | 
					
						
							| 
									
										
										
										
											2023-06-17 15:27:14 +12:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto promise = WebIDL::create_promise(realm); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     auto success_steps = GC::create_function(realm.heap(), [promise, &realm](ByteBuffer bytes) { | 
					
						
							| 
									
										
										
										
											2024-04-30 06:57:52 -04:00
										 |  |  |         auto buffer = JS::ArrayBuffer::create(realm, move(bytes)); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:27:14 +12:00
										 |  |  |         WebIDL::resolve_promise(realm, promise, buffer); | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:27:14 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     auto failure_steps = GC::create_function(realm.heap(), [promise, &realm](JS::Value error) { | 
					
						
							| 
									
										
										
										
											2023-06-17 15:27:14 +12:00
										 |  |  |         WebIDL::reject_promise(realm, promise, error); | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:27:14 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-18 15:07:54 +12:00
										 |  |  |     read_all_bytes(success_steps, failure_steps); | 
					
						
							| 
									
										
										
										
											2023-06-17 15:27:14 +12:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return promise; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | // https://streams.spec.whatwg.org/#default-reader-release-lock
 | 
					
						
							| 
									
										
										
										
											2024-04-29 16:45:50 -04:00
										 |  |  | void ReadableStreamDefaultReader::release_lock() | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. If this.[[stream]] is undefined, return.
 | 
					
						
							|  |  |  |     if (!m_stream) | 
					
						
							| 
									
										
										
										
											2024-04-29 16:45:50 -04:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Perform ! ReadableStreamDefaultReaderRelease(this).
 | 
					
						
							| 
									
										
										
										
											2024-04-29 16:45:50 -04:00
										 |  |  |     readable_stream_default_reader_release(*this); | 
					
						
							| 
									
										
										
										
											2023-03-28 18:30:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |