@@ -42,7 +42,7 @@ 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 = ( \d + ) $ / ;
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
46
function getDefaultPort ( ) {
47
47
for ( const arg of process . execArgv ) {
48
48
const match = arg . match ( DEBUG_PORT_PATTERN ) ;
@@ -53,54 +53,6 @@ function getDefaultPort() {
53
53
return 9229 ;
54
54
}
55
55
56
- function runScript ( script , scriptArgs , inspectPort , childPrint ) {
57
- return new Promise ( ( resolve ) => {
58
- const needDebugBrk = process . version . match ( / ^ v ( 6 | 7 ) \. / ) ;
59
- const args = ( needDebugBrk ?
60
- [ '--inspect' , `--debug-brk=${ inspectPort } ` ] :
61
- [ `--inspect-brk=${ inspectPort } ` ] )
62
- . concat ( [ script ] , scriptArgs ) ;
63
- const child = spawn ( process . execPath , args ) ;
64
- child . stdout . setEncoding ( 'utf8' ) ;
65
- child . stderr . setEncoding ( 'utf8' ) ;
66
- child . stdout . on ( 'data' , childPrint ) ;
67
- child . stderr . on ( 'data' , childPrint ) ;
68
-
69
- let output = '' ;
70
- function waitForListenHint ( text ) {
71
- output += text ;
72
- if ( / c h r o m e - d e v t o o l s : \/ \/ / . test ( output ) ) {
73
- child . stderr . removeListener ( 'data' , waitForListenHint ) ;
74
- resolve ( child ) ;
75
- }
76
- }
77
-
78
- child . stderr . on ( 'data' , waitForListenHint ) ;
79
- } ) ;
80
- }
81
-
82
- function createAgentProxy ( domain , client ) {
83
- const agent = new EventEmitter ( ) ;
84
- agent . then = ( ...args ) => {
85
- // TODO: potentially fetch the protocol and pretty-print it here.
86
- const descriptor = {
87
- [ util . inspect . custom ] ( depth , { stylize } ) {
88
- return stylize ( `[Agent ${ domain } ]` , 'special' ) ;
89
- } ,
90
- } ;
91
- return Promise . resolve ( descriptor ) . then ( ...args ) ;
92
- } ;
93
-
94
- return new Proxy ( agent , {
95
- get ( target , name ) {
96
- if ( name in target ) return target [ name ] ;
97
- return function callVirtualMethod ( params ) {
98
- return client . callMethod ( `${ domain } .${ name } ` , params ) ;
99
- } ;
100
- } ,
101
- } ) ;
102
- }
103
-
104
56
function portIsFree ( host , port , timeout = 2000 ) {
105
57
const retryDelay = 150 ;
106
58
let didTimeOut = false ;
@@ -140,6 +92,57 @@ function portIsFree(host, port, timeout = 2000) {
140
92
} ) ;
141
93
}
142
94
95
+ function runScript ( script , scriptArgs , inspectHost , inspectPort , childPrint ) {
96
+ return portIsFree ( inspectHost , inspectPort )
97
+ . then ( ( ) => {
98
+ return new Promise ( ( resolve ) => {
99
+ const needDebugBrk = process . version . match ( / ^ v ( 6 | 7 ) \. / ) ;
100
+ const args = ( needDebugBrk ?
101
+ [ '--inspect' , `--debug-brk=${ inspectPort } ` ] :
102
+ [ `--inspect-brk=${ inspectPort } ` ] )
103
+ . concat ( [ script ] , scriptArgs ) ;
104
+ const child = spawn ( process . execPath , args ) ;
105
+ child . stdout . setEncoding ( 'utf8' ) ;
106
+ child . stderr . setEncoding ( 'utf8' ) ;
107
+ child . stdout . on ( 'data' , childPrint ) ;
108
+ child . stderr . on ( 'data' , childPrint ) ;
109
+
110
+ let output = '' ;
111
+ function waitForListenHint ( text ) {
112
+ 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 ) ) {
114
+ child . stderr . removeListener ( 'data' , waitForListenHint ) ;
115
+ resolve ( child ) ;
116
+ }
117
+ }
118
+
119
+ child . stderr . on ( 'data' , waitForListenHint ) ;
120
+ } ) ;
121
+ } ) ;
122
+ }
123
+
124
+ function createAgentProxy ( domain , client ) {
125
+ const agent = new EventEmitter ( ) ;
126
+ agent . then = ( ...args ) => {
127
+ // TODO: potentially fetch the protocol and pretty-print it here.
128
+ const descriptor = {
129
+ [ util . inspect . custom ] ( depth , { stylize } ) {
130
+ return stylize ( `[Agent ${ domain } ]` , 'special' ) ;
131
+ } ,
132
+ } ;
133
+ return Promise . resolve ( descriptor ) . then ( ...args ) ;
134
+ } ;
135
+
136
+ return new Proxy ( agent , {
137
+ get ( target , name ) {
138
+ if ( name in target ) return target [ name ] ;
139
+ return function callVirtualMethod ( params ) {
140
+ return client . callMethod ( `${ domain } .${ name } ` , params ) ;
141
+ } ;
142
+ } ,
143
+ } ) ;
144
+ }
145
+
143
146
class NodeInspector {
144
147
constructor ( options , stdin , stdout ) {
145
148
this . options = options ;
@@ -153,6 +156,7 @@ class NodeInspector {
153
156
this . _runScript = runScript . bind ( null ,
154
157
options . script ,
155
158
options . scriptArgs ,
159
+ options . host ,
156
160
options . port ,
157
161
this . childPrint . bind ( this ) ) ;
158
162
} else {
@@ -221,12 +225,7 @@ class NodeInspector {
221
225
this . killChild ( ) ;
222
226
const { host, port } = this . options ;
223
227
224
- const runOncePortIsFree = ( ) => {
225
- return portIsFree ( host , port )
226
- . then ( ( ) => this . _runScript ( ) ) ;
227
- } ;
228
-
229
- return runOncePortIsFree ( ) . then ( ( child ) => {
228
+ return this . _runScript ( ) . then ( ( child ) => {
230
229
this . child = child ;
231
230
232
231
let connectionAttempts = 0 ;
@@ -296,6 +295,7 @@ function parseArgv([target, ...args]) {
296
295
297
296
const hostMatch = target . match ( / ^ ( [ ^ : ] + ) : ( \d + ) $ / ) ;
298
297
const portMatch = target . match ( / ^ - - p o r t = ( \d + ) $ / ) ;
298
+
299
299
if ( hostMatch ) {
300
300
// Connecting to remote debugger
301
301
// `node-inspect localhost:9229`
@@ -304,16 +304,31 @@ function parseArgv([target, ...args]) {
304
304
isRemote = true ;
305
305
script = null ;
306
306
} else if ( portMatch ) {
307
- // Start debugger on custom port
308
- // `node debug --port=8058 app .js`
307
+ // start debugee on custom port
308
+ // `node inspect --port=9230 script .js`
309
309
port = parseInt ( portMatch [ 1 ] , 10 ) ;
310
310
script = args [ 0 ] ;
311
311
scriptArgs = args . slice ( 1 ) ;
312
+ } else if ( args . length === 1 && / ^ \d + $ / . test ( args [ 0 ] ) && target === '-p' ) {
313
+ // Start debugger against a given pid
314
+ const pid = parseInt ( args [ 0 ] , 10 ) ;
315
+ try {
316
+ process . _debugProcess ( pid ) ;
317
+ } catch ( e ) {
318
+ if ( e . code === 'ESRCH' ) {
319
+ /* eslint-disable no-console */
320
+ console . error ( `Target process: ${ pid } doesn't exist.` ) ;
321
+ /* eslint-enable no-console */
322
+ process . exit ( 1 ) ;
323
+ }
324
+ throw e ;
325
+ }
326
+ script = null ;
327
+ isRemote = true ;
312
328
}
313
329
314
330
return {
315
- host, port,
316
- isRemote, script, scriptArgs,
331
+ host, port, isRemote, script, scriptArgs,
317
332
} ;
318
333
}
319
334
@@ -328,6 +343,7 @@ function startInspect(argv = process.argv.slice(2),
328
343
329
344
console . error ( `Usage: ${ invokedAs } script.js` ) ;
330
345
console . error ( ` ${ invokedAs } <host>:<port>` ) ;
346
+ console . error ( ` ${ invokedAs } -p <pid>` ) ;
331
347
process . exit ( 1 ) ;
332
348
}
333
349
0 commit comments