summaryrefslogtreecommitdiff
path: root/js/io.ts
diff options
context:
space:
mode:
Diffstat (limited to 'js/io.ts')
-rw-r--r--js/io.ts27
1 files changed, 27 insertions, 0 deletions
diff --git a/js/io.ts b/js/io.ts
index aa041585b..4e37355bb 100644
--- a/js/io.ts
+++ b/js/io.ts
@@ -115,3 +115,30 @@ export async function copy(dst: Writer, src: Reader): Promise<number> {
}
return n;
}
+
+/**
+ * Turns `r` into async iterator.
+ *
+ * for await (const chunk of readerIterator(reader)) {
+ * console.log(chunk)
+ * }
+ */
+export function toAsyncIterator(
+ r: Reader
+): AsyncIterableIterator<ArrayBufferView> {
+ const b = new Uint8Array(1024);
+
+ return {
+ [Symbol.asyncIterator]() {
+ return this;
+ },
+
+ async next(): Promise<IteratorResult<ArrayBufferView>> {
+ const result = await r.read(b);
+ return {
+ value: b.subarray(0, result.nread),
+ done: result.eof
+ };
+ }
+ };
+}