@@ -42,18 +42,9 @@ const [ InspectClient, createRepl ] =
42
42
43
43
const debuglog = util . debuglog ( 'inspect' ) ;
44
44
45
- const DEBUG_PORT_PATTERN = / ^ - - (?: d e b u g | i n s p e c t ) (?: - p o r t | - b r k ) ? = ( \d { 1 , 5 } ) $ / ;
46
- function getDefaultPort ( ) {
47
- for ( const arg of process . execArgv ) {
48
- const match = arg . match ( DEBUG_PORT_PATTERN ) ;
49
- if ( match ) {
50
- return + match [ 1 ] ;
51
- }
52
- }
53
- return 9229 ;
54
- }
55
-
56
45
function portIsFree ( host , port , timeout = 2000 ) {
46
+ if ( port === 0 ) return Promise . resolve ( ) ; // Binding to a random port.
47
+
57
48
const retryDelay = 150 ;
58
49
let didTimeOut = false ;
59
50
@@ -110,9 +101,11 @@ function runScript(script, scriptArgs, inspectHost, inspectPort, childPrint) {
110
101
let output = '' ;
111
102
function waitForListenHint ( text ) {
112
103
output += text ;
113
- if ( / D e b u g g e r l i s t e n i n g o n / . test ( output ) ) {
104
+ if ( / D e b u g g e r l i s t e n i n g o n w s : \/ \/ \[ ? ( .+ ?) \] ? : ( \d + ) \/ / . test ( output ) ) {
105
+ const host = RegExp . $1 ;
106
+ const port = Number . parseInt ( RegExp . $2 ) ;
114
107
child . stderr . removeListener ( 'data' , waitForListenHint ) ;
115
- resolve ( child ) ;
108
+ resolve ( [ child , port , host ] ) ;
116
109
}
117
110
}
118
111
@@ -160,10 +153,11 @@ class NodeInspector {
160
153
options . port ,
161
154
this . childPrint . bind ( this ) ) ;
162
155
} else {
163
- this . _runScript = ( ) => Promise . resolve ( null ) ;
156
+ this . _runScript =
157
+ ( ) => Promise . resolve ( [ null , options . port , options . host ] ) ;
164
158
}
165
159
166
- this . client = new InspectClient ( options . port , options . host ) ;
160
+ this . client = new InspectClient ( ) ;
167
161
168
162
this . domainNames = [ 'Debugger' , 'HeapProfiler' , 'Profiler' , 'Runtime' ] ;
169
163
this . domainNames . forEach ( ( domain ) => {
@@ -223,17 +217,16 @@ class NodeInspector {
223
217
224
218
run ( ) {
225
219
this . killChild ( ) ;
226
- const { host, port } = this . options ;
227
220
228
- return this . _runScript ( ) . then ( ( child ) => {
221
+ return this . _runScript ( ) . then ( ( [ child , port , host ] ) => {
229
222
this . child = child ;
230
223
231
224
let connectionAttempts = 0 ;
232
225
const attemptConnect = ( ) => {
233
226
++ connectionAttempts ;
234
227
debuglog ( 'connection attempt #%d' , connectionAttempts ) ;
235
228
this . stdout . write ( '.' ) ;
236
- return this . client . connect ( )
229
+ return this . client . connect ( port , host )
237
230
. then ( ( ) => {
238
231
debuglog ( 'connection established' ) ;
239
232
this . stdout . write ( ' ok' ) ;
@@ -288,7 +281,7 @@ class NodeInspector {
288
281
289
282
function parseArgv ( [ target , ...args ] ) {
290
283
let host = '127.0.0.1' ;
291
- let port = getDefaultPort ( ) ;
284
+ let port = 9229 ;
292
285
let isRemote = false ;
293
286
let script = target ;
294
287
let scriptArgs = args ;
0 commit comments