@@ -158,17 +158,17 @@ Monitor.prototype.start = function (restart) {
158
158
self . emit ( restart ? 'restart' : 'start' , self , self . data ) ;
159
159
} ) ;
160
160
161
- function reemit ( ) {
162
- self . emit . apply ( self , arguments ) ;
161
+ function onMessage ( msg ) {
162
+ self . emit ( 'message' , msg ) ;
163
163
}
164
164
165
165
// Re-emit messages from the child process
166
- this . child . on ( 'message' , reemit ) ;
166
+ this . child . on ( 'message' , onMessage ) ;
167
167
168
168
child . on ( 'exit' , function ( code ) {
169
169
var spinning = Date . now ( ) - self . ctime < self . minUptime ;
170
170
self . warn ( 'Forever detected script exited with code: ' + code ) ;
171
- child . removeListener ( 'message' , reemit ) ;
171
+ child . removeListener ( 'message' , onMessage ) ;
172
172
173
173
function letChildDie ( ) {
174
174
self . running = false ;
@@ -210,9 +210,6 @@ Monitor.prototype.start = function (restart) {
210
210
// trying to execute a script with an env: e.g. node myfile.js
211
211
//
212
212
Monitor . prototype . trySpawn = function ( ) {
213
- var execPath = process . execPath ,
214
- forked ;
215
-
216
213
if ( this . command === 'node' || ( this . checkFile && ! this . childExists ) ) {
217
214
try {
218
215
var stats = fs . statSync ( this . args [ 0 ] ) ;
@@ -226,14 +223,9 @@ Monitor.prototype.trySpawn = function () {
226
223
this . spawnWith . cwd = this . cwd || this . spawnWith . cwd ;
227
224
this . spawnWith . env = this . _getEnv ( ) ;
228
225
229
- if ( this . command === 'node' || this . fork ) {
230
- process . execPath = this . command ;
231
- forked = fork ( this . options [ 0 ] , this . options . slice ( 1 ) , this . spawnWith ) ;
232
- process . execPath = execPath ;
233
- return forked ;
234
- }
235
-
236
- return spawn ( this . command , this . options , this . spawnWith ) ;
226
+ return this . fork
227
+ ? this . _forkSpawn ( )
228
+ : spawn ( this . command , this . options , this . spawnWith ) ;
237
229
} ;
238
230
239
231
//
@@ -402,3 +394,47 @@ Monitor.prototype._getEnv = function () {
402
394
403
395
return merged ;
404
396
} ;
397
+
398
+ //
399
+ // ### @private function _forkSpawn()
400
+ //
401
+ // This is an unfortunate hack which works around two inconsistencies
402
+ // in the `child_process` APIs in node.js core as of `v0.6.6`. Pull-requests
403
+ // are open to resolve both issues.
404
+ //
405
+ // https://github.com/joyent/node/pull/2455
406
+ // https://github.com/joyent/node/pull/2454
407
+ //
408
+ // Until then, setup `options.customFds` to always return null.
409
+ //
410
+ //
411
+ Monitor . prototype . _forkSpawn = function ( ) {
412
+ var execPath = process . execPath ,
413
+ hackSpawn = { } ,
414
+ self = this ,
415
+ forked ;
416
+
417
+ if ( this . spawnWith && typeof this . spawnWith === 'object' ) {
418
+ Object . keys ( this . spawnWith ) . forEach ( function ( key ) {
419
+ hackSpawn [ key ] = self . spawnWith [ key ] ;
420
+ } ) ;
421
+ }
422
+
423
+ delete hackSpawn . customFds ;
424
+ Object . defineProperty ( hackSpawn , 'customFds' , {
425
+ get : function ( ) {
426
+ return null ;
427
+ } ,
428
+ set : function ( ) {
429
+ //
430
+ // Do nothing, ignore the attempt to overwrite here:
431
+ // https://github.com/joyent/node/blob/master/lib/child_process.js#L172
432
+ //
433
+ }
434
+ } ) ;
435
+
436
+ process . execPath = this . command ;
437
+ forked = fork ( this . options [ 0 ] , this . options . slice ( 1 ) , hackSpawn ) ;
438
+ process . execPath = execPath ;
439
+ return forked ;
440
+ } ;
0 commit comments