feat(http): asReadableStream — AsyncIterable to web ReadableStream
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+53
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import type { StreamEncoder } from '../encoders';
|
||||
|
||||
/** Wraps an AsyncIterable as a web ReadableStream, encoding each value. Cancels the source on stream cancel. */
|
||||
export function asReadableStream(source: AsyncIterable<unknown>, encoder: StreamEncoder): ReadableStream<Uint8Array> {
|
||||
const iterator = source[Symbol.asyncIterator]();
|
||||
const te = new TextEncoder();
|
||||
return new ReadableStream<Uint8Array>({
|
||||
async pull(controller) {
|
||||
try {
|
||||
const { value, done } = await iterator.next();
|
||||
|
||||
if (done) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const encoded = encoder.encode(value);
|
||||
const bytes = typeof encoded === 'string' ? te.encode(encoded) : encoded;
|
||||
controller.enqueue(bytes);
|
||||
} catch (err) {
|
||||
controller.error(err);
|
||||
}
|
||||
},
|
||||
async cancel() {
|
||||
await iterator.return?.();
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user