1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

toAsyncIterable: Remove unnecessary EOF check (#3914)

In denoland/deno#2335 a conditional was added to make sure
toAsyncIterator didn't skip chunks because the reader returned data and
EOF in a single call, fixing #2330.

Later, in denoland/deno#2591, the `Reader` interface changed to
`Promise<number | EOF>`. Since the reader no longer returns data and EOF
in a single call, this conditional is not necessary. We can just return
`{ done: true }` when we get `EOF`.

Co-authored-by: Arun Srinivasan <rulfzid@gmail.com>

Co-authored-by: Arun Srinivasan <rulfzid@gmail.com>
This commit is contained in:
Brad Dunbar 2020-02-07 18:51:01 -05:00 committed by GitHub
parent 724e39f13f
commit d7edf393b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -135,28 +135,14 @@ export async function copy(dst: Writer, src: Reader): Promise<number> {
*/
export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array> {
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 `number | EOF`
// 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<Uint8Array> {
return this;
},
async next(): Promise<IteratorResult<Uint8Array>> {
if (sawEof) {
return { value: new Uint8Array(), done: true };
}
const result = await r.read(b);
if (result === EOF) {
sawEof = true;
return { value: new Uint8Array(), done: true };
}