@@ -42,18 +42,9 @@ const [ InspectClient, createRepl ] =
4242
4343const debuglog = util . debuglog ( 'inspect' ) ;
4444
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-
5645function portIsFree ( host , port , timeout = 2000 ) {
46+ if ( port === 0 ) return Promise . resolve ( ) ; // Binding to a random port.
47+
5748 const retryDelay = 150 ;
5849 let didTimeOut = false ;
5950
@@ -110,9 +101,11 @@ function runScript(script, scriptArgs, inspectHost, inspectPort, childPrint) {
110101 let output = '' ;
111102 function waitForListenHint ( text ) {
112103 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 ) ;
114107 child . stderr . removeListener ( 'data' , waitForListenHint ) ;
115- resolve ( child ) ;
108+ resolve ( [ child , port , host ] ) ;
116109 }
117110 }
118111
@@ -160,10 +153,11 @@ class NodeInspector {
160153 options . port ,
161154 this . childPrint . bind ( this ) ) ;
162155 } else {
163- this . _runScript = ( ) => Promise . resolve ( null ) ;
156+ this . _runScript =
157+ ( ) => Promise . resolve ( [ null , options . port , options . host ] ) ;
164158 }
165159
166- this . client = new InspectClient ( options . port , options . host ) ;
160+ this . client = new InspectClient ( ) ;
167161
168162 this . domainNames = [ 'Debugger' , 'HeapProfiler' , 'Profiler' , 'Runtime' ] ;
169163 this . domainNames . forEach ( ( domain ) => {
@@ -223,17 +217,16 @@ class NodeInspector {
223217
224218 run ( ) {
225219 this . killChild ( ) ;
226- const { host, port } = this . options ;
227220
228- return this . _runScript ( ) . then ( ( child ) => {
221+ return this . _runScript ( ) . then ( ( [ child , port , host ] ) => {
229222 this . child = child ;
230223
231224 let connectionAttempts = 0 ;
232225 const attemptConnect = ( ) => {
233226 ++ connectionAttempts ;
234227 debuglog ( 'connection attempt #%d' , connectionAttempts ) ;
235228 this . stdout . write ( '.' ) ;
236- return this . client . connect ( )
229+ return this . client . connect ( port , host )
237230 . then ( ( ) => {
238231 debuglog ( 'connection established' ) ;
239232 this . stdout . write ( ' ok' ) ;
@@ -288,7 +281,7 @@ class NodeInspector {
288281
289282function parseArgv ( [ target , ...args ] ) {
290283 let host = '127.0.0.1' ;
291- let port = getDefaultPort ( ) ;
284+ let port = 9229 ;
292285 let isRemote = false ;
293286 let script = target ;
294287 let scriptArgs = args ;
0 commit comments