@@ -43,7 +43,7 @@ const { TCPConnectWrap } = process.binding('tcp_wrap');
43
43
const { PipeConnectWrap } = process . binding ( 'pipe_wrap' ) ;
44
44
const { ShutdownWrap, WriteWrap } = process . binding ( 'stream_wrap' ) ;
45
45
const { async_id_symbol } = process . binding ( 'async_wrap' ) ;
46
- const { newUid, setDefaultTriggerAsyncId } = require ( 'internal/async_hooks' ) ;
46
+ const { newUid, defaultTriggerAsyncIdScope } = require ( 'internal/async_hooks' ) ;
47
47
const { nextTick } = require ( 'internal/process/next_tick' ) ;
48
48
const errors = require ( 'internal/errors' ) ;
49
49
const dns = require ( 'dns' ) ;
@@ -277,6 +277,14 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
277
277
timers . _unrefActive ( s ) ;
278
278
} ;
279
279
280
+
281
+ function shutdownSocket ( self , callback ) {
282
+ var req = new ShutdownWrap ( ) ;
283
+ req . oncomplete = callback ;
284
+ req . handle = self . _handle ;
285
+ return self . _handle . shutdown ( req ) ;
286
+ }
287
+
280
288
// the user has called .end(), and all the bytes have been
281
289
// sent out to the other side.
282
290
function onSocketFinish ( ) {
@@ -298,14 +306,9 @@ function onSocketFinish() {
298
306
if ( ! this . _handle || ! this . _handle . shutdown )
299
307
return this . destroy ( ) ;
300
308
301
- var req = new ShutdownWrap ( ) ;
302
- req . oncomplete = afterShutdown ;
303
- req . handle = this . _handle ;
304
- // node::ShutdownWrap isn't instantiated and attached to the JS instance of
305
- // ShutdownWrap above until shutdown() is called. So don't set the init
306
- // trigger id until now.
307
- setDefaultTriggerAsyncId ( this [ async_id_symbol ] ) ;
308
- var err = this . _handle . shutdown ( req ) ;
309
+ var err = defaultTriggerAsyncIdScope (
310
+ this [ async_id_symbol ] , [ this , afterShutdown ] , shutdownSocket
311
+ ) ;
309
312
310
313
if ( err )
311
314
return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
@@ -945,23 +948,15 @@ function internalConnect(
945
948
req . localAddress = localAddress ;
946
949
req . localPort = localPort ;
947
950
948
- // node::TCPConnectWrap isn't instantiated and attached to the JS instance
949
- // of TCPConnectWrap above until connect() is called. So don't set the init
950
- // trigger id until now.
951
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
952
951
if ( addressType === 4 )
953
952
err = self . _handle . connect ( req , address , port ) ;
954
953
else
955
954
err = self . _handle . connect6 ( req , address , port ) ;
956
-
957
955
} else {
958
956
const req = new PipeConnectWrap ( ) ;
959
957
req . address = address ;
960
958
req . oncomplete = afterConnect ;
961
- // node::PipeConnectWrap isn't instantiated and attached to the JS instance
962
- // of PipeConnectWrap above until connect() is called. So don't set the
963
- // init trigger id until now.
964
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
959
+
965
960
err = self . _handle . connect ( req , address , afterConnect ) ;
966
961
}
967
962
@@ -1030,7 +1025,9 @@ Socket.prototype.connect = function(...args) {
1030
1025
'string' ,
1031
1026
path ) ;
1032
1027
}
1033
- internalConnect ( this , path ) ;
1028
+ defaultTriggerAsyncIdScope (
1029
+ this [ async_id_symbol ] , [ this , path ] , internalConnect
1030
+ ) ;
1034
1031
} else {
1035
1032
lookupAndConnect ( this , options ) ;
1036
1033
}
@@ -1073,7 +1070,11 @@ function lookupAndConnect(self, options) {
1073
1070
if ( addressType ) {
1074
1071
nextTick ( self [ async_id_symbol ] , function ( ) {
1075
1072
if ( self . connecting )
1076
- internalConnect ( self , host , port , addressType , localAddress , localPort ) ;
1073
+ defaultTriggerAsyncIdScope (
1074
+ self [ async_id_symbol ] ,
1075
+ [ self , host , port , addressType , localAddress , localPort ] ,
1076
+ internalConnect
1077
+ ) ;
1077
1078
} ) ;
1078
1079
return ;
1079
1080
}
@@ -1097,33 +1098,33 @@ function lookupAndConnect(self, options) {
1097
1098
debug ( 'connect: dns options' , dnsopts ) ;
1098
1099
self . _host = host ;
1099
1100
var lookup = options . lookup || dns . lookup ;
1100
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
1101
- lookup ( host , dnsopts , function emitLookup ( err , ip , addressType ) {
1102
- self . emit ( 'lookup' , err , ip , addressType , host ) ;
1101
+ defaultTriggerAsyncIdScope ( self [ async_id_symbol ] , [ ] , function ( ) {
1102
+ lookup ( host , dnsopts , function emitLookup ( err , ip , addressType ) {
1103
+ self . emit ( 'lookup' , err , ip , addressType , host ) ;
1103
1104
1104
- // It's possible we were destroyed while looking this up.
1105
- // XXX it would be great if we could cancel the promise returned by
1106
- // the look up.
1107
- if ( ! self . connecting ) return ;
1105
+ // It's possible we were destroyed while looking this up.
1106
+ // XXX it would be great if we could cancel the promise returned by
1107
+ // the look up.
1108
+ if ( ! self . connecting ) return ;
1108
1109
1109
- if ( err ) {
1110
- // net.createConnection() creates a net.Socket object and
1111
- // immediately calls net.Socket.connect() on it (that's us).
1112
- // There are no event listeners registered yet so defer the
1113
- // error event to the next tick.
1114
- err . host = options . host ;
1115
- err . port = options . port ;
1116
- err . message = err . message + ' ' + options . host + ':' + options . port ;
1117
- process . nextTick ( connectErrorNT , self , err ) ;
1118
- } else {
1119
- self . _unrefTimer ( ) ;
1120
- internalConnect ( self ,
1121
- ip ,
1122
- port ,
1123
- addressType ,
1124
- localAddress ,
1125
- localPort ) ;
1126
- }
1110
+ if ( err ) {
1111
+ // net.createConnection() creates a net.Socket object and
1112
+ // immediately calls net.Socket.connect() on it (that's us).
1113
+ // There are no event listeners registered yet so defer the
1114
+ // error event to the next tick.
1115
+ err . host = options . host ;
1116
+ err . port = options . port ;
1117
+ err . message = err . message + ' ' + options . host + ':' + options . port ;
1118
+ process . nextTick ( connectErrorNT , self , err ) ;
1119
+ } else {
1120
+ self . _unrefTimer ( ) ;
1121
+ defaultTriggerAsyncIdScope (
1122
+ self [ async_id_symbol ] ,
1123
+ [ self , ip , port , addressType , localAddress , localPort ] ,
1124
+ internalConnect
1125
+ ) ;
1126
+ }
1127
+ } ) ;
1127
1128
} ) ;
1128
1129
}
1129
1130
0 commit comments