Skip to content

Commit 8429295

Browse files
committed
stream: eos make const state const
writable & readable is based on type and is not actual state, treat them as such. PR-URL: #32031 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 43b7142 commit 8429295

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

lib/internal/streams/end-of-stream.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ function eos(stream, opts, callback) {
5656

5757
callback = once(callback);
5858

59-
let readable = opts.readable ||
59+
const readable = opts.readable ||
6060
(opts.readable !== false && isReadable(stream));
61-
let writable = opts.writable ||
61+
const writable = opts.writable ||
6262
(opts.writable !== false && isWritable(stream));
6363

6464
const wState = stream._writableState;
@@ -71,35 +71,31 @@ function eos(stream, opts, callback) {
7171
let writableFinished = stream.writableFinished ||
7272
(wState && wState.finished);
7373
const onfinish = () => {
74-
writable = false;
7574
writableFinished = true;
76-
if (!readable) callback.call(stream);
75+
if (!readable || readableEnded) callback.call(stream);
7776
};
7877

7978
let readableEnded = stream.readableEnded ||
8079
(rState && rState.endEmitted);
8180
const onend = () => {
82-
readable = false;
8381
readableEnded = true;
84-
if (!writable) callback.call(stream);
82+
if (!writable || writableFinished) callback.call(stream);
8583
};
8684

8785
const onerror = (err) => {
8886
callback.call(stream, err);
8987
};
9088

9189
const onclose = () => {
92-
let err;
9390
if (readable && !readableEnded) {
9491
if (!isReadableEnded(stream))
95-
err = new ERR_STREAM_PREMATURE_CLOSE();
96-
return callback.call(stream, err);
92+
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
9793
}
9894
if (writable && !writableFinished) {
9995
if (!isWritableFinished(stream))
100-
err = new ERR_STREAM_PREMATURE_CLOSE();
101-
return callback.call(stream, err);
96+
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
10297
}
98+
callback.call(stream);
10399
};
104100

105101
const onrequest = () => {

test/parallel/test-stream-finished.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ const { promisify } = require('util');
181181
const streamLike = new EE();
182182
streamLike.readableEnded = true;
183183
streamLike.readable = true;
184-
finished(streamLike, common.mustCall);
184+
finished(streamLike, common.mustCall());
185185
streamLike.emit('close');
186186
}
187187

0 commit comments

Comments
 (0)