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: fix finished writable/readable state #31527

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/internal/streams/end-of-stream.js
Expand Up @@ -13,6 +13,18 @@ function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';
}

function isReadable(stream) {
return typeof stream.readable === 'boolean' ||
typeof stream.readableEnded === 'boolean' ||
!!stream._readableState;
}
ronag marked this conversation as resolved.
Show resolved Hide resolved

function isWritable(stream) {
return typeof stream.writable === 'boolean' ||
typeof stream.writableEnded === 'boolean' ||
!!stream._writableState;
}

function eos(stream, opts, callback) {
if (arguments.length === 2) {
callback = opts;
Expand All @@ -28,8 +40,10 @@ function eos(stream, opts, callback) {

callback = once(callback);

let readable = opts.readable || (opts.readable !== false && stream.readable);
let writable = opts.writable || (opts.writable !== false && stream.writable);
let readable = opts.readable ||
(opts.readable !== false && isReadable(stream));
let writable = opts.writable ||
(opts.writable !== false && isWritable(stream));

const onlegacyfinish = () => {
if (!stream.writable) onfinish();
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-stream-finished.js
Expand Up @@ -184,3 +184,21 @@ const { promisify } = require('util');
finished(streamLike, common.mustCall);
streamLike.emit('close');
}

{
const writable = new Writable({ write() {} });
writable.writable = false;
writable.destroy();
finished(writable, common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
ronag marked this conversation as resolved.
Show resolved Hide resolved
}));
}

{
const readable = new Readable();
readable.readable = false;
readable.destroy();
finished(readable, common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
}));
}