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: simplify Writable.end() #32882

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
33 changes: 13 additions & 20 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,21 +595,26 @@ Writable.prototype.end = function(chunk, encoding, cb) {
this.uncork();
}

if (typeof cb !== 'function')
cb = nop;

// 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.
let err;
if (!state.errored && !state.ending) {
endWritable(this, state, cb);
state.ending = true;
finishMaybe(this, state, true);
state.ended = true;
} else if (state.finished) {
process.nextTick(cb, new ERR_STREAM_ALREADY_FINISHED('end'));
err = new ERR_STREAM_ALREADY_FINISHED('end');
} else if (state.destroyed) {
process.nextTick(cb, new ERR_STREAM_DESTROYED('end'));
} else if (cb !== nop) {
onFinished(this, state, cb);
err = new ERR_STREAM_DESTROYED('end');
}

if (typeof cb === 'function') {
if (err || state.finished)
process.nextTick(cb, err);
else
onFinished(this, state, cb);
}

return this;
Expand Down Expand Up @@ -690,18 +695,6 @@ function finish(stream, state) {
}
}

function endWritable(stream, state, cb) {
state.ending = true;
finishMaybe(stream, state, true);
if (cb !== nop) {
if (state.finished)
process.nextTick(cb);
else
onFinished(stream, state, cb);
}
state.ended = true;
}

function onCorkedFinish(corkReq, state, err) {
let entry = corkReq.entry;
corkReq.entry = null;
Expand Down