Skip to content

Commit 5e91917

Browse files
committed
stream: stricter isReadableNodeStream
Fixes: #40938
1 parent 7ad052d commit 5e91917

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ 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.read === 'function' ||
20+
(typeof obj.pause === 'function' && typeof obj.resume === 'function')
21+
) &&
1722
(!obj._writableState || obj._readableState?.readable !== false) && // Duplex
1823
(!obj._writableState || obj._readableState) // Writable has .pipe.
1924
);

test/parallel/test-stream-finished.js

+16
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,19 @@ 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+
if (err) {
652+
throw err
653+
}
654+
}))
655+
})).listen(0, function () {
656+
http.request({ method: 'GET', port: this.address().port }, common.mustCall(function (res) {
657+
res.resume()
658+
server.close()
659+
})).end()
660+
})
661+
}

0 commit comments

Comments
 (0)