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