Skip to content

Commit

Permalink
stream: stricter isReadableNodeStream
Browse files Browse the repository at this point in the history
Fixes: #40938
  • Loading branch information
ronag committed Nov 23, 2021
1 parent 7ad052d commit 5e91917
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function eos(stream, options, callback) {
return callback.call(stream, errored);
}

if (readable && !readableFinished) {
if (readable && !readableFinished && isReadableNodeStream(stream, true)) {
if (!isReadableFinished(stream, false))
return callback.call(stream,
new ERR_STREAM_PREMATURE_CLOSE());
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ const {
const kDestroyed = Symbol('kDestroyed');
const kIsDisturbed = Symbol('kIsDisturbed');

function isReadableNodeStream(obj) {
function isReadableNodeStream(obj, strict = false) {
return !!(
obj &&
typeof obj.pipe === 'function' &&
typeof obj.on === 'function' &&
(
!strict ||
typeof obj.read === 'function' ||
(typeof obj.pause === 'function' && typeof obj.resume === 'function')
) &&
(!obj._writableState || obj._readableState?.readable !== false) && // Duplex
(!obj._writableState || obj._readableState) // Writable has .pipe.
);
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,19 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
const s = new Stream();
finished(s, common.mustNotCall());
}

{
const server = http.createServer(common.mustCall(function (req, res) {
fs.createReadStream(__filename).pipe(res)
finished(res, common.mustCall(function (err) {
if (err) {
throw err
}
}))
})).listen(0, function () {
http.request({ method: 'GET', port: this.address().port }, common.mustCall(function (res) {
res.resume()
server.close()
})).end()
})
}

0 comments on commit 5e91917

Please sign in to comment.