|
1 | 1 | // `.from()` is expected by `@std/tar` |
2 | | -;(ReadableStream as any).from ??= function <T>(iterator: Iterator<T> | AsyncIterator<T>) { |
3 | | - return new ReadableStream<T>({ |
| 2 | +;(globalThis as any).ReadableStream.from ??= function <T>( |
| 3 | + iterator: Iterator<T> | AsyncIterator<T> |
| 4 | +) { |
| 5 | + return new globalThis.ReadableStream<T>({ |
4 | 6 | async pull(controller) { |
5 | 7 | try { |
6 | 8 | const { value, done } = await iterator.next() |
|
16 | 18 | }) |
17 | 19 | } |
18 | 20 |
|
19 | | -// Some browsers don't yet make `ReadableStream` async iterable (eg. Safari), so polyfill |
20 | | -ReadableStream.prototype.values ??= function <T>({ |
| 21 | +// Some browsers don't make `ReadableStream` async iterable (eg. Safari), so polyfill |
| 22 | +globalThis.ReadableStream.prototype.values ??= function <T>({ |
21 | 23 | preventCancel = false, |
22 | 24 | } = {}): AsyncIterableIterator<T> { |
23 | 25 | const reader = this.getReader() |
@@ -50,4 +52,17 @@ ReadableStream.prototype.values ??= function <T>({ |
50 | 52 | } |
51 | 53 | } |
52 | 54 |
|
53 | | -ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values |
| 55 | +globalThis.ReadableStream.prototype[Symbol.asyncIterator] ??= |
| 56 | + globalThis.ReadableStream.prototype.values |
| 57 | + |
| 58 | +// @std/tar conditionally uses `ReadableStreamBYOBReader` which isn't supported in Safari, |
| 59 | +// so patch `ReadableStream`'s constructor to prevent using BYOB. |
| 60 | +// Webpack's `ProvidePlugin` replaces `ReadableStream` references with this patch |
| 61 | +export default class PatchedReadableStream<T> extends globalThis.ReadableStream<T> { |
| 62 | + constructor(underlyingSource?: UnderlyingSource<T>, strategy?: QueuingStrategy<T>) { |
| 63 | + if (underlyingSource?.type === 'bytes' && !('ReadableStreamBYOBReader' in globalThis)) { |
| 64 | + underlyingSource.type = undefined |
| 65 | + } |
| 66 | + super(underlyingSource, strategy) |
| 67 | + } |
| 68 | +} |
0 commit comments