LibWeb: Add ReadableStreamGetReaderOptions to ReadableStream.getReader

Co-Authored-By: Matthew Olsson <mattco@serenityos.org>
This commit is contained in:
Shannon Booth 2023-06-29 20:53:19 +12:00 committed by Linus Groh
parent 729f4838c2
commit 0f040456a7
Notes: sideshowbarker 2024-07-17 08:43:11 +09:00
4 changed files with 26 additions and 8 deletions

View file

@ -33,6 +33,7 @@ enum class CanvasFillRule;
enum class DOMParserSupportedType;
enum class EndingType;
enum class ImageSmoothingQuality;
enum class ReadableStreamReaderMode;
enum class ReferrerPolicy;
enum class RequestCache;
enum class RequestCredentials;
@ -553,6 +554,7 @@ class WritableStreamDefaultWriter;
struct PullIntoDescriptor;
struct QueuingStrategy;
struct QueuingStrategyInit;
struct ReadableStreamGetReaderOptions;
struct UnderlyingSink;
struct UnderlyingSource;
}

View file

@ -93,14 +93,17 @@ WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> ReadableStream::cancel(JS::Value reas
}
// https://streams.spec.whatwg.org/#rs-get-reader
WebIDL::ExceptionOr<ReadableStreamReader> ReadableStream::get_reader()
WebIDL::ExceptionOr<ReadableStreamReader> ReadableStream::get_reader(ReadableStreamGetReaderOptions const& options)
{
// FIXME:
// 1. If options["mode"] does not exist, return ? AcquireReadableStreamDefaultReader(this).
// 2. Assert: options["mode"] is "byob".
// 3. Return ? AcquireReadableStreamBYOBReader(this).
if (!options.mode.has_value())
return TRY(acquire_readable_stream_default_reader(*this));
return TRY(acquire_readable_stream_default_reader(*this));
// 2. Assert: options["mode"] is "byob".
VERIFY(*options.mode == Bindings::ReadableStreamReaderMode::Byob);
// 3. Return ? AcquireReadableStreamBYOBReader(this).
return TRY(acquire_readable_stream_byob_reader(*this));
}
JS::ThrowCompletionOr<void> ReadableStream::initialize(JS::Realm& realm)

View file

@ -9,6 +9,7 @@
#include <AK/Forward.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Bindings/ReadableStreamPrototype.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Streams/QueuingStrategy.h>
@ -20,6 +21,11 @@ using ReadableStreamReader = Variant<JS::NonnullGCPtr<ReadableStreamDefaultReade
// https://streams.spec.whatwg.org/#typedefdef-readablestreamcontroller
using ReadableStreamController = Variant<JS::NonnullGCPtr<ReadableStreamDefaultController>, JS::NonnullGCPtr<ReadableByteStreamController>>;
// https://streams.spec.whatwg.org/#dictdef-readablestreamgetreaderoptions
struct ReadableStreamGetReaderOptions {
Optional<Bindings::ReadableStreamReaderMode> mode;
};
// https://streams.spec.whatwg.org/#readablestream
class ReadableStream final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
@ -37,7 +43,7 @@ public:
bool locked() const;
WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> cancel(JS::Value reason);
WebIDL::ExceptionOr<ReadableStreamReader> get_reader();
WebIDL::ExceptionOr<ReadableStreamReader> get_reader(ReadableStreamGetReaderOptions const& = {});
Optional<ReadableStreamController>& controller() { return m_controller; }
void set_controller(Optional<ReadableStreamController> value) { m_controller = move(value); }

View file

@ -2,6 +2,14 @@
#import <Streams/ReadableStreamBYOBReader.idl>
#import <Streams/ReadableStreamDefaultReader.idl>
// https://streams.spec.whatwg.org/#enumdef-readablestreamreadermode
enum ReadableStreamReaderMode { "byob" };
// https://streams.spec.whatwg.org/#dictdef-readablestreamgetreaderoptions
dictionary ReadableStreamGetReaderOptions {
ReadableStreamReaderMode mode;
};
// https://streams.spec.whatwg.org/#readablestream
[Exposed=*, Transferable]
interface ReadableStream {
@ -10,8 +18,7 @@ interface ReadableStream {
readonly attribute boolean locked;
Promise<undefined> cancel(optional any reason);
// FIXME: optional ReadableStreamGetReaderOptions options = {}
ReadableStreamReader getReader();
ReadableStreamReader getReader(optional ReadableStreamGetReaderOptions options = {});
};
typedef (ReadableStreamDefaultReader or ReadableStreamBYOBReader) ReadableStreamReader;