Skip to content

Commit 49b8c4f

Browse files
authored
stream: stricter isReadableNodeStream
Fixes: #40938 PR-URL: #40941 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 43e1278 commit 49b8c4f

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/internal/streams/end-of-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function eos(stream, options, callback) {
118118
return callback.call(stream, errored);
119119
}
120120

121-
if (readable && !readableFinished) {
121+
if (readable && !readableFinished && isReadableNodeStream(stream, true)) {
122122
if (!isReadableFinished(stream, false))
123123
return callback.call(stream,
124124
new ERR_STREAM_PREMATURE_CLOSE());

lib/internal/streams/utils.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ const {
99
const kDestroyed = Symbol('kDestroyed');
1010
const kIsDisturbed = Symbol('kIsDisturbed');
1111

12-
function isReadableNodeStream(obj) {
12+
function isReadableNodeStream(obj, strict = false) {
1313
return !!(
1414
obj &&
1515
typeof obj.pipe === 'function' &&
1616
typeof obj.on === 'function' &&
17+
(
18+
!strict ||
19+
(typeof obj.pause === 'function' && typeof obj.resume === 'function')
20+
) &&
1721
(!obj._writableState || obj._readableState?.readable !== false) && // Duplex
1822
(!obj._writableState || obj._readableState) // Writable has .pipe.
1923
);

test/parallel/test-stream-finished.js

+17
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,20 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
643643
const s = new Stream();
644644
finished(s, common.mustNotCall());
645645
}
646+
647+
{
648+
const server = http.createServer(common.mustCall(function(req, res) {
649+
fs.createReadStream(__filename).pipe(res);
650+
finished(res, common.mustCall(function(err) {
651+
assert.strictEqual(err, undefined);
652+
}));
653+
})).listen(0, function() {
654+
http.request(
655+
{ method: 'GET', port: this.address().port },
656+
common.mustCall(function(res) {
657+
res.resume();
658+
server.close();
659+
})
660+
).end();
661+
});
662+
}

0 commit comments

Comments
 (0)