Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: stricter isReadableNodeStream #40941

Merged
merged 3 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 5 additions & 1 deletion lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ 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.pause === 'function' && typeof obj.resume === 'function')
) &&
(!obj._writableState || obj._readableState?.readable !== false) && // Duplex
(!obj._writableState || obj._readableState) // Writable has .pipe.
);
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,20 @@ 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) {
assert.strictEqual(err, undefined);
}));
})).listen(0, function() {
http.request(
{ method: 'GET', port: this.address().port },
common.mustCall(function(res) {
res.resume();
server.close();
})
).end();
});
}