1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:17:36 +00:00

LibWeb: Implement QueuingStrategy for Web::Streams::ReadableStream

This commit is contained in:
Shannon Booth 2023-06-18 21:57:07 +12:00 committed by Andreas Kling
parent e5c7d8407b
commit 4f217947b3
3 changed files with 16 additions and 10 deletions

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org> * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -18,7 +19,7 @@
namespace Web::Streams { namespace Web::Streams {
// https://streams.spec.whatwg.org/#rs-constructor // https://streams.spec.whatwg.org/#rs-constructor
WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_impl(JS::Realm& realm, Optional<JS::Handle<JS::Object>> const& underlying_source_object) WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_impl(JS::Realm& realm, Optional<JS::Handle<JS::Object>> const& underlying_source_object, QueuingStrategy const& strategy)
{ {
auto& vm = realm.vm(); auto& vm = realm.vm();
@ -34,22 +35,26 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_
// 4. If underlyingSourceDict["type"] is "bytes": // 4. If underlyingSourceDict["type"] is "bytes":
if (underlying_source_dict.type.has_value() && underlying_source_dict.type.value() == ReadableStreamType::Bytes) { if (underlying_source_dict.type.has_value() && underlying_source_dict.type.value() == ReadableStreamType::Bytes) {
// FIXME:
// 1. If strategy["size"] exists, throw a RangeError exception. // 1. If strategy["size"] exists, throw a RangeError exception.
if (strategy.size)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Size strategy not allowed for byte stream"sv };
// 2. Let highWaterMark be ? ExtractHighWaterMark(strategy, 0). // 2. Let highWaterMark be ? ExtractHighWaterMark(strategy, 0).
auto high_water_mark = TRY(extract_high_water_mark(strategy, 0));
// 3. Perform ? SetUpReadableByteStreamControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark). // 3. Perform ? SetUpReadableByteStreamControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark).
TODO(); TRY(set_up_readable_byte_stream_controller_from_underlying_source(*readable_stream, underlying_source, underlying_source_dict, high_water_mark));
} }
// 5. Otherwise, // 5. Otherwise,
else { else {
// 1. Assert: underlyingSourceDict["type"] does not exist. // 1. Assert: underlyingSourceDict["type"] does not exist.
VERIFY(!underlying_source_dict.type.has_value()); VERIFY(!underlying_source_dict.type.has_value());
// FIXME: 2. Let sizeAlgorithm be ! ExtractSizeAlgorithm(strategy). // 2. Let sizeAlgorithm be ! ExtractSizeAlgorithm(strategy).
SizeAlgorithm size_algorithm = [](auto const&) { return JS::normal_completion(JS::Value(1)); }; auto size_algorithm = extract_size_algorithm(strategy);
// FIXME: 3. Let highWaterMark be ? ExtractHighWaterMark(strategy, 1). // 3. Let highWaterMark be ? ExtractHighWaterMark(strategy, 1).
auto high_water_mark = 1.0; auto high_water_mark = TRY(extract_high_water_mark(strategy, 1));
// 4. Perform ? SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark, sizeAlgorithm). // 4. Perform ? SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark, sizeAlgorithm).
TRY(set_up_readable_stream_default_controller_from_underlying_source(*readable_stream, underlying_source, underlying_source_dict, high_water_mark, move(size_algorithm))); TRY(set_up_readable_stream_default_controller_from_underlying_source(*readable_stream, underlying_source, underlying_source_dict, high_water_mark, move(size_algorithm)));

View file

@ -10,6 +10,7 @@
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
#include <LibWeb/Streams/QueuingStrategy.h>
namespace Web::Streams { namespace Web::Streams {
@ -30,7 +31,7 @@ public:
Errored, Errored,
}; };
static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&, Optional<JS::Handle<JS::Object>> const& underlying_source); static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&, Optional<JS::Handle<JS::Object>> const& underlying_source, QueuingStrategy const& = {});
virtual ~ReadableStream() override; virtual ~ReadableStream() override;

View file

@ -1,11 +1,11 @@
#import <Streams/QueuingStrategy.idl>
#import <Streams/ReadableStreamBYOBReader.idl> #import <Streams/ReadableStreamBYOBReader.idl>
#import <Streams/ReadableStreamDefaultReader.idl> #import <Streams/ReadableStreamDefaultReader.idl>
// https://streams.spec.whatwg.org/#readablestream // https://streams.spec.whatwg.org/#readablestream
[Exposed=*, Transferable] [Exposed=*, Transferable]
interface ReadableStream { interface ReadableStream {
// FIXME: optional QueuingStrategy strategy = {} constructor(optional object underlyingSource, optional QueuingStrategy strategy = {});
constructor(optional object underlyingSource);
readonly attribute boolean locked; readonly attribute boolean locked;