From 534409d4e7a9e014aac628c93ac516d44da5b689 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 22 Nov 2021 10:32:02 +0100 Subject: [PATCH] stream: fix finished regression when working with legacy Stream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matteo Collina PR-URL: https://github.com/nodejs/node/pull/40858 Reviewed-By: Robert Nagy Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel Reviewed-By: Tobias Nießen Reviewed-By: Benjamin Gruenbaum --- lib/internal/streams/end-of-stream.js | 4 ++-- test/parallel/test-stream-finished.js | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index c224b44eac6631..ab60f5a7ea78f1 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -165,13 +165,13 @@ function eos(stream, options, callback) { } else if ( !readable && (!willEmitClose || isReadable(stream)) && - (writableFinished || !isWritable(stream)) + (writableFinished || isWritable(stream) === false) ) { process.nextTick(onclose); } else if ( !writable && (!willEmitClose || isWritable(stream)) && - (readableFinished || !isReadable(stream)) + (readableFinished || isReadable(stream) === false) ) { process.nextTick(onclose); } else if ((rState && stream.req && stream.aborted)) { diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index 8ada0c4c348cb7..570acded584b7d 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -7,7 +7,8 @@ const { Transform, finished, Duplex, - PassThrough + PassThrough, + Stream, } = require('stream'); const assert = require('assert'); const EE = require('events'); @@ -630,3 +631,11 @@ testClosed((opts) => new Writable({ write() {}, ...opts })); })); })); } + +{ + // Legacy Streams do not inherit from Readable or Writable. + // We cannot really assume anything about them, so we cannot close them + // automatically. + const s = new Stream(); + finished(s, common.mustNotCall()); +}