diff options
author | Robert Nagy <ronagy@icloud.com> | 2020-04-16 17:25:22 +0200 |
---|---|---|
committer | Robert Nagy <ronagy@icloud.com> | 2020-04-18 21:07:26 +0200 |
commit | 73f3072ffb1256f013ffd75f479060029675b7a3 (patch) | |
tree | 0323b873a8c172b86d31224fc9e654a0baf9820e | |
parent | f67601cd772b9dbcc94cbf9939b0e229b815e939 (diff) | |
download | node-new-73f3072ffb1256f013ffd75f479060029675b7a3.tar.gz |
stream: simplify Writable.end()
Simplifies Writable.end() by inlining and
de-duplicating code.
PR-URL: https://github.com/nodejs/node/pull/32882
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
-rw-r--r-- | lib/_stream_writable.js | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 25235166d9..2b36a765e5 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -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; @@ -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; |