-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnatives_blob.bin
More file actions
2601 lines (2600 loc) · 81.4 KB
/
Copy pathnatives_blob.bin
File metadata and controls
2601 lines (2600 loc) · 81.4 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@CommonOperationsU
(function(global, binding, v8) {
'use strict';
const _queue = v8.createPrivateSymbol('[[queue]]');
const _queueTotalSize = v8.createPrivateSymbol('[[queueTotalSize]]');
const _isSettled = v8.createPrivateSymbol('isSettled');
const Boolean = global.Boolean;
const Number = global.Number;
const Number_isFinite = Number.isFinite;
const Number_isNaN = Number.isNaN;
const RangeError = global.RangeError;
const TypeError = global.TypeError;
const TypeError_prototype = TypeError.prototype;
const hasOwnProperty = v8.uncurryThis(global.Object.hasOwnProperty);
const getPrototypeOf = global.Object.getPrototypeOf.bind(global.Object);
const getOwnPropertyDescriptor =
global.Object.getOwnPropertyDescriptor.bind(global.Object);
const thenPromise = v8.uncurryThis(Promise.prototype.then);
const JSON_parse = global.JSON.parse.bind(global.JSON);
const JSON_stringify = global.JSON.stringify.bind(global.JSON);
function hasOwnPropertyNoThrow(x, property) {
return Boolean(x) && hasOwnProperty(x, property);
}
function streamInternalError() {
throw new RangeError('Stream API Internal Error');
}
function createPromise() {
const p = v8.createPromise();
p[_isSettled] = false;
return p;
}
function rejectPromise(p, reason) {
if (!v8.isPromise(p)) {
streamInternalError();
}
if (p[_isSettled] !== false) {
return;
}
p[_isSettled] = true;
v8.rejectPromise(p, reason);
}
function createRejectedPromise(reason) {
const p = createPromise();
rejectPromise(p, reason);
return p;
}
function resolvePromise(p, value) {
if (!v8.isPromise(p)) {
streamInternalError();
}
if (p[_isSettled] !== false) {
return;
}
p[_isSettled] = true;
v8.resolvePromise(p, value);
}
function createResolvedPromise(value) {
if (v8.isPromise(value)) {
return value;
}
const p = createPromise();
resolvePromise(p, value);
return p;
}
function markPromiseAsHandled(p) {
if (!v8.isPromise(p)) {
streamInternalError();
}
v8.markPromiseAsHandled(p);
}
function promiseState(p) {
if (!v8.isPromise(p)) {
streamInternalError();
}
return v8.promiseState(p);
}
function DequeueValue(container) {
const pair = container[_queue].shift();
container[_queueTotalSize] -= pair.size;
if (container[_queueTotalSize] < 0) {
container[_queueTotalSize] = 0;
}
return pair.value;
}
function EnqueueValueWithSize(container, value, size) {
size = Number(size);
if (!IsFiniteNonNegativeNumber(size)) {
throw new RangeError(binding.streamErrors.invalidSize);
}
container[_queue].push({value, size});
container[_queueTotalSize] += size;
}
function PeekQueueValue(container) {
const pair = container[_queue].peek();
return pair.value;
}
function ResetQueue(container) {
container[_queue] = new binding.SimpleQueue();
container[_queueTotalSize] = 0;
}
function IsFiniteNonNegativeNumber(v) {
return Number_isFinite(v) && v >= 0;
}
function ValidateAndNormalizeHighWaterMark(highWaterMark) {
highWaterMark = Number(highWaterMark);
if (Number_isNaN(highWaterMark)) {
throw new RangeError(binding.streamErrors.invalidHWM);
}
if (highWaterMark < 0) {
throw new RangeError(binding.streamErrors.invalidHWM);
}
return highWaterMark;
}
function MakeSizeAlgorithmFromSizeFunction(size) {
if (size === undefined) {
return () => 1;
}
if (typeof size !== 'function') {
throw new TypeError(binding.streamErrors.sizeNotAFunction);
}
return size;
}
const callFunction = v8.uncurryThis(global.Function.prototype.call);
const errTmplMustBeFunctionOrUndefined = name =>
`${name} must be a function or undefined`;
const Function_bind = v8.uncurryThis(global.Function.prototype.bind);
function resolveMethod(O, P, nameForError) {
const method = O[P];
if (typeof method !== 'function' && typeof method !== 'undefined') {
throw new TypeError(errTmplMustBeFunctionOrUndefined(nameForError));
}
return method;
}
function CreateAlgorithmFromUnderlyingMethod(
underlyingObject, methodName, algoArgCount, methodNameForError) {
const method =
resolveMethod(underlyingObject, methodName, methodNameForError);
if (method === undefined) {
return () => createResolvedPromise();
}
if (algoArgCount === 0) {
return Function_bind(PromiseCall0, undefined, method, underlyingObject);
}
return Function_bind(PromiseCall1, undefined, method, underlyingObject);
}
function CreateAlgorithmFromUnderlyingMethodPassingController(
underlyingObject, methodName, algoArgCount, controller,
methodNameForError) {
const method =
resolveMethod(underlyingObject, methodName, methodNameForError);
if (method === undefined) {
return () => createResolvedPromise();
}
if (algoArgCount === 0) {
return Function_bind(
PromiseCall1, undefined, method, underlyingObject, controller);
}
return arg => PromiseCall2(method, underlyingObject, arg, controller);
}
function CallOrNoop1(O, P, arg0, nameForError) {
const method = resolveMethod(O, P, nameForError);
if (method === undefined) {
return undefined;
}
return callFunction(method, O, arg0);
}
function PromiseCall0(F, V) {
try {
return createResolvedPromise(callFunction(F, V));
} catch (e) {
return createRejectedPromise(e);
}
}
function PromiseCall1(F, V, arg0) {
try {
return createResolvedPromise(callFunction(F, V, arg0));
} catch (e) {
return createRejectedPromise(e);
}
}
function PromiseCall2(F, V, arg0, arg1) {
try {
return createResolvedPromise(callFunction(F, V, arg0, arg1));
} catch (e) {
return createRejectedPromise(e);
}
}
const kPull = 1;
const kCancel = 2;
const kChunk = 3;
const kClose = 4;
const kAbort = 5;
const kError = 6;
function isATypeError(object) {
return object !== null && getPrototypeOf(object) === TypeError_prototype;
}
function isADOMException(object) {
try {
callFunction(binding.DOMException_name_get, object);
return true;
} catch (e) {
return false;
}
}
function packReason(reason) {
switch (typeof reason) {
case 'string':
case 'number':
case 'boolean':
return {encoder: 'json', string: JSON_stringify(reason)};
case 'object':
try {
if (isATypeError(reason)) {
let message;
const descriptor = getOwnPropertyDescriptor(reason, 'message');
if (descriptor) {
message = descriptor.value;
if (typeof message !== 'string') {
message = undefined;
}
}
return {encoder: 'typeerror', string: message};
}
if (isADOMException(reason)) {
const message =
callFunction(binding.DOMException_message_get, reason);
const name = callFunction(binding.DOMException_name_get, reason);
return {
encoder: 'domexception',
string: JSON_stringify({message, name})
};
}
return {encoder: 'json', string: JSON_stringify(reason)};
} catch (e) {
return {encoder: 'typeerror', string: 'Cannot transfer message'};
}
default:
return {encoder: 'undefined', string: undefined};
}
}
function unpackReason(packedReason) {
const {encoder, string} = packedReason;
switch (encoder) {
case 'json':
return JSON_parse(string);
case 'typeerror':
return new TypeError(string);
case 'domexception':
const {message, name} = JSON_parse(string);
return new binding.DOMException(message, name);
case 'undefined':
return undefined;
}
}
function CreateCrossRealmTransformWritable(port) {
let backpressurePromise = createPromise();
callFunction(binding.EventTarget_addEventListener, port, 'message', evt => {
const {type, value} = callFunction(binding.MessageEvent_data_get, evt);
switch (type) {
case kPull:
resolvePromise(backpressurePromise);
backpressurePromise = undefined;
break;
case kCancel:
case kError:
binding.WritableStreamDefaultControllerErrorIfNeeded(
controller, unpackReason(value));
if (backpressurePromise !== undefined) {
resolvePromise(backpressurePromise);
backpressurePromise = undefined;
}
break;
}
});
callFunction(
binding.EventTarget_addEventListener, port, 'messageerror', () => {
const error = new binding.DOMException('chunk could not be cloned',
'DataCloneError');
callFunction(binding.MessagePort_postMessage, port,
{type: kError, value: packReason(error)});
callFunction(binding.MessagePort_close, port);
binding.WritableStreamDefaultControllerErrorIfNeeded(controller,
error);
});
callFunction(binding.MessagePort_start, port);
function doWrite(chunk) {
backpressurePromise = createPromise();
try {
callFunction(
binding.MessagePort_postMessage, port,
{type: kChunk, value: chunk});
} catch (e) {
callFunction(
binding.MessagePort_postMessage, port,
{type: kError, value: packReason(e)});
callFunction(binding.MessagePort_close, port);
throw e;
}
}
const stream = binding.CreateWritableStream(
() => undefined,
chunk => {
if (!backpressurePromise) {
return PromiseCall1(doWrite, null, chunk);
}
return thenPromise(backpressurePromise, () => doWrite(chunk));
},
() => {
callFunction(
binding.MessagePort_postMessage, port,
{type: kClose, value: undefined});
callFunction(binding.MessagePort_close, port);
return createResolvedPromise();
},
reason => {
callFunction(
binding.MessagePort_postMessage, port,
{type: kAbort, value: packReason(reason)});
callFunction(binding.MessagePort_close, port);
return createResolvedPromise();
});
const controller = binding.getWritableStreamController(stream);
return stream;
}
function CreateCrossRealmTransformReadable(port) {
let backpressurePromise = createPromise();
let finished = false;
callFunction(binding.EventTarget_addEventListener, port, 'message', evt => {
const {type, value} = callFunction(binding.MessageEvent_data_get, evt);
if (finished) {
return;
}
switch (type) {
case kChunk:
binding.ReadableStreamDefaultControllerEnqueue(controller, value);
resolvePromise(backpressurePromise);
backpressurePromise = createPromise();
break;
case kClose:
finished = true;
binding.ReadableStreamDefaultControllerClose(controller);
callFunction(binding.MessagePort_close, port);
break;
case kAbort:
case kError:
finished = true;
binding.ReadableStreamDefaultControllerError(
controller, unpackReason(value));
callFunction(binding.MessagePort_close, port);
break;
}
});
callFunction(
binding.EventTarget_addEventListener, port, 'messageerror', () => {
const error = new binding.DOMException('chunk could not be cloned',
'DataCloneError');
callFunction(binding.MessagePort_postMessage, port,
{type: kError, value: packReason(error)});
callFunction(binding.MessagePort_close, port);
binding.ReadableStreamDefaultControllerError(controller, error);
});
callFunction(binding.MessagePort_start, port);
const stream = binding.CreateReadableStream(
() => undefined,
() => {
callFunction(
binding.MessagePort_postMessage, port,
{type: kPull, value: undefined});
return backpressurePromise;
},
reason => {
finished = true;
callFunction(
binding.MessagePort_postMessage, port,
{type: kCancel, value: packReason(reason)});
callFunction(binding.MessagePort_close, port);
return createResolvedPromise();
},
0);
const controller = binding.getReadableStreamController(stream);
return stream;
}
binding.streamOperations = {
_queue,
_queueTotalSize,
createPromise,
createRejectedPromise,
createResolvedPromise,
hasOwnPropertyNoThrow,
rejectPromise,
resolvePromise,
markPromiseAsHandled,
promiseState,
CreateAlgorithmFromUnderlyingMethod,
CreateAlgorithmFromUnderlyingMethodPassingController,
CreateCrossRealmTransformWritable,
CreateCrossRealmTransformReadable,
DequeueValue,
EnqueueValueWithSize,
PeekQueueValue,
ResetQueue,
ValidateAndNormalizeHighWaterMark,
MakeSizeAlgorithmFromSizeFunction,
CallOrNoop1,
PromiseCall2
};
});
4CommonStringså
(function(global, binding, v8) {
'use strict';
binding.streamErrors = {
cannotTransferLockedStream: 'Cannot transfer a locked stream',
cannotTransferContext: 'Cannot transfer from this context',
illegalInvocation: 'Illegal invocation',
illegalConstructor: 'Illegal constructor',
invalidType: 'Invalid type is specified',
invalidSize: 'The return value of a queuing strategy\'s size function ' +
'must be a finite, non-NaN, non-negative number',
sizeNotAFunction: 'A queuing strategy\'s size property must be a function',
invalidHWM:
'A queueing strategy\'s highWaterMark property must be a nonnegative, ' +
'non-NaN number',
};
});
,SimpleQueueý
(function(global, binding, v8) {
'use strict';
const _front = v8.createPrivateSymbol('front');
const _back = v8.createPrivateSymbol('back');
const _cursor = v8.createPrivateSymbol('cursor');
const _size = v8.createPrivateSymbol('size');
const _elements = v8.createPrivateSymbol('elements');
const _next = v8.createPrivateSymbol('next');
const RangeError = global.RangeError;
function requireNonEmptyQueue(queue, functionName) {
if (queue[_size] === 0) {
throw new RangeError(
`${functionName}() must not be called on an empty queue`);
}
}
const QUEUE_MAX_ARRAY_SIZE = 16384;
class SimpleQueue {
constructor() {
this[_front] = {
[_elements]: new v8.InternalPackedArray(),
[_next]: undefined,
};
this[_back] = this[_front];
this[_cursor] = 0;
this[_size] = 0;
}
get length() {
return this[_size];
}
push(element) {
const oldBack = this[_back];
let newBack = oldBack;
if (oldBack[_elements].length === QUEUE_MAX_ARRAY_SIZE - 1) {
newBack = {
[_elements]: new v8.InternalPackedArray(),
[_next]: undefined,
};
}
oldBack[_elements].push(element);
if (newBack !== oldBack) {
this[_back] = newBack;
oldBack[_next] = newBack;
}
++this[_size];
}
shift() {
requireNonEmptyQueue(this, 'shift');
const oldFront = this[_front];
let newFront = oldFront;
const oldCursor = this[_cursor];
let newCursor = oldCursor + 1;
const elements = oldFront[_elements];
const element = elements[oldCursor];
if (newCursor === QUEUE_MAX_ARRAY_SIZE) {
newFront = oldFront[_next];
newCursor = 0;
}
--this[_size];
this[_cursor] = newCursor;
if (oldFront !== newFront) {
this[_front] = newFront;
}
elements[oldCursor] = undefined;
return element;
}
forEach(callback) {
let i = this[_cursor];
let node = this[_front];
let elements = node[_elements];
while (i !== elements.length || node[_next] !== undefined) {
if (i === elements.length) {
node = node[_next];
elements = node[_elements];
i = 0;
if (elements.length === 0) {
break;
}
}
callback(elements[i]);
++i;
}
}
peek() {
requireNonEmptyQueue(this, 'peek');
const front = this[_front];
const cursor = this[_cursor];
return front[_elements][cursor];
}
}
binding.SimpleQueue = SimpleQueue;
});
dByteLengthQueuingStrategy
(function(global, binding, v8) {
'use strict';
const defineProperty = global.Object.defineProperty;
class ByteLengthQueuingStrategy {
constructor(options) {
defineProperty(this, 'highWaterMark', {
value: options.highWaterMark,
enumerable: true,
configurable: true,
writable: true
});
}
size(chunk) {
return chunk.byteLength;
}
}
defineProperty(global, 'ByteLengthQueuingStrategy', {
value: ByteLengthQueuingStrategy,
enumerable: false,
configurable: true,
writable: true
});
});
PCountQueuingStrategy
(function(global, binding, v8) {
'use strict';
const defineProperty = global.Object.defineProperty;
class CountQueuingStrategy {
constructor(options) {
defineProperty(this, 'highWaterMark', {
value: options.highWaterMark,
enumerable: true,
configurable: true,
writable: true
});
}
size() {
return 1;
}
}
defineProperty(global, 'CountQueuingStrategy', {
value: CountQueuingStrategy,
enumerable: false,
configurable: true,
writable: true
});
class BuiltInCountQueuingStrategy {
constructor(highWaterMark) {
defineProperty(this, 'highWaterMark', {value: highWaterMark});
}
size() {
return 1;
}
}
binding.createBuiltInCountQueuingStrategy = highWaterMark =>
new BuiltInCountQueuingStrategy(highWaterMark);
});
8ReadableStream¸
(function(global, binding, v8) {
'use strict';
const _reader = v8.createPrivateSymbol('[[reader]]');
const _storedError = v8.createPrivateSymbol('[[storedError]]');
const _controller = v8.createPrivateSymbol('[[controller]]');
const _closedPromise = v8.createPrivateSymbol('[[closedPromise]]');
const _ownerReadableStream =
v8.createPrivateSymbol('[[ownerReadableStream]]');
const _readRequests = v8.createPrivateSymbol('[[readRequests]]');
const createWithExternalControllerSentinel =
v8.createPrivateSymbol('flag for UA-created ReadableStream to pass');
const _readableStreamBits =
v8.createPrivateSymbol('bit field for [[state]] and [[disturbed]]');
const DISTURBED = 0b1;
const STATE_MASK = 0b110;
const STATE_BITS_OFFSET = 1;
const STATE_READABLE = 0;
const STATE_CLOSED = 1;
const STATE_ERRORED = 2;
const _controlledReadableStream =
v8.createPrivateSymbol('[[controlledReadableStream]]');
const _strategyHWM = v8.createPrivateSymbol('[[strategyHWM]]');
const _readableStreamDefaultControllerBits = v8.createPrivateSymbol(
'bit field for [[started]], [[closeRequested]], [[pulling]], ' +
'[[pullAgain]]');
const internalReadableStreamSymbol = v8.createPrivateSymbol(
'internal ReadableStream in exposed ReadableStream interface');
const _lockNotifyTarget = v8.createPrivateSymbol('[[lockNotifyTarget]]');
const _strategySizeAlgorithm = v8.createPrivateSymbol(
'[[strategySizeAlgorithm]]');
const _pullAlgorithm = v8.createPrivateSymbol('[[pullAlgorithm]]');
const _cancelAlgorithm = v8.createPrivateSymbol('[[cancelAlgorithm]]');
const STARTED = 0b1;
const CLOSE_REQUESTED = 0b10;
const PULLING = 0b100;
const PULL_AGAIN = 0b1000;
const BLINK_LOCK_NOTIFICATIONS = 0b10000;
const ObjectCreate = global.Object.create;
const callFunction = v8.uncurryThis(global.Function.prototype.call);
const applyFunction = v8.uncurryThis(global.Function.prototype.apply);
const TypeError = global.TypeError;
const RangeError = global.RangeError;
const String = global.String;
const Promise = global.Promise;
const thenPromise = v8.uncurryThis(Promise.prototype.then);
const {
_queue,
_queueTotalSize,
createPromise,
createRejectedPromise,
createResolvedPromise,
hasOwnPropertyNoThrow,
rejectPromise,
resolvePromise,
markPromiseAsHandled,
CallOrNoop1,
CreateAlgorithmFromUnderlyingMethod,
CreateAlgorithmFromUnderlyingMethodPassingController,
CreateCrossRealmTransformReadable,
CreateCrossRealmTransformWritable,
DequeueValue,
EnqueueValueWithSize,
MakeSizeAlgorithmFromSizeFunction,
ValidateAndNormalizeHighWaterMark,
} = binding.streamOperations;
const streamErrors = binding.streamErrors;
const errEnqueueCloseRequestedStream =
'Cannot enqueue a chunk into a readable stream that is closed or ' +
'has been requested to be closed';
const errCancelReleasedReader =
'This readable stream reader has been released and cannot be used ' +
'to cancel its previous owner stream';
const errReadReleasedReader =
'This readable stream reader has been released and cannot be used ' +
'to read from its previous owner stream';
const errCloseCloseRequestedStream =
'Cannot close a readable stream that has already been requested to ' +
'be closed';
const errEnqueueClosedStream =
'Cannot enqueue a chunk into a closed readable stream';
const errEnqueueErroredStream =
'Cannot enqueue a chunk into an errored readable stream';
const errCloseClosedStream = 'Cannot close a closed readable stream';
const errCloseErroredStream = 'Cannot close an errored readable stream';
const errReaderConstructorBadArgument =
'ReadableStreamReader constructor argument is not a readable stream';
const errReaderConstructorStreamAlreadyLocked =
'ReadableStreamReader constructor can only accept readable streams ' +
'that are not yet locked to a reader';
const errReleaseReaderWithPendingRead =
'Cannot release a readable stream reader when it still has ' +
'outstanding read() calls that have not yet settled';
const errReleasedReaderClosedPromise =
'This readable stream reader has been released and cannot be used ' +
'to monitor the stream\'s state';
const errDestinationStreamClosed = 'Destination stream closed';
let useCounted = false;
class ReadableStream {
constructor(underlyingSource = {}, strategy = {},
internalArgument = undefined) {
const enableBlinkLockNotifications =
internalArgument === createWithExternalControllerSentinel;
if (!useCounted && !enableBlinkLockNotifications) {
binding.countUse('ReadableStreamConstructor');
useCounted = true;
}
InitializeReadableStream(this);
const size = strategy.size;
let highWaterMark = strategy.highWaterMark;
const type = underlyingSource.type;
const typeString = String(type);
if (typeString === 'bytes') {
throw new RangeError('bytes type is not yet implemented');
}
if (type !== undefined) {
throw new RangeError(streamErrors.invalidType);
}
const sizeAlgorithm = MakeSizeAlgorithmFromSizeFunction(size);
if (highWaterMark === undefined) {
highWaterMark = 1;
}
highWaterMark = ValidateAndNormalizeHighWaterMark(highWaterMark);
SetUpReadableStreamDefaultControllerFromUnderlyingSource(
this, underlyingSource, highWaterMark, sizeAlgorithm,
enableBlinkLockNotifications);
}
}
const ReadableStream_prototype = ReadableStream.prototype;
function ReadableStreamPipeTo(
readable, dest, preventClose, preventAbort, preventCancel) {
const reader = AcquireReadableStreamDefaultReader(readable);
const writer = binding.AcquireWritableStreamDefaultWriter(dest);
let shuttingDown = false;
const promise = createPromise();
let reading = false;
let lastWrite;
if (checkInitialState()) {
thenPromise(reader[_closedPromise], onReaderClosed, readableError);
thenPromise(
binding.getWritableStreamDefaultWriterClosedPromise(writer),
undefined, writableError);
pump();
}
function checkInitialState() {
const state = ReadableStreamGetState(readable);
if (state === STATE_ERRORED) {
readableError(readable[_storedError]);
return false;
}
if (binding.isWritableStreamErrored(dest)) {
writableError(binding.getWritableStreamStoredError(dest));
return false;
}
if (state === STATE_CLOSED) {
readableClosed();
return false;
}
if (binding.isWritableStreamClosingOrClosed(dest)) {
writableStartedClosed();
return false;
}
return true;
}
function pump() {
if (shuttingDown) {
return;
}
const desiredSize =
binding.WritableStreamDefaultWriterGetDesiredSize(writer);
if (desiredSize === null) {
return;
}
if (desiredSize <= 0) {
thenPromise(
binding.getWritableStreamDefaultWriterReadyPromise(writer), pump,
writableError);
return;
}
reading = true;
thenPromise(
ReadableStreamDefaultReaderRead(reader), readFulfilled, readRejected);
}
function readFulfilled({value, done}) {
reading = false;
if (done) {
readableClosed();
return;
}
const write = binding.WritableStreamDefaultWriterWrite(writer, value);
lastWrite = write;
thenPromise(write, undefined, writableError);
pump();
}
function readRejected() {
reading = false;
readableError(readable[_storedError]);
}
function onReaderClosed() {
if (!reading) {
readableClosed();
}
}
function readableError(error) {
if (!preventAbort) {
shutdownWithAction(
binding.WritableStreamAbort, [dest, error], error, true);
} else {
shutdown(error, true);
}
}
function writableError(error) {
if (!preventCancel) {
shutdownWithAction(
ReadableStreamCancel, [readable, error], error, true);
} else {
shutdown(error, true);
}
}
function readableClosed() {
if (!preventClose) {
shutdownWithAction(
binding.WritableStreamDefaultWriterCloseWithErrorPropagation,
[writer]);
} else {
shutdown();
}
}
function writableStartedClosed() {
const destClosed = new TypeError(errDestinationStreamClosed);
if (!preventCancel) {
shutdownWithAction(
ReadableStreamCancel, [readable, destClosed], destClosed, true);
} else {
shutdown(destClosed, true);
}
}
function shutdownWithAction(
action, args, originalError = undefined, errorGiven = false) {
if (shuttingDown) {
return;
}
shuttingDown = true;
let p;
if (shouldWriteQueuedChunks()) {
p = thenPromise(writeQueuedChunks(),
() => applyFunction(action, undefined, args));
} else {
p = applyFunction(action, undefined, args);
}
thenPromise(
p, () => finalize(originalError, errorGiven),
newError => finalize(newError, true));
}
function shutdown(error = undefined, errorGiven = false) {
if (shuttingDown) {
return;
}
shuttingDown = true;
if (shouldWriteQueuedChunks()) {
thenPromise(writeQueuedChunks(), () => finalize(error, errorGiven));
} else {
finalize(error, errorGiven);
}
}
function finalize(error, errorGiven) {
binding.WritableStreamDefaultWriterRelease(writer);
ReadableStreamReaderGenericRelease(reader);
if (errorGiven) {
rejectPromise(promise, error);
} else {
resolvePromise(promise, undefined);
}
}
function shouldWriteQueuedChunks() {
return binding.isWritableStreamWritable(dest) &&
!binding.WritableStreamCloseQueuedOrInFlight(dest);
}
function writeQueuedChunks() {
if (lastWrite) {
return thenPromise(lastWrite, () => undefined, () => undefined);
}
return createResolvedPromise(undefined);
}
return promise;
}
function AcquireReadableStreamDefaultReader(stream) {
return new ReadableStreamDefaultReader(stream);
}
function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm,
highWaterMark, sizeAlgorithm,
enableBlinkLockNotifications) {
if (highWaterMark === undefined) {
highWaterMark = 1;
}
if (sizeAlgorithm === undefined) {
sizeAlgorithm = () => 1;
}
const stream = ObjectCreate(ReadableStream_prototype);
InitializeReadableStream(stream);
const controller = ObjectCreate(ReadableStreamDefaultController_prototype);
SetUpReadableStreamDefaultController(
stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm,
highWaterMark, sizeAlgorithm, enableBlinkLockNotifications);
return stream;
}
function InitializeReadableStream(stream) {
stream[_readableStreamBits] = 0b0;
ReadableStreamSetState(stream, STATE_READABLE);
stream[_reader] = undefined;
stream[_storedError] = undefined;
}
function IsReadableStream(x) {
return hasOwnPropertyNoThrow(x, _controller);
}
function IsReadableStreamDisturbed(stream) {
return stream[_readableStreamBits] & DISTURBED;
}
function IsReadableStreamLocked(stream) {
return stream[_reader] !== undefined;
}
function ReadableStreamTee(stream) {
const reader = AcquireReadableStreamDefaultReader(stream);
let closedOrErrored = false;
let canceled1 = false;
let canceled2 = false;
let reason1;
let reason2;
const cancelPromise = createPromise();
function pullAlgorithm() {
return thenPromise(
ReadableStreamDefaultReaderRead(reader), ({value, done}) => {
if (done && !closedOrErrored) {
if (!canceled1) {
ReadableStreamDefaultControllerClose(branch1controller);
}
if (!canceled2) {
ReadableStreamDefaultControllerClose(branch2controller);
}
closedOrErrored = true;
}
if (closedOrErrored) {
return;
}
if (!canceled1) {
ReadableStreamDefaultControllerEnqueue(branch1controller, value);
}
if (!canceled2) {
ReadableStreamDefaultControllerEnqueue(branch2controller, value);
}
});
}
function cancel1Algorithm(reason) {
canceled1 = true;
reason1 = reason;
if (canceled2) {
const cancelResult = ReadableStreamCancel(stream, [reason1, reason2]);
resolvePromise(cancelPromise, cancelResult);
}
return cancelPromise;
}
function cancel2Algorithm(reason) {
canceled2 = true;
reason2 = reason;
if (canceled1) {
const cancelResult = ReadableStreamCancel(stream, [reason1, reason2]);
resolvePromise(cancelPromise, cancelResult);
}
return cancelPromise;
}
const startAlgorithm = () => undefined;
const branch1Stream = CreateReadableStream(
startAlgorithm, pullAlgorithm, cancel1Algorithm, undefined, undefined,
false);
const branch2Stream = CreateReadableStream(
startAlgorithm, pullAlgorithm, cancel2Algorithm, undefined, undefined,
false);
const branch1controller = branch1Stream[_controller];
const branch2controller = branch2Stream[_controller];
thenPromise(reader[_closedPromise], undefined, r => {
if (closedOrErrored === true) {
return;
}
ReadableStreamDefaultControllerError(branch1controller, r);
ReadableStreamDefaultControllerError(branch2controller, r);
closedOrErrored = true;
});
return [branch1Stream, branch2Stream];
}
function ReadableStreamAddReadRequest(stream, forAuthorCode) {
const promise = createPromise();
stream[_reader][_readRequests].push({promise, forAuthorCode});
return promise;
}
function ReadableStreamCancel(stream, reason) {
stream[_readableStreamBits] |= DISTURBED;
const state = ReadableStreamGetState(stream);
if (state === STATE_CLOSED) {
return createResolvedPromise(undefined);
}
if (state === STATE_ERRORED) {
return createRejectedPromise(stream[_storedError]);
}
ReadableStreamClose(stream);
const sourceCancelPromise =
ReadableStreamDefaultControllerCancel(stream[_controller], reason);
return thenPromise(sourceCancelPromise, () => undefined);
}
function ReadableStreamClose(stream) {
ReadableStreamSetState(stream, STATE_CLOSED);
const reader = stream[_reader];
if (reader === undefined) {
return;
}
if (IsReadableStreamDefaultReader(reader) === true) {
reader[_readRequests].forEach(
request =>
resolvePromise(
request.promise,
ReadableStreamCreateReadResult(undefined, true,
request.forAuthorCode)));
reader[_readRequests] = new binding.SimpleQueue();
}
resolvePromise(reader[_closedPromise], undefined);
}
function ReadableStreamCreateReadResult(value, done, forAuthorCode) {