Skip to content

Commit

Permalink
stream: use more accurate end-of-stream writable and readable detection
Browse files Browse the repository at this point in the history
The value of stream.readable and stream.writable should not
be used to detect whether a stream is Writable or Readable.

Refs: #29395
PR-URL: #29409
Backport-PR-URL: #31345
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
Stewart Addison authored and targos committed Jan 14, 2020
1 parent 4fd4a73 commit 536d088
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
18 changes: 16 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 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
49 changes: 49 additions & 0 deletions test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,52 @@ const { promisify } = require('util');
finished(streamLike, common.mustCall);
streamLike.emit('close');
}


{
// 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');
}

0 comments on commit 536d088

Please sign in to comment.