From b8f9039cfe1e340c8c043dfa4906ba6fafe62ee6 Mon Sep 17 00:00:00 2001 From: jakecastelli <38635403+jakecastelli@users.noreply.github.com> Date: Mon, 5 Aug 2024 22:00:29 +0930 Subject: [PATCH] stream: make checking pendingcb on WritableStream backward compatible PR-URL: https://github.com/nodejs/node/pull/54142 Fixes: https://github.com/nodejs/node/issues/54131 Refs: https://github.com/nodejs/node/issues/54131 Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy --- lib/internal/streams/end-of-stream.js | 2 +- test/parallel/test-stream-finished.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 31b8dfca58cbe3..c1403f419c5ea5 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -215,7 +215,7 @@ function eos(stream, options, callback) { !readable && (!willEmitClose || isReadable(stream)) && (writableFinished || isWritable(stream) === false) && - (wState == null || wState.pendingcb === 0) + (wState == null || wState.pendingcb === undefined || wState.pendingcb === 0) ) { process.nextTick(onclosed); } else if ( diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index 6820ac18cf4b5c..9d66cbe59b11f3 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -687,3 +687,16 @@ testClosed((opts) => new Writable({ write() {}, ...opts })); assert.strictEqual(stream._writableState.pendingcb, 0); })); } + +{ + const stream = new Duplex({ + write(chunk, enc, cb) {} + }); + + stream.end('foo'); + + // Simulate an old stream implementation that doesn't have pendingcb + delete stream._writableState.pendingcb; + + finished(stream, { readable: false }, common.mustCall()); +}