diff options
author | Robert Nagy <ronagy@icloud.com> | 2021-11-26 18:33:05 +0100 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2021-11-29 10:47:36 +0100 |
commit | 70bd90e039e45a12827a29c7af57c43b88626c35 (patch) | |
tree | 562f28db661c5c5ca4ef5a7c29bc2f9ade547a5d | |
parent | 86d1c0b19d91c9ba6e2869eb7c5dee8262fb8d80 (diff) | |
download | node-new-70bd90e039e45a12827a29c7af57c43b88626c35.tar.gz |
stream: stricter isReadableNodeStream
Fixes: https://github.com/nodejs/node/issues/40938
PR-URL: https://github.com/nodejs/node/pull/40941
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | lib/internal/streams/end-of-stream.js | 2 | ||||
-rw-r--r-- | lib/internal/streams/utils.js | 6 | ||||
-rw-r--r-- | test/parallel/test-stream-finished.js | 17 |
3 files changed, 23 insertions, 2 deletions
diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index ab60f5a7ea..f238b9c973 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -116,7 +116,7 @@ function eos(stream, options, callback) { return callback.call(stream, errored); } - if (readable && !readableFinished) { + if (readable && !readableFinished && isReadableNodeStream(stream, true)) { if (!isReadableFinished(stream, false)) return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE()); diff --git a/lib/internal/streams/utils.js b/lib/internal/streams/utils.js index a77ae4d932..c8456b3b6f 100644 --- a/lib/internal/streams/utils.js +++ b/lib/internal/streams/utils.js @@ -9,11 +9,15 @@ const { const kDestroyed = Symbol('kDestroyed'); const kIsDisturbed = Symbol('kIsDisturbed'); -function isReadableNodeStream(obj) { +function isReadableNodeStream(obj, strict = false) { return !!( obj && typeof obj.pipe === 'function' && typeof obj.on === 'function' && + ( + !strict || + (typeof obj.pause === 'function' && typeof obj.resume === 'function') + ) && (!obj._writableState || obj._readableState?.readable !== false) && // Duplex (!obj._writableState || obj._readableState) // Writable has .pipe. ); diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index 570acded58..17ba3c610f 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -639,3 +639,20 @@ testClosed((opts) => new Writable({ write() {}, ...opts })); const s = new Stream(); finished(s, common.mustNotCall()); } + +{ + const server = http.createServer(common.mustCall(function(req, res) { + fs.createReadStream(__filename).pipe(res); + finished(res, common.mustCall(function(err) { + assert.strictEqual(err, undefined); + })); + })).listen(0, function() { + http.request( + { method: 'GET', port: this.address().port }, + common.mustCall(function(res) { + res.resume(); + server.close(); + }) + ).end(); + }); +} |