From 2c6b93e0a0c7927dc4dd66c1f681a78a6d79b8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 11 May 2019 16:05:56 +0200 Subject: fix: edge case in toAsyncIterator (#2335) --- js/io.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'js/io.ts') diff --git a/js/io.ts b/js/io.ts index 0b59a0849..60b7e442c 100644 --- a/js/io.ts +++ b/js/io.ts @@ -144,6 +144,14 @@ export async function copy(dst: Writer, src: Reader): Promise { */ export function toAsyncIterator(r: Reader): AsyncIterableIterator { const b = new Uint8Array(1024); + // Keep track if end-of-file has been reached, then + // signal that iterator is done during subsequent next() + // call. This is required because `r` can return a `ReadResult` + // with data read and EOF reached. But if iterator returns + // `done` then `value` is discarded. + // + // See https://github.com/denoland/deno/issues/2330 for reference. + let sawEof = false; return { [Symbol.asyncIterator](): AsyncIterableIterator { @@ -151,10 +159,16 @@ export function toAsyncIterator(r: Reader): AsyncIterableIterator { }, async next(): Promise> { + if (sawEof) { + return { value: new Uint8Array(), done: true }; + } + const result = await r.read(b); + sawEof = result.eof; + return { value: b.subarray(0, result.nread), - done: result.eof + done: false }; } }; -- cgit v1.2.3