diff options
author | Robert Nagy <ronagy@icloud.com> | 2019-08-17 10:51:38 +0200 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2019-08-23 08:58:10 +0200 |
commit | 4e188b3c63f0488be03a1f723b9b50720c76fed4 (patch) | |
tree | b0d6f4c71fbb1a42818fb2e2cf68ca936774f7de /lib | |
parent | 2fc87685d90ea9a8ad21d2f4515eaf329d71b3f1 (diff) | |
download | android-node-v8-4e188b3c63f0488be03a1f723b9b50720c76fed4.tar.gz android-node-v8-4e188b3c63f0488be03a1f723b9b50720c76fed4.tar.bz2 android-node-v8-4e188b3c63f0488be03a1f723b9b50720c76fed4.zip |
stream: async iterator destroy compat
async iterator should not depend on internal API for better compat
with streamlike objects.
PR-URL: https://github.com/nodejs/node/pull/29176
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/streams/async_iterator.js | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/internal/streams/async_iterator.js b/lib/internal/streams/async_iterator.js index 0161ca98ef..07f2191e71 100644 --- a/lib/internal/streams/async_iterator.js +++ b/lib/internal/streams/async_iterator.js @@ -110,17 +110,26 @@ const ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf({ }, return() { - // destroy(err, cb) is a private API. - // We can guarantee we have that here, because we control the - // Readable class this is attached to. return new Promise((resolve, reject) => { - this[kStream].destroy(null, (err) => { - if (err) { + const stream = this[kStream]; + + // TODO(ronag): Remove this check once finished() handles + // already ended and/or destroyed streams. + const ended = stream.destroyed || stream.readableEnded || + (stream._readableState && stream._readableState.endEmitted); + if (ended) { + resolve(createIterResult(undefined, true)); + return; + } + + finished(stream, (err) => { + if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { reject(err); - return; + } else { + resolve(createIterResult(undefined, true)); } - resolve(createIterResult(undefined, true)); }); + stream.destroy(); }); }, }, AsyncIteratorPrototype); |