@@ -27,8 +27,6 @@ var util = require('util'),
2727 inherits = util . inherits ,
2828 spawn = require ( 'child_process' ) . spawn ;
2929
30- exports . port = 5858 ;
31-
3230exports . start = function ( argv , stdin , stdout ) {
3331 argv || ( argv = process . argv . slice ( 2 ) ) ;
3432
@@ -922,6 +920,8 @@ Interface.prototype.controlEval = function(code, context, filename, callback) {
922920
923921// Used for debugger's remote evaluation (`repl`) commands
924922Interface . prototype . debugEval = function ( code , context , filename , callback ) {
923+ if ( ! this . requireConnection ( ) ) return ;
924+
925925 var self = this ,
926926 client = this . client ;
927927
@@ -1409,14 +1409,43 @@ Interface.prototype.killChild = function() {
14091409// Spawns child process (and restores breakpoints)
14101410Interface . prototype . trySpawn = function ( cb ) {
14111411 var self = this ,
1412- breakpoints = this . breakpoints || [ ] ;
1412+ breakpoints = this . breakpoints || [ ] ,
1413+ port = 5858 ,
1414+ host = 'localhost' ;
14131415
14141416 this . killChild ( ) ;
14151417
1416- this . child = spawn ( process . execPath , this . args ) ;
1418+ // Connecting to remote debugger
1419+ // `node debug localhost:5858`
1420+ if ( this . args . length === 2 ) {
1421+ var match = this . args [ 1 ] . match ( / ^ ( [ ^ : ] + ) : ( \d + ) $ / ) ;
1422+ if ( match ) {
1423+ host = match [ 1 ] ;
1424+ port = parseInt ( match [ 2 ] , 10 ) ;
1425+ this . child = {
1426+ kill : function ( ) {
1427+ // TODO Do we really need to handle it?
1428+ }
1429+ } ;
1430+ }
1431+ } else if ( this . args . length === 3 ) {
1432+ // `node debug -p pid`
1433+ if ( this . args [ 1 ] === '-p' && / ^ \d + $ / . test ( this . args [ 2 ] ) ) {
1434+ this . child = {
1435+ kill : function ( ) {
1436+ // TODO Do we really need to handle it?
1437+ }
1438+ } ;
1439+ process . kill ( parseInt ( this . args [ 2 ] , 10 ) , 'SIGUSR1' ) ;
1440+ }
1441+ }
1442+
1443+ if ( ! this . child ) {
1444+ this . child = spawn ( process . execPath , this . args ) ;
14171445
1418- this . child . stdout . on ( 'data' , this . childPrint . bind ( this ) ) ;
1419- this . child . stderr . on ( 'data' , this . childPrint . bind ( this ) ) ;
1446+ this . child . stdout . on ( 'data' , this . childPrint . bind ( this ) ) ;
1447+ this . child . stderr . on ( 'data' , this . childPrint . bind ( this ) ) ;
1448+ }
14201449
14211450 this . pause ( ) ;
14221451
@@ -1461,16 +1490,16 @@ Interface.prototype.trySpawn = function(cb) {
14611490 client . on ( 'error' , connectError ) ;
14621491 function connectError ( ) {
14631492 // If it's failed to connect 4 times then don't catch the next error
1464- if ( connectionAttempts >= 4 ) {
1493+ if ( connectionAttempts >= 10 ) {
14651494 client . removeListener ( 'error' , connectError ) ;
14661495 }
1467- setTimeout ( attemptConnect , 50 ) ;
1496+ setTimeout ( attemptConnect , 500 ) ;
14681497 }
14691498
14701499 function attemptConnect ( ) {
14711500 ++ connectionAttempts ;
14721501 self . stdout . write ( '.' ) ;
1473- client . connect ( exports . port ) ;
1502+ client . connect ( port , host ) ;
14741503 }
14751504
14761505 setTimeout ( function ( ) {
0 commit comments