// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of#iterating_over_async_generators | |
export async function* streamToAsyncIterable( | |
stream: ReadableStream<Uint8Array> | |
): AsyncIterableIterator<Uint8Array> { | |
const reader = stream.getReader(); | |
try { | |
while (true) { | |
const { done, value } = await reader.read(); | |
if (done) return; | |
yield value; | |
} | |
} finally { | |
reader.releaseLock(); | |
} | |
} | |