Skip to content

Commit 0b3bdd7

Browse files
committed
stream: stricter isReadableNodeStream
Fixes: nodejs#40938
1 parent 7ad052d commit 0b3bdd7

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
isNodeStream,
3131
willEmitClose: _willEmitClose,
3232
} = require('internal/streams/utils');
33+
const console = require('console');
3334

3435
function isRequest(stream) {
3536
return stream.setHeader && typeof stream.abort === 'function';
@@ -52,7 +53,7 @@ function eos(stream, options, callback) {
5253
callback = once(callback);
5354

5455
const readable = options.readable ||
55-
(options.readable !== false && isReadableNodeStream(stream));
56+
(options.readable !== false && isReadableNodeStream(stream, true));
5657
const writable = options.writable ||
5758
(options.writable !== false && isWritableNodeStream(stream));
5859

@@ -75,7 +76,7 @@ function eos(stream, options, callback) {
7576
// this generic check.
7677
let willEmitClose = (
7778
_willEmitClose(stream) &&
78-
isReadableNodeStream(stream) === readable &&
79+
isReadableNodeStream(stream, true) === readable &&
7980
isWritableNodeStream(stream) === writable
8081
);
8182

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)