/
readable.h
403 lines (302 loc) · 13.8 KB
/
readable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#pragma once
#include "internal.h"
#include "standard.h"
#include <kj/function.h>
namespace workerd::api {
class ReadableStreamDefaultReader;
class ReadableStreamBYOBReader;
class ReaderImpl {
public:
ReaderImpl(ReadableStreamController::Reader& reader);
~ReaderImpl() noexcept(false);
void attach(ReadableStreamController& controller, jsg::Promise<void> closedPromise);
jsg::Promise<void> cancel(jsg::Lock& js, jsg::Optional<v8::Local<v8::Value>> maybeReason);
void detach();
jsg::MemoizedIdentity<jsg::Promise<void>>& getClosed();
void lockToStream(jsg::Lock& js, ReadableStream& stream);
jsg::Promise<ReadResult> read(jsg::Lock& js,
kj::Maybe<ReadableStreamController::ByobOptions> byobOptions);
void releaseLock(jsg::Lock& js);
void visitForGc(jsg::GcVisitor& visitor);
private:
struct Initial {};
using Attached = jsg::Ref<ReadableStream>;
// While a Reader is attached to a ReadableStream, it holds a strong reference to the
// ReadableStream to prevent it from being GC'd so long as the Reader is available.
// Once the reader is closed, released, or GC'd the reference to the ReadableStream
// is cleared and the ReadableStream can be GC'd if there are no other references to
// it being held anywhere. If the reader is still attached to the ReadableStream when
// it is destroyed, the ReadableStream's reference to the reader is cleared but the
// ReadableStream remains in the "reader locked" state, per the spec.
struct Released {};
ReadableStreamController::Reader& reader;
kj::OneOf<Initial, Attached, StreamStates::Closed, Released> state = Initial();
kj::Maybe<jsg::MemoizedIdentity<jsg::Promise<void>>> closedPromise;
friend class ReadableStreamDefaultReader;
friend class ReadableStreamBYOBReader;
};
class ReadableStreamDefaultReader : public jsg::Object,
public ReadableStreamController::Reader {
public:
explicit ReadableStreamDefaultReader();
// JavaScript API
static jsg::Ref<ReadableStreamDefaultReader> constructor(
jsg::Lock& js, jsg::Ref<ReadableStream> stream);
jsg::MemoizedIdentity<jsg::Promise<void>>& getClosed();
jsg::Promise<void> cancel(jsg::Lock& js, jsg::Optional<v8::Local<v8::Value>> reason);
jsg::Promise<ReadResult> read(jsg::Lock& js);
void releaseLock(jsg::Lock& js);
JSG_RESOURCE_TYPE(ReadableStreamDefaultReader, CompatibilityFlags::Reader flags) {
if (flags.getJsgPropertyOnPrototypeTemplate()) {
JSG_READONLY_PROTOTYPE_PROPERTY(closed, getClosed);
} else {
JSG_READONLY_INSTANCE_PROPERTY(closed, getClosed);
}
JSG_METHOD(cancel);
JSG_METHOD(read);
JSG_METHOD(releaseLock);
JSG_TS_OVERRIDE(<R = any> {
read(): Promise<ReadableStreamReadResult<R>>;
});
}
// Internal API
void attach(ReadableStreamController& controller, jsg::Promise<void> closedPromise) override;
void detach() override;
void lockToStream(jsg::Lock& js, ReadableStream& stream);
inline bool isByteOriented() const override { return false; }
private:
ReaderImpl impl;
void visitForGc(jsg::GcVisitor& visitor);
};
class ReadableStreamBYOBReader: public jsg::Object,
public ReadableStreamController::Reader {
public:
explicit ReadableStreamBYOBReader();
// JavaScript API
static jsg::Ref<ReadableStreamBYOBReader> constructor(
jsg::Lock& js,
jsg::Ref<ReadableStream> stream);
jsg::MemoizedIdentity<jsg::Promise<void>>& getClosed();
jsg::Promise<void> cancel(jsg::Lock& js, jsg::Optional<v8::Local<v8::Value>> reason);
jsg::Promise<ReadResult> read(
jsg::Lock& js,
v8::Local<v8::ArrayBufferView> byobBuffer,
CompatibilityFlags::Reader featureFlags);
jsg::Promise<ReadResult> readAtLeast(jsg::Lock& js,
int minBytes,
v8::Local<v8::ArrayBufferView> byobBuffer);
// Non-standard extension so that reads can specify a minimum number of bytes to read. It's a
// struct so that we could eventually add things like timeouts if we need to. Since there's no
// existing spec that's a leading contendor, this is behind a different method name to avoid
// conflicts with any changes to `read`. Fewer than `minBytes` may be returned if EOF is hit or
// the underlying stream is closed/errors out. In all cases the read result is either
// {value: theChunk, done: false} or {value: undefined, done: true} as with read.
// TODO(soon): Like fetch() and Cache.match(), readAtLeast() returns a promise for a V8 object.
void releaseLock(jsg::Lock& js);
JSG_RESOURCE_TYPE(ReadableStreamBYOBReader, CompatibilityFlags::Reader flags) {
if (flags.getJsgPropertyOnPrototypeTemplate()) {
JSG_READONLY_PROTOTYPE_PROPERTY(closed, getClosed);
} else {
JSG_READONLY_INSTANCE_PROPERTY(closed, getClosed);
}
JSG_METHOD(cancel);
JSG_METHOD(read);
JSG_METHOD(releaseLock);
JSG_METHOD(readAtLeast);
// Non-standard extension that should only apply to BYOB byte streams.
JSG_TS_OVERRIDE(ReadableStreamBYOBReader {
read<T extends ArrayBufferView>(view: T): Promise<ReadableStreamReadResult<T>>;
readAtLeast<T extends ArrayBufferView>(minElements: number, view: T): Promise<ReadableStreamReadResult<T>>;
});
}
// Internal API
void attach(
ReadableStreamController& controller,
jsg::Promise<void> closedPromise) override;
void detach() override;
void lockToStream(jsg::Lock& js, ReadableStream& stream);
inline bool isByteOriented() const override { return true; }
private:
ReaderImpl impl;
void visitForGc(jsg::GcVisitor& visitor);
};
class ReadableStream: public jsg::Object {
private:
struct AsyncIteratorState {
jsg::Ref<ReadableStreamDefaultReader> reader;
bool preventCancel;
};
static jsg::Promise<kj::Maybe<jsg::Value>> nextFunction(
jsg::Lock& js,
AsyncIteratorState& state);
static jsg::Promise<void> returnFunction(
jsg::Lock& js,
AsyncIteratorState& state,
jsg::Optional<jsg::Value> value);
public:
using Controller = kj::OneOf<kj::Own<ReadableStreamInternalController>,
kj::Own<ReadableStreamJsController>>;
explicit ReadableStream(IoContext& ioContext,
kj::Own<ReadableStreamSource> source);
explicit ReadableStream(Controller controller);
ReadableStreamController& getController();
jsg::Ref<ReadableStream> addRef() { return JSG_THIS; }
kj::Own<ReadableStreamSource> removeSource(jsg::Lock& js);
// Remove and return the underlying implementation of this ReadableStream. Throw a TypeError if
// this ReadableStream is disturbed or locked, otherwise this ReadableStream becomes immediately
// disturbed, locked, and closed. If this ReadableStream is already closed, return a null input
// stream. If this readable stream is errored, throw the stored error.
bool isDisturbed() { return getController().isDisturbed(); }
// ---------------------------------------------------------------------------
// JS interface
static jsg::Ref<ReadableStream> constructor(
jsg::Lock& js,
jsg::Optional<UnderlyingSource> underlyingSource,
jsg::Optional<StreamQueuingStrategy> queuingStrategy,
CompatibilityFlags::Reader flags);
// Creates a new JS-backed ReadableStream using the provided source and strategy.
// We use v8::Local<v8::Object>'s here instead of jsg structs because we need
// to preserve the object references within the implementation.
bool isLocked() { return getController().isLockedToReader(); }
jsg::Promise<void> cancel(jsg::Lock& js, jsg::Optional<v8::Local<v8::Value>> reason);
// Closes the stream. All present and future read requests are fulfilled with successful empty
// results. `reason` will be passed to the underlying source's cancel algorithm -- if this
// readable stream is one side of a transform stream, then its cancel algorithm causes the
// transform's writable side to become errored with `reason`.
using Reader = kj::OneOf<jsg::Ref<ReadableStreamDefaultReader>,
jsg::Ref<ReadableStreamBYOBReader>>;
struct GetReaderOptions {
jsg::Optional<kj::String> mode; // can be "byob" or undefined
JSG_STRUCT(mode);
JSG_STRUCT_TS_OVERRIDE({ mode: "byob" });
// Intentionally required, so we can use `GetReaderOptions` directly in the
// `ReadableStream#getReader()` overload.
};
Reader getReader(jsg::Lock& js, jsg::Optional<GetReaderOptions> options);
struct ValuesOptions {
// Options specifically for the values() function.
jsg::Optional<bool> preventCancel = false;
JSG_STRUCT(preventCancel);
};
JSG_ASYNC_ITERATOR_WITH_OPTIONS(ReadableStreamAsyncIterator,
values,
jsg::Value,
AsyncIteratorState,
nextFunction,
returnFunction,
ValuesOptions);
struct Transform {
jsg::Ref<WritableStream> writable;
jsg::Ref<ReadableStream> readable;
JSG_STRUCT(writable, readable);
JSG_STRUCT_TS_OVERRIDE(ReadableWritablePair<R = any, W = any> {
readable: ReadableStream<R>;
writable: WritableStream<W>;
});
};
jsg::Ref<ReadableStream> pipeThrough(
jsg::Lock& js,
Transform transform,
jsg::Optional<PipeToOptions> options);
jsg::Promise<void> pipeTo(
jsg::Lock& js,
jsg::Ref<WritableStream> destination,
jsg::Optional<PipeToOptions> options);
kj::Array<jsg::Ref<ReadableStream>> tee(jsg::Lock& js);
// Locks the stream and returns a pair of two new ReadableStreams, each of which read the same
// data as this ReadableStream would.
JSG_RESOURCE_TYPE(ReadableStream, CompatibilityFlags::Reader flags) {
if (flags.getJsgPropertyOnPrototypeTemplate()) {
JSG_READONLY_PROTOTYPE_PROPERTY(locked, isLocked);
} else {
JSG_READONLY_INSTANCE_PROPERTY(locked, isLocked);
}
JSG_METHOD(cancel);
JSG_METHOD(getReader);
JSG_METHOD(pipeThrough);
JSG_METHOD(pipeTo);
JSG_METHOD(tee);
JSG_METHOD(values);
JSG_ASYNC_ITERABLE(values);
JSG_TS_DEFINE(interface ReadableStream<R = any> {
cancel(reason?: any): Promise<void>;
getReader(): ReadableStreamDefaultReader<R>;
getReader(options: ReadableStreamGetReaderOptions): ReadableStreamBYOBReader;
pipeThrough<T>(transform: ReadableWritablePair<T, R>, options?: StreamPipeOptions): ReadableStream<T>;
pipeTo(destination: WritableStream<R>, options?: StreamPipeOptions): Promise<void>;
tee(): [ReadableStream<R>, ReadableStream<R>];
values(options?: ReadableStreamValuesOptions): AsyncIterableIterator<R>;
[Symbol.asyncIterator](options?: ReadableStreamValuesOptions): AsyncIterableIterator<R>;
});
JSG_TS_OVERRIDE(const ReadableStream: {
prototype: ReadableStream;
new (underlyingSource: UnderlyingByteSource, strategy?: QueuingStrategy<Uint8Array>): ReadableStream<Uint8Array>;
new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
});
// Replace ReadableStream class with an interface and const, so we can have
// two constructors with differing type parameters for byte-oriented and
// value-oriented streams.
}
private:
Controller controller;
kj::Maybe<jsg::Promise<void>> maybePipeThrough;
void visitForGc(jsg::GcVisitor& visitor) {
visitor.visit(getController(), maybePipeThrough);
}
};
struct QueuingStrategyInit {
double highWaterMark;
JSG_STRUCT(highWaterMark);
};
using QueuingStrategySizeFunction =
jsg::Optional<uint32_t>(jsg::Optional<v8::Local<v8::Value>>);
class ByteLengthQueuingStrategy: public jsg::Object {
// Utility class defined by the streams spec that uses byteLength to calculate
// backpressure changes.
public:
ByteLengthQueuingStrategy(QueuingStrategyInit init) : init(init) {}
static jsg::Ref<ByteLengthQueuingStrategy> constructor(QueuingStrategyInit init) {
return jsg::alloc<ByteLengthQueuingStrategy>(init);
}
double getHighWaterMark() const { return init.highWaterMark; }
jsg::Function<QueuingStrategySizeFunction> getSize() const { return &size; }
JSG_RESOURCE_TYPE(ByteLengthQueuingStrategy) {
JSG_READONLY_PROTOTYPE_PROPERTY(highWaterMark, getHighWaterMark);
JSG_READONLY_PROTOTYPE_PROPERTY(size, getSize);
JSG_TS_OVERRIDE(implements QueuingStrategy<ArrayBufferView> {
get size(): (chunk?: any) => number;
});
// QueuingStrategy requires the result of the size function to be defined
}
private:
static jsg::Optional<uint32_t> size(jsg::Lock& js, jsg::Optional<v8::Local<v8::Value>>);
QueuingStrategyInit init;
};
class CountQueuingStrategy: public jsg::Object {
// Utility class defined by the streams spec that uses a fixed value of 1 to calculate
// backpressure changes.
public:
CountQueuingStrategy(QueuingStrategyInit init) : init(init) {}
static jsg::Ref<CountQueuingStrategy> constructor(QueuingStrategyInit init) {
return jsg::alloc<CountQueuingStrategy>(init);
}
double getHighWaterMark() const { return init.highWaterMark; }
jsg::Function<QueuingStrategySizeFunction> getSize() const { return &size; }
JSG_RESOURCE_TYPE(CountQueuingStrategy) {
JSG_READONLY_PROTOTYPE_PROPERTY(highWaterMark, getHighWaterMark);
JSG_READONLY_PROTOTYPE_PROPERTY(size, getSize);
JSG_TS_OVERRIDE(implements QueuingStrategy {
get size(): (chunk?: any) => number;
});
// QueuingStrategy requires the result of the size function to be defined
}
private:
static jsg::Optional<uint32_t> size(jsg::Lock& js, jsg::Optional<v8::Local<v8::Value>>) {
return 1;
}
QueuingStrategyInit init;
};
} // namespace workerd::api