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: eos more accurate writable and readable detection #29409

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
stream: eos more accurate writable and readable detection
The value of stream.readable and stream.writable should not
be used to detect whether a stream is Writable or Readable.

Refs: #29395
  • Loading branch information
ronag committed Sep 3, 2019
commit a26c3a0f00a1c4942faca98e4cc176c7894d2530
16 changes: 14 additions & 2 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
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
}

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 Down Expand Up @@ -47,8 +59,8 @@ function eos(stream, opts, 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
48 changes: 48 additions & 0 deletions test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,51 @@ const { promisify } = require('util');
}));
r.destroy();
}

{
// Test is readable check through readable
const streamLike = new EE();
streamLike.readable = false;
finished(streamLike, common.mustCall());
streamLike.emit('end');
}

{
// Test is readable check through readableEnded
const streamLike = new EE();
streamLike.readableEnded = true;
finished(streamLike, common.mustCall());
streamLike.emit('end');
}

{
// Test is readable check through _readableState
const streamLike = new EE();
streamLike._readableState = {};
finished(streamLike, common.mustCall());
streamLike.emit('end');
}

{
// Test is writable check through writable
const streamLike = new EE();
streamLike.writable = false;
finished(streamLike, common.mustCall());
streamLike.emit('finish');
}

{
// Test is writable check through writableEnded
const streamLike = new EE();
streamLike.writableEnded = true;
finished(streamLike, common.mustCall());
streamLike.emit('finish');
}

{
// Test is writable check through _writableState
const streamLike = new EE();
streamLike._writableState = {};
finished(streamLike, common.mustCall());
streamLike.emit('finish');
}