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
|
/*
* Copyright (C) 2015 Canon Inc.
* Copyright (C) 2015 Igalia.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function initializeReadableStream(underlyingSource, strategy)
{
"use strict";
if (underlyingSource === @undefined)
underlyingSource = { };
if (strategy === @undefined)
strategy = { };
if (!@isObject(underlyingSource))
@throwTypeError("ReadableStream constructor takes an object as first argument");
if (strategy !== @undefined && !@isObject(strategy))
@throwTypeError("ReadableStream constructor takes an object as second argument, if any");
@putByIdDirectPrivate(this, "state", @streamReadable);
@putByIdDirectPrivate(this, "reader", @undefined);
@putByIdDirectPrivate(this, "storedError", @undefined);
@putByIdDirectPrivate(this, "disturbed", false);
// Initialized with null value to enable distinction with undefined case.
@putByIdDirectPrivate(this, "readableStreamController", null);
// FIXME: We should introduce https://streams.spec.whatwg.org/#create-readable-stream.
// For now, we emulate this with underlyingSource with private properties.
if (@getByIdDirectPrivate(underlyingSource, "pull") !== @undefined) {
const size = @getByIdDirectPrivate(strategy, "size");
const highWaterMark = @getByIdDirectPrivate(strategy, "highWaterMark");
@setupReadableStreamDefaultController(this, underlyingSource, size, highWaterMark !== @undefined ? highWaterMark : 1, @getByIdDirectPrivate(underlyingSource, "start"), @getByIdDirectPrivate(underlyingSource, "pull"), @getByIdDirectPrivate(underlyingSource, "cancel"));
return this;
}
const type = underlyingSource.type;
const typeString = @toString(type);
if (typeString === "bytes") {
// if (!@readableByteStreamAPIEnabled())
// @throwTypeError("ReadableByteStreamController is not implemented");
if (strategy.highWaterMark === @undefined)
strategy.highWaterMark = 0;
if (strategy.size !== @undefined)
@throwRangeError("Strategy for a ReadableByteStreamController cannot have a size");
let readableByteStreamControllerConstructor = @ReadableByteStreamController;
@putByIdDirectPrivate(this, "readableStreamController", new @ReadableByteStreamController(this, underlyingSource, strategy.highWaterMark, @isReadableStream));
} else if (type === @undefined) {
if (strategy.highWaterMark === @undefined)
strategy.highWaterMark = 1;
@setupReadableStreamDefaultController(this, underlyingSource, strategy.size, strategy.highWaterMark, underlyingSource.start, underlyingSource.pull, underlyingSource.cancel);
} else
@throwRangeError("Invalid type for underlying source");
return this;
}
@globalPrivate
function createNativeReadableStream(nativePtr, nativeType) {
"use strict";
var cached = globalThis[Symbol.for("Bun.nativeReadableStreamPrototype")] ||= new @Map;
var Prototype = cached.@get(nativeType);
if (Prototype === @undefined) {
var [pull, start, cancel, setClose, deinit] = globalThis[Symbol.for("Bun.lazy")](nativeType);
var closer = [false];
function handleNativeReadableStreamPromiseResult(val) {
"use strict";
var {r, c} = this;
this.r = @undefined;
this.c = @undefined;
r(val, c);
}
function closeNativeReadableStreamOnNextTick(controller) {
"use strict";
controller.close();
controller = @undefined;
}
var handleResult = function handleResult(result, controller) {
"use strict";
if (result && @isPromise(result)) {
return result.then(handleNativeReadableStreamPromiseResult.bind({c: controller, r: handleResult}), controller.error);
} else if (result !== false) {
controller.enqueue(result);
}
if (closer[0] || result === false) {
@enqueueJob(closeNativeReadableStreamOnNextTick, controller);
closer[0] = false;
}
}
Prototype = class NativeReadableStreamSource {
constructor(tag) {
this.pull = this.pull_.bind(tag);
this.start = this.start_.bind(tag);
this.cancel = this.cancel_.bind(tag);
}
pull;
start;
cancel;
pull_(controller) {
closer[0] = false;
var result;
try {
result = pull(this, closer);
} catch(err) {
return controller.error(err);
}
return handleResult(result, controller);
}
start_(controller) {
setClose(this, controller.close);
closer[0] = false;
var result;
try {
result = start(this, closer);
} catch(err) {
return controller.error(err);
}
return handleResult(result, controller);
}
cancel_(reason) {
cancel(this, reason);
}
static registry = new FinalizationRegistry(deinit);
}
cached.@set(nativeType, Prototype);
}
var instance = new Prototype(nativePtr);
Prototype.registry.register(instance, nativePtr);
var stream = new @ReadableStream(instance);
@putByIdDirectPrivate(stream, "bunNativeType", nativeType);
@putByIdDirectPrivate(stream, "bunNativePtr", nativePtr);
return stream;
}
function cancel(reason)
{
"use strict";
if (!@isReadableStream(this))
return @Promise.@reject(@makeThisTypeError("ReadableStream", "cancel"));
if (@isReadableStreamLocked(this))
return @Promise.@reject(@makeTypeError("ReadableStream is locked"));
return @readableStreamCancel(this, reason);
}
function getReader(options)
{
"use strict";
if (!@isReadableStream(this))
throw @makeThisTypeError("ReadableStream", "getReader");
const mode = @toDictionary(options, { }, "ReadableStream.getReader takes an object as first argument").mode;
if (mode === @undefined)
return new @ReadableStreamDefaultReader(this);
// String conversion is required by spec, hence double equals.
if (mode == 'byob')
return new @ReadableStreamBYOBReader(this);
@throwTypeError("Invalid mode is specified");
}
function pipeThrough(streams, options)
{
"use strict";
const transforms = streams;
const readable = transforms["readable"];
if (!@isReadableStream(readable))
throw @makeTypeError("readable should be ReadableStream");
const writable = transforms["writable"];
const internalWritable = @getInternalWritableStream(writable);
if (!@isWritableStream(internalWritable))
throw @makeTypeError("writable should be WritableStream");
let preventClose = false;
let preventAbort = false;
let preventCancel = false;
let signal;
if (!@isUndefinedOrNull(options)) {
if (!@isObject(options))
throw @makeTypeError("options must be an object");
preventAbort = !!options["preventAbort"];
preventCancel = !!options["preventCancel"];
preventClose = !!options["preventClose"];
signal = options["signal"];
if (signal !== @undefined && !@isAbortSignal(signal))
throw @makeTypeError("options.signal must be AbortSignal");
}
if (!@isReadableStream(this))
throw @makeThisTypeError("ReadableStream", "pipeThrough");
if (@isReadableStreamLocked(this))
throw @makeTypeError("ReadableStream is locked");
if (@isWritableStreamLocked(internalWritable))
throw @makeTypeError("WritableStream is locked");
@readableStreamPipeToWritableStream(this, internalWritable, preventClose, preventAbort, preventCancel, signal);
return readable;
}
function pipeTo(destination)
{
"use strict";
// FIXME: https://bugs.webkit.org/show_bug.cgi?id=159869.
// Built-in generator should be able to parse function signature to compute the function length correctly.
let options = arguments[1];
let preventClose = false;
let preventAbort = false;
let preventCancel = false;
let signal;
if (!@isUndefinedOrNull(options)) {
if (!@isObject(options))
return @Promise.@reject(@makeTypeError("options must be an object"));
try {
preventAbort = !!options["preventAbort"];
preventCancel = !!options["preventCancel"];
preventClose = !!options["preventClose"];
signal = options["signal"];
} catch(e) {
return @Promise.@reject(e);
}
if (signal !== @undefined && !@isAbortSignal(signal))
return @Promise.@reject(@makeTypeError("options.signal must be AbortSignal"));
}
const internalDestination = @getInternalWritableStream(destination);
if (!@isWritableStream(internalDestination))
return @Promise.@reject(@makeTypeError("ReadableStream pipeTo requires a WritableStream"));
if (!@isReadableStream(this))
return @Promise.@reject(@makeThisTypeError("ReadableStream", "pipeTo"));
if (@isReadableStreamLocked(this))
return @Promise.@reject(@makeTypeError("ReadableStream is locked"));
if (@isWritableStreamLocked(internalDestination))
return @Promise.@reject(@makeTypeError("WritableStream is locked"));
return @readableStreamPipeToWritableStream(this, internalDestination, preventClose, preventAbort, preventCancel, signal);
}
function tee()
{
"use strict";
if (!@isReadableStream(this))
throw @makeThisTypeError("ReadableStream", "tee");
return @readableStreamTee(this, false);
}
@getter
function locked()
{
"use strict";
if (!@isReadableStream(this))
throw @makeGetterTypeError("ReadableStream", "locked");
return @isReadableStreamLocked(this);
}
|