| 1 |
import { ReadableStreamLike } from '../types'; |
| 2 |
import { isFunction } from './isFunction'; |
| 3 |
|
| 4 |
export async function* readableStreamLikeToAsyncGenerator<T>(readableStream: ReadableStreamLike<T>): AsyncGenerator<T> { |
| 5 |
const reader = readableStream.getReader(); |
| 6 |
try { |
| 7 |
while (true) { |
| 8 |
const { value, done } = await reader.read(); |
| 9 |
if (done) { |
| 10 |
return; |
| 11 |
} |
| 12 |
yield value!; |
| 13 |
} |
| 14 |
} finally { |
| 15 |
reader.releaseLock(); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
export function isReadableStreamLike<T>(obj: any): obj is ReadableStreamLike<T> { |
| 20 |
// We don't want to use instanceof checks because they would return |
| 21 |
// false for instances from another Realm, like an <iframe>. |
| 22 |
return isFunction(obj?.getReader); |
| 23 |
} |