Skip to content

Commit

Permalink
stream: eos make const state const
Browse files Browse the repository at this point in the history
writable & readable is based on type and is not actual
state, treat them as such.

Backport-PR-URL: #32178
PR-URL: #32031
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
ronag authored and MylesBorins committed Mar 10, 2020
1 parent 4b04bf8 commit f263659
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions lib/internal/streams/end-of-stream.js
Expand Up @@ -47,9 +47,9 @@ function eos(stream, opts, callback) {

callback = once(callback);

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

const onlegacyfinish = () => {
Expand All @@ -59,35 +59,31 @@ function eos(stream, opts, callback) {
let writableFinished = stream.writableFinished ||
(stream._writableState && stream._writableState.finished);
const onfinish = () => {
writable = false;
writableFinished = true;
if (!readable) callback.call(stream);
if (!readable || readableEnded) callback.call(stream);
};

let readableEnded = stream.readableEnded ||
(stream._readableState && stream._readableState.endEmitted);
const onend = () => {
readable = false;
readableEnded = true;
if (!writable) callback.call(stream);
if (!writable || writableFinished) callback.call(stream);
};

const onerror = (err) => {
callback.call(stream, err);
};

const onclose = () => {
let err;
if (readable && !readableEnded) {
if (!stream._readableState || !stream._readableState.ended)
err = new ERR_STREAM_PREMATURE_CLOSE();
return callback.call(stream, err);
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
}
if (writable && !writableFinished) {
if (!isWritableFinished(stream))
err = new ERR_STREAM_PREMATURE_CLOSE();
return callback.call(stream, err);
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
}
callback.call(stream);
};

const onrequest = () => {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-finished.js
Expand Up @@ -181,7 +181,7 @@ const { promisify } = require('util');
const streamLike = new EE();
streamLike.readableEnded = true;
streamLike.readable = true;
finished(streamLike, common.mustCall);
finished(streamLike, common.mustCall());
streamLike.emit('close');
}

Expand Down

0 comments on commit f263659

Please sign in to comment.