-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.browser.js
41 lines (38 loc) · 1.09 KB
/
index.browser.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
export const {
ReadableStream,
ReadableStreamDefaultReader,
ReadableStreamBYOBReader,
ReadableStreamBYOBRequest,
ReadableByteStreamController,
ReadableStreamDefaultController,
TransformStream,
TransformStreamDefaultController,
WritableStream,
WritableStreamDefaultWriter,
WritableStreamDefaultController,
ByteLengthQueuingStrategy,
CountQueuingStrategy,
TextEncoderStream,
TextDecoderStream,
} = window;
// Polyfill to make ReadableStream async-iterable with for-await
// https://jakearchibald.com/2017/async-iterators-and-generators/#making-streams-iterate
if (!ReadableStream.prototype[Symbol.asyncIterator]) {
async function* streamAsyncIterator() {
// Get a lock on the stream
const reader = this.getReader();
try {
while (true) {
// Read from the stream
const { done, value } = await reader.read();
// Exit if we're done
if (done) return;
// Else yield the chunk
yield value;
}
} finally {
reader.releaseLock();
}
}
ReadableStream.prototype[Symbol.asyncIterator] = streamAsyncIterator;
}