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

LibWeb: Implement ReadableStream's constructor

This commit is contained in:
Matthew Olsson 2023-03-28 19:11:22 -07:00 committed by Linus Groh
parent 66dec1bf54
commit d8710aa604
5 changed files with 160 additions and 3 deletions

View file

@ -9,14 +9,48 @@
#include <LibWeb/Streams/ReadableStream.h>
#include <LibWeb/Streams/ReadableStreamDefaultController.h>
#include <LibWeb/Streams/ReadableStreamDefaultReader.h>
#include <LibWeb/Streams/UnderlyingSource.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Streams {
// https://streams.spec.whatwg.org/#rs-constructor
WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_impl(JS::Realm& realm)
WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_impl(JS::Realm& realm, Optional<JS::Handle<JS::Object>> const& underlying_source_object)
{
return MUST_OR_THROW_OOM(realm.heap().allocate<ReadableStream>(realm, realm));
auto readable_stream = MUST_OR_THROW_OOM(realm.heap().allocate<ReadableStream>(realm, realm));
// 1. If underlyingSource is missing, set it to null.
auto underlying_source = underlying_source_object.has_value() ? JS::Value(underlying_source_object.value().ptr()) : JS::js_null();
// 2. Let underlyingSourceDict be underlyingSource, converted to an IDL value of type UnderlyingSource.
auto underlying_source_dict = TRY(UnderlyingSource::from_value(realm.vm(), underlying_source));
// 3. Perform ! InitializeReadableStream(this).
// 4. If underlyingSourceDict["type"] is "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.
// 2. Let highWaterMark be ? ExtractHighWaterMark(strategy, 0).
// 3. Perform ? SetUpReadableByteStreamControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark).
TODO();
}
// 5. Otherwise,
else {
// 1. Assert: underlyingSourceDict["type"] does not exist.
VERIFY(!underlying_source_dict.type.has_value());
// FIXME: 2. Let sizeAlgorithm be ! ExtractSizeAlgorithm(strategy).
SizeAlgorithm size_algorithm = [](auto const&) { return JS::normal_completion(JS::Value(1)); };
// FIXME: 3. Let highWaterMark be ? ExtractHighWaterMark(strategy, 1).
auto high_water_mark = 1.0;
// 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)));
}
return readable_stream;
}
ReadableStream::ReadableStream(JS::Realm& realm)