@@ -84,6 +84,10 @@ function closeNT(callback, err) {
84
84
callback ( err ) ;
85
85
}
86
86
87
+ function detachAfterFinish ( ) {
88
+ this [ bunSocketInternal ] = null ;
89
+ }
90
+
87
91
var SocketClass ;
88
92
const Socket = ( function ( InternalSocket ) {
89
93
SocketClass = InternalSocket ;
@@ -164,7 +168,7 @@ const Socket = (function (InternalSocket) {
164
168
self . _secureEstablished = ! ! success ;
165
169
166
170
self . emit ( "secure" , self ) ;
167
-
171
+ self . alpnProtocol = socket . alpnProtocol ;
168
172
const { checkServerIdentity } = self [ bunTLSConnectOptions ] ;
169
173
if ( ! verifyError && typeof checkServerIdentity === "function" && self . servername ) {
170
174
const cert = self . getPeerCertificate ( true ) ;
@@ -291,10 +295,7 @@ const Socket = (function (InternalSocket) {
291
295
292
296
if ( typeof connectionListener == "function" ) {
293
297
this . pauseOnConnect = pauseOnConnect ;
294
- if ( isTLS ) {
295
- // add secureConnection event handler
296
- self . once ( "secureConnection" , ( ) => connectionListener . $call ( self , _socket ) ) ;
297
- } else {
298
+ if ( ! isTLS ) {
298
299
connectionListener . $call ( self , _socket ) ;
299
300
}
300
301
}
@@ -312,6 +313,7 @@ const Socket = (function (InternalSocket) {
312
313
self . _secureEstablished = ! ! success ;
313
314
self . servername = socket . getServername ( ) ;
314
315
const server = self . server ;
316
+ self . alpnProtocol = socket . alpnProtocol ;
315
317
if ( self . _requestCert || self . _rejectUnauthorized ) {
316
318
if ( verifyError ) {
317
319
self . authorized = false ;
@@ -329,7 +331,11 @@ const Socket = (function (InternalSocket) {
329
331
} else {
330
332
self . authorized = true ;
331
333
}
332
- self . server . emit ( "secureConnection" , self ) ;
334
+ const connectionListener = server [ bunSocketServerOptions ] ?. connectionListener ;
335
+ if ( typeof connectionListener == "function" ) {
336
+ connectionListener . $call ( server , self ) ;
337
+ }
338
+ server . emit ( "secureConnection" , self ) ;
333
339
// after secureConnection event we emmit secure and secureConnect
334
340
self . emit ( "secure" , self ) ;
335
341
self . emit ( "secureConnect" , verifyError ) ;
@@ -685,14 +691,23 @@ const Socket = (function (InternalSocket) {
685
691
}
686
692
687
693
_destroy ( err , callback ) {
688
- const socket = this [ bunSocketInternal ] ;
689
- if ( socket ) {
694
+ const { ending } = this . _writableState ;
695
+ // lets make sure that the writable side is closed
696
+ if ( ! ending ) {
697
+ // at this state destroyed will be true but we need to close the writable side
698
+ this . _writableState . destroyed = false ;
699
+ this . end ( ) ;
700
+ // we now restore the destroyed flag
701
+ this . _writableState . destroyed = true ;
702
+ }
703
+
704
+ if ( this . writableFinished ) {
705
+ // closed we can detach the socket
690
706
this [ bunSocketInternal ] = null ;
691
- // we still have a socket, call end before destroy
692
- process . nextTick ( endNT , socket , callback , err ) ;
693
- return ;
707
+ } else {
708
+ // lets wait for the finish event before detaching the socket
709
+ this . once ( "finish" , detachAfterFinish ) ;
694
710
}
695
- // no socket, just destroy
696
711
process . nextTick ( closeNT , callback , err ) ;
697
712
}
698
713
@@ -706,7 +721,6 @@ const Socket = (function (InternalSocket) {
706
721
this . #final_callback = callback ;
707
722
} else {
708
723
// emit FIN not allowing half open
709
- this [ bunSocketInternal ] = null ;
710
724
process . nextTick ( endNT , socket , callback ) ;
711
725
}
712
726
}
0 commit comments