Skip to content

Commit 513305c

Browse files
ronagdanielleadams
authored andcommitted
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 e292271 commit 513305c

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

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

+34-17
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,10 @@ function eos(stream, options, callback) {
4949

5050
callback = once(callback);
5151

52-
const readable = options.readable ||
53-
(options.readable !== false && isReadableNodeStream(stream));
54-
const writable = options.writable ||
55-
(options.writable !== false && isWritableNodeStream(stream));
52+
const readable = options.readable ?? isReadableNodeStream(stream);
53+
const writable = options.writable ?? isWritableNodeStream(stream);
5654

57-
if (isNodeStream(stream)) {
58-
// Do nothing...
59-
} else {
55+
if (!isNodeStream(stream)) {
6056
// TODO: Webstreams.
6157
// TODO: Throw INVALID_ARG_TYPE.
6258
}
@@ -65,7 +61,9 @@ function eos(stream, options, callback) {
6561
const rState = stream._readableState;
6662

6763
const onlegacyfinish = () => {
68-
if (!stream.writable) onfinish();
64+
if (!stream.writable) {
65+
onfinish();
66+
}
6967
};
7068

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

88-
if (willEmitClose && (!stream.readable || readable)) return;
89-
if (!readable || readableFinished) callback.call(stream);
88+
if (willEmitClose && (!stream.readable || readable)) {
89+
return;
90+
}
91+
92+
if (!readable || readableFinished) {
93+
callback.call(stream);
94+
}
9095
};
9196

9297
let readableFinished = isReadableFinished(stream, false);
@@ -95,10 +100,17 @@ function eos(stream, options, callback) {
95100
// Stream should not be destroyed here. If it is that
96101
// means that user space is doing something differently and
97102
// we cannot trust willEmitClose.
98-
if (stream.destroyed) willEmitClose = false;
103+
if (stream.destroyed) {
104+
willEmitClose = false;
105+
}
99106

100-
if (willEmitClose && (!stream.writable || writable)) return;
101-
if (!writable || writableFinished) callback.call(stream);
107+
if (willEmitClose && (!stream.writable || writable)) {
108+
return;
109+
}
110+
111+
if (!writable || writableFinished) {
112+
callback.call(stream);
113+
}
102114
};
103115

104116
const onerror = (err) => {
@@ -139,8 +151,11 @@ function eos(stream, options, callback) {
139151
if (!willEmitClose) {
140152
stream.on('abort', onclose);
141153
}
142-
if (stream.req) onrequest();
143-
else stream.on('request', onrequest);
154+
if (stream.req) {
155+
onrequest();
156+
} else {
157+
stream.on('request', onrequest);
158+
}
144159
} else if (writable && !wState) { // legacy streams
145160
stream.on('end', onlegacyfinish);
146161
stream.on('close', onlegacyfinish);
@@ -153,7 +168,9 @@ function eos(stream, options, callback) {
153168

154169
stream.on('end', onend);
155170
stream.on('finish', onfinish);
156-
if (options.error !== false) stream.on('error', onerror);
171+
if (options.error !== false) {
172+
stream.on('error', onerror);
173+
}
157174
stream.on('close', onclose);
158175

159176
if (closed) {

0 commit comments

Comments
 (0)