@@ -588,21 +588,26 @@ Writable.prototype.end = function(chunk, encoding, cb) {
588
588
this . uncork ( ) ;
589
589
}
590
590
591
- if ( typeof cb !== 'function' )
592
- cb = nop ;
593
-
594
591
// This is forgiving in terms of unnecessary calls to end() and can hide
595
592
// logic errors. However, usually such errors are harmless and causing a
596
593
// hard error can be disproportionately destructive. It is not always
597
594
// trivial for the user to determine whether end() needs to be called or not.
595
+ let err ;
598
596
if ( ! state . errored && ! state . ending ) {
599
- endWritable ( this , state , cb ) ;
597
+ state . ending = true ;
598
+ finishMaybe ( this , state , true ) ;
599
+ state . ended = true ;
600
600
} else if ( state . finished ) {
601
- process . nextTick ( cb , new ERR_STREAM_ALREADY_FINISHED ( 'end' ) ) ;
601
+ err = new ERR_STREAM_ALREADY_FINISHED ( 'end' ) ;
602
602
} else if ( state . destroyed ) {
603
- process . nextTick ( cb , new ERR_STREAM_DESTROYED ( 'end' ) ) ;
604
- } else if ( cb !== nop ) {
605
- onFinished ( this , state , cb ) ;
603
+ err = new ERR_STREAM_DESTROYED ( 'end' ) ;
604
+ }
605
+
606
+ if ( typeof cb === 'function' ) {
607
+ if ( err || state . finished )
608
+ process . nextTick ( cb , err ) ;
609
+ else
610
+ onFinished ( this , state , cb ) ;
606
611
}
607
612
608
613
return this ;
@@ -683,18 +688,6 @@ function finish(stream, state) {
683
688
}
684
689
}
685
690
686
- function endWritable ( stream , state , cb ) {
687
- state . ending = true ;
688
- finishMaybe ( stream , state , true ) ;
689
- if ( cb !== nop ) {
690
- if ( state . finished )
691
- process . nextTick ( cb ) ;
692
- else
693
- onFinished ( stream , state , cb ) ;
694
- }
695
- state . ended = true ;
696
- }
697
-
698
691
function onCorkedFinish ( corkReq , state , err ) {
699
692
let entry = corkReq . entry ;
700
693
corkReq . entry = null ;
0 commit comments