Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: cleanup eos #40998

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ function eos(stream, options, callback) {

callback = once(callback);

const readable = options.readable ||
(options.readable !== false && isReadableNodeStream(stream));
const writable = options.writable ||
(options.writable !== false && isWritableNodeStream(stream));
const readable = options.readable ?? isReadableNodeStream(stream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behavior is different for some falsy values.

const writable = options.writable ?? isWritableNodeStream(stream);

if (isNodeStream(stream)) {
// Do nothing...
} else {
if (!isNodeStream(stream)) {
// TODO: Webstreams.
// TODO: Throw INVALID_ARG_TYPE.
}
Expand All @@ -67,7 +63,9 @@ function eos(stream, options, callback) {
const rState = stream._readableState;

const onlegacyfinish = () => {
if (!stream.writable) onfinish();
if (!stream.writable) {
onfinish();
}
};

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

if (willEmitClose && (!stream.readable || readable)) return;
if (!readable || readableFinished) callback.call(stream);
if (willEmitClose && (!stream.readable || readable)) {
return;
}

if (!readable || readableFinished) {
callback.call(stream);
}
};

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

if (willEmitClose && (!stream.writable || writable)) return;
if (!writable || writableFinished) callback.call(stream);
if (willEmitClose && (!stream.writable || writable)) {
return;
}

if (!writable || writableFinished) {
callback.call(stream);
}
};

const onerror = (err) => {
Expand Down Expand Up @@ -141,8 +153,11 @@ function eos(stream, options, callback) {
if (!willEmitClose) {
stream.on('abort', onclose);
}
if (stream.req) onrequest();
else stream.on('request', onrequest);
if (stream.req) {
onrequest();
} else {
stream.on('request', onrequest);
}
} else if (writable && !wState) { // legacy streams
stream.on('end', onlegacyfinish);
stream.on('close', onlegacyfinish);
Expand All @@ -155,7 +170,9 @@ function eos(stream, options, callback) {

stream.on('end', onend);
stream.on('finish', onfinish);
if (options.error !== false) stream.on('error', onerror);
if (options.error !== false) {
stream.on('error', onerror);
}
stream.on('close', onclose);

if (closed) {
Expand Down