From 2c9432b69dcb7574768d8900fadd06699da0a246 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 14 Oct 2020 13:05:35 +0200 Subject: [PATCH] stream: async iterator stop read if destroyed Fixes some compatibility issues where it is expected that for await stops reading when the stream is destroyed. Refs: https://github.com/nodejs/node/pull/34887 --- lib/internal/streams/readable.js | 2 +- test/parallel/test-stream-readable-async-iterators.js | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index 275fef2c676361..43b53447d8734e 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -1095,7 +1095,7 @@ async function* createAsyncIterator(stream) { try { while (true) { - const chunk = stream.read(); + const chunk = stream.destroyed ? null : stream.read(); if (chunk !== null) { yield chunk; } else if (errorEmitted) { diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index c2fde72457f22f..788ed8a8e2e0e0 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -43,8 +43,6 @@ async function tests() { }); const iter = Readable.prototype[Symbol.asyncIterator].call(stream); - await iter.next(); - await iter.next(); await iter.next() .then(common.mustNotCall()) .catch(common.mustCall((err) => {