/src/serenity/Userland/Libraries/LibWeb/Streams/WritableStream.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/Runtime/PromiseCapability.h> |
8 | | #include <LibWeb/Bindings/Intrinsics.h> |
9 | | #include <LibWeb/Bindings/WritableStreamPrototype.h> |
10 | | #include <LibWeb/Streams/AbstractOperations.h> |
11 | | #include <LibWeb/Streams/UnderlyingSink.h> |
12 | | #include <LibWeb/Streams/WritableStream.h> |
13 | | #include <LibWeb/Streams/WritableStreamDefaultController.h> |
14 | | #include <LibWeb/Streams/WritableStreamDefaultWriter.h> |
15 | | #include <LibWeb/WebIDL/ExceptionOr.h> |
16 | | |
17 | | namespace Web::Streams { |
18 | | |
19 | | JS_DEFINE_ALLOCATOR(WritableStream); |
20 | | |
21 | | // https://streams.spec.whatwg.org/#ws-constructor |
22 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> WritableStream::construct_impl(JS::Realm& realm, Optional<JS::Handle<JS::Object>> const& underlying_sink_object, QueuingStrategy const& strategy) |
23 | 0 | { |
24 | 0 | auto& vm = realm.vm(); |
25 | |
|
26 | 0 | auto writable_stream = realm.heap().allocate<WritableStream>(realm, realm); |
27 | | |
28 | | // 1. If underlyingSink is missing, set it to null. |
29 | 0 | auto underlying_sink = underlying_sink_object.has_value() ? JS::Value(underlying_sink_object.value()) : JS::js_null(); |
30 | | |
31 | | // 2. Let underlyingSinkDict be underlyingSink, converted to an IDL value of type UnderlyingSink. |
32 | 0 | auto underlying_sink_dict = TRY(UnderlyingSink::from_value(vm, underlying_sink)); |
33 | | |
34 | | // 3. If underlyingSinkDict["type"] exists, throw a RangeError exception. |
35 | 0 | if (underlying_sink_dict.type.has_value()) |
36 | 0 | return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Invalid use of reserved key 'type'"sv }; |
37 | | |
38 | | // 4. Perform ! InitializeWritableStream(this). |
39 | | // Note: This AO configures slot values which are already specified in the class's field initializers. |
40 | | |
41 | | // 5. Let sizeAlgorithm be ! ExtractSizeAlgorithm(strategy). |
42 | 0 | auto size_algorithm = extract_size_algorithm(vm, strategy); |
43 | | |
44 | | // 6. Let highWaterMark be ? ExtractHighWaterMark(strategy, 1). |
45 | 0 | auto high_water_mark = TRY(extract_high_water_mark(strategy, 1)); |
46 | | |
47 | | // 7. Perform ? SetUpWritableStreamDefaultControllerFromUnderlyingSink(this, underlyingSink, underlyingSinkDict, highWaterMark, sizeAlgorithm). |
48 | 0 | TRY(set_up_writable_stream_default_controller_from_underlying_sink(*writable_stream, underlying_sink, underlying_sink_dict, high_water_mark, move(size_algorithm))); |
49 | |
|
50 | 0 | return writable_stream; |
51 | 0 | } |
52 | | |
53 | | // https://streams.spec.whatwg.org/#ws-locked |
54 | | bool WritableStream::locked() const |
55 | 0 | { |
56 | | // 1. Return ! IsWritableStreamLocked(this). |
57 | 0 | return is_writable_stream_locked(*this); |
58 | 0 | } |
59 | | |
60 | | // https://streams.spec.whatwg.org/#ws-close |
61 | | JS::GCPtr<JS::Object> WritableStream::close() |
62 | 0 | { |
63 | 0 | auto& realm = this->realm(); |
64 | | |
65 | | // 1. If ! IsWritableStreamLocked(this) is true, return a promise rejected with a TypeError exception. |
66 | 0 | if (is_writable_stream_locked(*this)) { |
67 | 0 | auto exception = JS::TypeError::create(realm, "Cannot close a locked stream"sv); |
68 | 0 | return WebIDL::create_rejected_promise(realm, exception)->promise(); |
69 | 0 | } |
70 | | |
71 | | // 2. If ! WritableStreamCloseQueuedOrInFlight(this) is true, return a promise rejected with a TypeError exception. |
72 | 0 | if (writable_stream_close_queued_or_in_flight(*this)) { |
73 | 0 | auto exception = JS::TypeError::create(realm, "Cannot close a stream that is already closed or errored"sv); |
74 | 0 | return WebIDL::create_rejected_promise(realm, exception)->promise(); |
75 | 0 | } |
76 | | |
77 | | // 3. Return ! WritableStreamClose(this). |
78 | 0 | return writable_stream_close(*this)->promise(); |
79 | 0 | } |
80 | | |
81 | | // https://streams.spec.whatwg.org/#ws-abort |
82 | | JS::GCPtr<JS::Object> WritableStream::abort(JS::Value reason) |
83 | 0 | { |
84 | 0 | auto& realm = this->realm(); |
85 | | |
86 | | // 1. If ! IsWritableStreamLocked(this) is true, return a promise rejected with a TypeError exception. |
87 | 0 | if (is_writable_stream_locked(*this)) { |
88 | 0 | auto exception = JS::TypeError::create(realm, "Cannot abort a locked stream"sv); |
89 | 0 | return WebIDL::create_rejected_promise(realm, exception)->promise(); |
90 | 0 | } |
91 | | |
92 | | // 2. Return ! WritableStreamAbort(this, reason). |
93 | 0 | return writable_stream_abort(*this, reason)->promise(); |
94 | 0 | } |
95 | | |
96 | | // https://streams.spec.whatwg.org/#ws-get-writer |
97 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> WritableStream::get_writer() |
98 | 0 | { |
99 | | // 1. Return ? AcquireWritableStreamDefaultWriter(this). |
100 | 0 | return acquire_writable_stream_default_writer(*this); |
101 | 0 | } |
102 | | |
103 | | WritableStream::WritableStream(JS::Realm& realm) |
104 | 0 | : Bindings::PlatformObject(realm) |
105 | 0 | { |
106 | 0 | } |
107 | | |
108 | | void WritableStream::initialize(JS::Realm& realm) |
109 | 0 | { |
110 | 0 | Base::initialize(realm); |
111 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(WritableStream); |
112 | 0 | } |
113 | | |
114 | | void WritableStream::visit_edges(Cell::Visitor& visitor) |
115 | 0 | { |
116 | 0 | Base::visit_edges(visitor); |
117 | 0 | visitor.visit(m_close_request); |
118 | 0 | visitor.visit(m_controller); |
119 | 0 | visitor.visit(m_in_flight_write_request); |
120 | 0 | visitor.visit(m_in_flight_close_request); |
121 | 0 | if (m_pending_abort_request.has_value()) { |
122 | 0 | visitor.visit(m_pending_abort_request->promise); |
123 | 0 | visitor.visit(m_pending_abort_request->reason); |
124 | 0 | } |
125 | 0 | visitor.visit(m_stored_error); |
126 | 0 | visitor.visit(m_writer); |
127 | 0 | for (auto& write_request : m_write_requests) |
128 | 0 | visitor.visit(write_request); |
129 | 0 | } |
130 | | |
131 | | } |