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: improve comments regarding end() errors. #32839

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 7 additions & 10 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
@@ -598,20 +598,16 @@ Writable.prototype.end = function(chunk, encoding, cb) {
if (typeof cb !== 'function')
cb = nop;

// Ignore unnecessary end() calls.
// TODO(ronag): Compat. Allow end() after destroy().
// This is forgiving in terms of unnecessary calls to end() and can hide
// logic errors. However, usually such errors are harmless and causing a
// hard error can be disproportionately destructive. It is not always
// trivial for the user to determine whether end() needs to be called or not.
if (!state.errored && !state.ending) {
endWritable(this, state, cb);
} else if (state.finished) {
const err = new ERR_STREAM_ALREADY_FINISHED('end');
process.nextTick(cb, err);
// TODO(ronag): Compat. Don't error the stream.
// errorOrDestroy(this, err, true);
process.nextTick(cb, new ERR_STREAM_ALREADY_FINISHED('end'));
} else if (state.destroyed) {
const err = new ERR_STREAM_DESTROYED('end');
process.nextTick(cb, err);
// TODO(ronag): Compat. Don't error the stream.
// errorOrDestroy(this, err, true);
process.nextTick(cb, new ERR_STREAM_DESTROYED('end'));
} else if (cb !== nop) {
onFinished(this, state, cb);
}
@@ -720,6 +716,7 @@ function onCorkedFinish(corkReq, state, err) {
state.corkedRequestsFree.next = corkReq;
}

// TODO(ronag): Avoid using events to implement internal logic.
function onFinished(stream, state, cb) {
function onerror(err) {
stream.removeListener('finish', onfinish);