@@ -38,7 +38,7 @@ function onhandshakestart() {
38
38
// callback to destroy the connection right now, it would crash and burn.
39
39
setImmediate ( function ( ) {
40
40
var err = new Error ( 'TLS session renegotiation attack detected.' ) ;
41
- self . _tlsError ( err ) ;
41
+ self . _emitTLSError ( err ) ;
42
42
} ) ;
43
43
}
44
44
}
@@ -233,7 +233,7 @@ function TLSSocket(socket, options) {
233
233
// Proxy for API compatibility
234
234
this . ssl = this . _handle ;
235
235
236
- this . on ( 'error' , this . _tlsError ) ;
236
+ this . on ( 'error' , this . _emitTLSError ) ;
237
237
238
238
this . _init ( socket , wrap ) ;
239
239
@@ -363,16 +363,15 @@ TLSSocket.prototype._init = function(socket, wrap) {
363
363
364
364
// Destroy socket if error happened before handshake's finish
365
365
if ( ! self . _secureEstablished ) {
366
- self . _tlsError ( err ) ;
367
- self . destroy ( ) ;
366
+ self . destroy ( self . _tlsError ( err ) ) ;
368
367
} else if ( options . isServer &&
369
368
rejectUnauthorized &&
370
369
/ p e e r d i d n o t r e t u r n a c e r t i f i c a t e / . test ( err . message ) ) {
371
370
// Ignore server's authorization errors
372
371
self . destroy ( ) ;
373
372
} else {
374
373
// Throw error
375
- self . _tlsError ( err ) ;
374
+ self . _emitTLSError ( err ) ;
376
375
}
377
376
} ;
378
377
@@ -416,7 +415,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
416
415
// Assume `tls.connect()`
417
416
if ( wrap ) {
418
417
wrap . on ( 'error' , function ( err ) {
419
- self . _tlsError ( err ) ;
418
+ self . _emitTLSError ( err ) ;
420
419
} ) ;
421
420
} else {
422
421
assert ( ! socket ) ;
@@ -472,20 +471,27 @@ TLSSocket.prototype.getTLSTicket = function getTLSTicket() {
472
471
} ;
473
472
474
473
TLSSocket . prototype . _handleTimeout = function ( ) {
475
- this . _tlsError ( new Error ( 'TLS handshake timeout' ) ) ;
474
+ this . _emitTLSError ( new Error ( 'TLS handshake timeout' ) ) ;
475
+ } ;
476
+
477
+ TLSSocket . prototype . _emitTLSError = function ( err ) {
478
+ var e = this . _tlsError ( err ) ;
479
+ if ( e )
480
+ this . emit ( 'error' , e ) ;
476
481
} ;
477
482
478
483
TLSSocket . prototype . _tlsError = function ( err ) {
479
484
this . emit ( '_tlsError' , err ) ;
480
485
if ( this . _controlReleased )
481
- this . emit ( 'error' , err ) ;
486
+ return err ;
487
+ return null ;
482
488
} ;
483
489
484
490
TLSSocket . prototype . _releaseControl = function ( ) {
485
491
if ( this . _controlReleased )
486
492
return false ;
487
493
this . _controlReleased = true ;
488
- this . removeListener ( 'error' , this . _tlsError ) ;
494
+ this . removeListener ( 'error' , this . _emitTLSError ) ;
489
495
return true ;
490
496
} ;
491
497
@@ -717,7 +723,11 @@ function Server(/* [options], listener */) {
717
723
} ) ;
718
724
719
725
var errorEmitted = false ;
720
- socket . on ( 'close' , function ( ) {
726
+ socket . on ( 'close' , function ( err ) {
727
+ // Closed because of error - no need to emit it twice
728
+ if ( err )
729
+ return ;
730
+
721
731
// Emit ECONNRESET
722
732
if ( ! socket . _controlReleased && ! errorEmitted ) {
723
733
errorEmitted = true ;
@@ -936,8 +946,7 @@ exports.connect = function(/* [port, host], options, cb */) {
936
946
socket . authorizationError = verifyError . code || verifyError . message ;
937
947
938
948
if ( options . rejectUnauthorized ) {
939
- socket . emit ( 'error' , verifyError ) ;
940
- socket . destroy ( ) ;
949
+ socket . destroy ( verifyError ) ;
941
950
return ;
942
951
} else {
943
952
socket . emit ( 'secureConnect' ) ;
@@ -957,8 +966,7 @@ exports.connect = function(/* [port, host], options, cb */) {
957
966
socket . _hadError = true ;
958
967
var error = new Error ( 'socket hang up' ) ;
959
968
error . code = 'ECONNRESET' ;
960
- socket . destroy ( ) ;
961
- socket . emit ( 'error' , error ) ;
969
+ socket . destroy ( error ) ;
962
970
}
963
971
}
964
972
socket . once ( 'end' , onHangUp ) ;
0 commit comments