Skip to content

Commit 40a52bd

Browse files
authored
stream: cleanup eos
PR-URL: #40998 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d81f328 commit 40a52bd

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

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

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,10 @@ function eos(stream, options, callback) {
5151

5252
callback = once(callback);
5353

54-
const readable = options.readable ||
55-
(options.readable !== false && isReadableNodeStream(stream));
56-
const writable = options.writable ||
57-
(options.writable !== false && isWritableNodeStream(stream));
54+
const readable = options.readable ?? isReadableNodeStream(stream);
55+
const writable = options.writable ?? isWritableNodeStream(stream);
5856

59-
if (isNodeStream(stream)) {
60-
// Do nothing...
61-
} else {
57+
if (!isNodeStream(stream)) {
6258
// TODO: Webstreams.
6359
// TODO: Throw INVALID_ARG_TYPE.
6460
}
@@ -67,7 +63,9 @@ function eos(stream, options, callback) {
6763
const rState = stream._readableState;
6864

6965
const onlegacyfinish = () => {
70-
if (!stream.writable) onfinish();
66+
if (!stream.writable) {
67+
onfinish();
68+
}
7169
};
7270

7371
// TODO (ronag): Improve soft detection to include core modules and
@@ -85,10 +83,17 @@ function eos(stream, options, callback) {
8583
// Stream should not be destroyed here. If it is that
8684
// means that user space is doing something differently and
8785
// we cannot trust willEmitClose.
88-
if (stream.destroyed) willEmitClose = false;
86+
if (stream.destroyed) {
87+
willEmitClose = false;
88+
}
8989

90-
if (willEmitClose && (!stream.readable || readable)) return;
91-
if (!readable || readableFinished) callback.call(stream);
90+
if (willEmitClose && (!stream.readable || readable)) {
91+
return;
92+
}
93+
94+
if (!readable || readableFinished) {
95+
callback.call(stream);
96+
}
9297
};
9398

9499
let readableFinished = isReadableFinished(stream, false);
@@ -97,10 +102,17 @@ function eos(stream, options, callback) {
97102
// Stream should not be destroyed here. If it is that
98103
// means that user space is doing something differently and
99104
// we cannot trust willEmitClose.
100-
if (stream.destroyed) willEmitClose = false;
105+
if (stream.destroyed) {
106+
willEmitClose = false;
107+
}
101108

102-
if (willEmitClose && (!stream.writable || writable)) return;
103-
if (!writable || writableFinished) callback.call(stream);
109+
if (willEmitClose && (!stream.writable || writable)) {
110+
return;
111+
}
112+
113+
if (!writable || writableFinished) {
114+
callback.call(stream);
115+
}
104116
};
105117

106118
const onerror = (err) => {
@@ -141,8 +153,11 @@ function eos(stream, options, callback) {
141153
if (!willEmitClose) {
142154
stream.on('abort', onclose);
143155
}
144-
if (stream.req) onrequest();
145-
else stream.on('request', onrequest);
156+
if (stream.req) {
157+
onrequest();
158+
} else {
159+
stream.on('request', onrequest);
160+
}
146161
} else if (writable && !wState) { // legacy streams
147162
stream.on('end', onlegacyfinish);
148163
stream.on('close', onlegacyfinish);
@@ -155,7 +170,9 @@ function eos(stream, options, callback) {
155170

156171
stream.on('end', onend);
157172
stream.on('finish', onfinish);
158-
if (options.error !== false) stream.on('error', onerror);
173+
if (options.error !== false) {
174+
stream.on('error', onerror);
175+
}
159176
stream.on('close', onclose);
160177

161178
if (closed) {

0 commit comments

Comments
 (0)