-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpolyfill-async-iterator.ts
More file actions
53 lines (50 loc) · 1.43 KB
/
polyfill-async-iterator.ts
File metadata and controls
53 lines (50 loc) · 1.43 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
// Copyright (c) 2026 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- skip
// @ts-nocheck
if (typeof Symbol === 'undefined' || !Symbol.asyncIterator) {
Symbol.asyncIterator = Symbol.for('Symbol.asyncIterator');
}
// see https://gist.github.com/MattiasBuelens/496fc1d37adb50a733edd43853f2f60e
if (
typeof ReadableStream !== 'undefined' &&
typeof ReadableStream.prototype.values === 'undefined'
) {
ReadableStream.prototype.values = function ({ preventCancel = false } = {}) {
const reader = this.getReader();
return {
async next() {
try {
const result = await reader.read();
if (result.done) {
reader.releaseLock();
}
return result;
} catch (e) {
reader.releaseLock();
throw e;
}
},
async return(value) {
if (!preventCancel) {
const cancelPromise = reader.cancel(value);
reader.releaseLock();
await cancelPromise;
} else {
reader.releaseLock();
}
return { done: true, value };
},
[Symbol.asyncIterator]() {
return this;
},
};
};
}
if (
typeof ReadableStream !== 'undefined' &&
typeof ReadableStream.prototype[Symbol.asyncIterator] === 'undefined'
) {
ReadableStream.prototype[Symbol.asyncIterator] =
ReadableStream.prototype.values;
}