-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathReadableStream-impl.js
165 lines (138 loc) · 5.51 KB
/
ReadableStream-impl.js
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
'use strict';
const assert = require('assert');
const { newPromise, resolvePromise, rejectPromise, promiseResolvedWith, promiseRejectedWith,
setPromiseIsHandledToTrue } = require('./helpers/webidl.js');
const { ExtractHighWaterMark, ExtractSizeAlgorithm } = require('./abstract-ops/queuing-strategy.js');
const aos = require('./abstract-ops/readable-streams.js');
const wsAOs = require('./abstract-ops/writable-streams.js');
const idlUtils = require('../generated/utils.js');
const UnderlyingSource = require('../generated/UnderlyingSource.js');
exports.implementation = class ReadableStreamImpl {
constructor(globalObject, [underlyingSource, strategy]) {
if (underlyingSource === undefined) {
underlyingSource = null;
}
const underlyingSourceDict = UnderlyingSource.convert(underlyingSource);
aos.InitializeReadableStream(this);
if (underlyingSourceDict.type === 'bytes') {
if ('size' in strategy) {
throw new RangeError('The strategy for a byte stream cannot have a size function');
}
const highWaterMark = ExtractHighWaterMark(strategy, 0);
aos.SetUpReadableByteStreamControllerFromUnderlyingSource(
this, underlyingSource, underlyingSourceDict, highWaterMark
);
} else {
assert(!('type' in underlyingSourceDict) || underlyingSourceDict.type === 'owning');
const sizeAlgorithm = ExtractSizeAlgorithm(strategy);
const highWaterMark = ExtractHighWaterMark(strategy, 1);
aos.SetUpReadableStreamDefaultControllerFromUnderlyingSource(
this, underlyingSource, underlyingSourceDict, highWaterMark, sizeAlgorithm
);
}
}
get locked() {
return aos.IsReadableStreamLocked(this);
}
cancel(reason) {
if (aos.IsReadableStreamLocked(this) === true) {
return promiseRejectedWith(new TypeError('Cannot cancel a stream that already has a reader'));
}
return aos.ReadableStreamCancel(this, reason);
}
getReader(options) {
if (!('mode' in options)) {
return aos.AcquireReadableStreamDefaultReader(this);
}
assert(options.mode === 'byob');
return aos.AcquireReadableStreamBYOBReader(this);
}
pipeThrough(transform, options) {
// Type checking here is needed until https://github.com/jsdom/webidl2js/issues/81 is fixed.
if ('signal' in options) {
if (!isAbortSignal(options.signal)) {
throw new TypeError('Invalid signal argument');
}
}
if (aos.IsReadableStreamLocked(this) === true) {
throw new TypeError('ReadableStream.prototype.pipeThrough cannot be used on a locked ReadableStream');
}
if (wsAOs.IsWritableStreamLocked(transform.writable) === true) {
throw new TypeError('ReadableStream.prototype.pipeThrough cannot be used on a locked WritableStream');
}
const promise = aos.ReadableStreamPipeTo(
this, transform.writable, options.preventClose, options.preventAbort, options.preventCancel, options.signal
);
setPromiseIsHandledToTrue(promise);
return transform.readable;
}
pipeTo(destination, options) {
// Type checking here is needed until https://github.com/jsdom/webidl2js/issues/81 is fixed.
if ('signal' in options) {
if (!isAbortSignal(options.signal)) {
return promiseRejectedWith(new TypeError('Invalid signal argument'));
}
}
if (aos.IsReadableStreamLocked(this) === true) {
return promiseRejectedWith(
new TypeError('ReadableStream.prototype.pipeTo cannot be used on a locked ReadableStream')
);
}
if (wsAOs.IsWritableStreamLocked(destination) === true) {
return promiseRejectedWith(
new TypeError('ReadableStream.prototype.pipeTo cannot be used on a locked WritableStream')
);
}
return aos.ReadableStreamPipeTo(
this, destination, options.preventClose, options.preventAbort, options.preventCancel, options.signal
);
}
tee() {
// Conversion here is only needed until https://github.com/jsdom/webidl2js/pull/108 gets merged.
return aos.ReadableStreamTee(this, false).map(idlUtils.wrapperForImpl);
}
[idlUtils.asyncIteratorInit](iterator, [options]) {
iterator._reader = aos.AcquireReadableStreamDefaultReader(this);
iterator._preventCancel = options.preventCancel;
}
[idlUtils.asyncIteratorNext](iterator) {
const reader = iterator._reader;
assert(reader._stream !== undefined);
const promise = newPromise();
const readRequest = {
chunkSteps: chunk => resolvePromise(promise, chunk),
closeSteps: () => {
aos.ReadableStreamDefaultReaderRelease(reader);
resolvePromise(promise, idlUtils.asyncIteratorEOI);
},
errorSteps: e => {
aos.ReadableStreamDefaultReaderRelease(reader);
rejectPromise(promise, e);
}
};
aos.ReadableStreamDefaultReaderRead(reader, readRequest);
return promise;
}
[idlUtils.asyncIteratorReturn](iterator, arg) {
const reader = iterator._reader;
assert(reader._stream !== undefined);
assert(reader._readRequests.length === 0);
if (iterator._preventCancel === false) {
const result = aos.ReadableStreamReaderGenericCancel(reader, arg);
aos.ReadableStreamDefaultReaderRelease(reader);
return result;
}
aos.ReadableStreamDefaultReaderRelease(reader);
return promiseResolvedWith(undefined);
}
};
// See pipeTo()/pipeThrough() for why this is needed.
const abortedGetter = Object.getOwnPropertyDescriptor(AbortSignal.prototype, 'aborted').get;
function isAbortSignal(v) {
try {
abortedGetter.call(v);
return true;
} catch {
return false;
}
}