lib/RemoteSlave.js
/**
 * Initializes a new remote slave, by setting up events.
 * @param {RemoteSlave} slave The new RemoteSlave.
 * @param {Array<String>} args The arguments that will be passed to the master proxy server to init the remote process.
 * @param {Object} socket The socket.io connection associated with this slave.
 * @param {Object} options Options that will be passed to the proxy server to init the remote process with.
 * @return {undefined}
 */
Initializes a new remote slave, by setting up events.
Initializes a new remote slave by setting up events .
function initializeSlaveEventsWithSlaveAndPseudoChildProcess(slave, args, socket, options) {
  let handledSpawnError = false;
  const setSlaveExitedToTrue = () => {
    slave[remoteslave].isConnected = false;
    Slave.cleanupSlaveReferences(slave);
  };
  socket.on('reconnect_failed', () => {
    setSlaveExitedToTrue();
    const e = new Error(`Unable to connect to host ${slave.location}`);
    if (!slave[remoteslave].handshake && !handledSpawnError) {
      handledSpawnError = true;
      if (slave.listenerCount('spawn error') === 0 && socket.listeners('reconnect_failed').length === 1) {
        throw e;
      } else {
        slave.emit('spawn error', e);
      }
    } else {
      slave.emit('uncaughtException', e);
    }
    slave.emit('remote connection failed');
  });
  const onSpawnError = m => {
    setSlaveExitedToTrue();
    if (!handledSpawnError) {
      handledSpawnError = true;
      if (slave.listenerCount('spawn error') === 0) {
        throw new Error(m);
      } else {
        slave.emit('spawn error', new Error(m));
      }
    }
  };
  socket.on('spawn failed', onSpawnError);
  socket.on('remote slave exited', () => {
    setSlaveExitedToTrue();
    slave.emit('exited');
  });
  socket.on('remote slave killed', () => {
    setSlaveExitedToTrue();
    slave.emit('killed');
  });
  socket.on('remote slave closed', () => {
    setSlaveExitedToTrue();
    slave.emit('closed');
  });
  socket.on('message', (m) => slave.slaveResponseListener(m));
  socket.on('stdout', m => {
    if (typeof options.forkOptions === 'object' && options.forkOptions.silent) {
      slave.emit('stdout', m);
    } else {
      process.stdout.write(m);
    }
  });
  socket.on('stderr', m => {
    if (typeof options.forkOptions === 'object' && options.forkOptions.silent) {
      slave.emit('stderr', m);
    } else {
      process.stderr.write(m);
    }
  });
  socket.on('remote pid', id => {
    slave.emit('remote handshake', id);
    slave[remoteslave].handshake = true;
    slave[slave[remoteslave].protected].received++;
    slave[remoteslave].remotePID = id;
  });
  socket.on('error', e => {
    if (slave[remoteslave].handshake) { 
      setSlaveExitedToTrue();
      if (slave.listenerCount('uncaughtException') === 0 && socket.listeners('error').length === 1) {
        throw e;
      } else {
        slave.emit('uncaughtException', e);
      }
    } else { 
      onSpawnError(e);
    }
  });
  const remoteOptions = options._.copy();
  remoteOptions.args = args;
  remoteOptions.alias = undefined;
  socket.on('disconnect', setSlaveExitedToTrue);
  socket.emit('message', {
    command: 'init',
    options: remoteOptions,
    count: 1,
    path: remoteOptions.remoteFilepath,
  });
}
function initializeSlaveEventsWithSlaveAndPseudoChildProcess ( slave , args , socket , options ) { let handledSpawnError = false ; const setSlaveExitedToTrue = ( ) => { slave [ remoteslave ] . isConnected = false ; Slave . cleanupSlaveReferences ( slave ) ; } ; socket . on ( 'reconnect_failed' , ( ) => { setSlaveExitedToTrue ( ) ; const e = new Error ( ` ${ slave . location } ` ) ; if ( ! slave [ remoteslave ] . handshake && ! handledSpawnError ) { handledSpawnError = true ; if ( slave . listenerCount ( 'spawn error' ) === 0 && socket . listeners ( 'reconnect_failed' ) . length === 1 ) { throw e ; } else { slave . emit ( 'spawn error' , e ) ; } } else { slave . emit ( 'uncaughtException' , e ) ; } slave . emit ( 'remote connection failed' ) ; } ) ; const onSpawnError = m => { setSlaveExitedToTrue ( ) ; if ( ! handledSpawnError ) { handledSpawnError = true ; if ( slave . listenerCount ( 'spawn error' ) === 0 ) { throw new Error ( m ) ; } else { slave . emit ( 'spawn error' , new Error ( m ) ) ; } } } ; socket . on ( 'spawn failed' , onSpawnError ) ; socket . on ( 'remote slave exited' , ( ) => { setSlaveExitedToTrue ( ) ; slave . emit ( 'exited' ) ; } ) ; socket . on ( 'remote slave killed' , ( ) => { setSlaveExitedToTrue ( ) ; slave . emit ( 'killed' ) ; } ) ; socket . on ( 'remote slave closed' , ( ) => { setSlaveExitedToTrue ( ) ; slave . emit ( 'closed' ) ; } ) ; socket . on ( 'message' , ( m ) => slave . slaveResponseListener ( m ) ) ; socket . on ( 'stdout' , m => { if ( typeof options . forkOptions === 'object' && options . forkOptions . silent ) { slave . emit ( 'stdout' , m ) ; } else { process . stdout . write ( m ) ; } } ) ; socket . on ( 'stderr' , m => { if ( typeof options . forkOptions === 'object' && options . forkOptions . silent ) { slave . emit ( 'stderr' , m ) ; } else { process . stderr . write ( m ) ; } } ) ; socket . on ( 'remote pid' , id => { slave . emit ( 'remote handshake' , id ) ; slave [ remoteslave ] . handshake = true ; slave [ slave [ remoteslave ] . protected ] . received ++ ; slave [ remoteslave ] . remotePID = id ; } ) ; socket . on ( 'error' , e => { if ( slave [ remoteslave ] . handshake ) { setSlaveExitedToTrue ( ) ; if ( slave . listenerCount ( 'uncaughtException' ) === 0 && socket . listeners ( 'error' ) . length === 1 ) { throw e ; } else { slave . emit ( 'uncaughtException' , e ) ; } } else { onSpawnError ( e ) ; } } ) ; const remoteOptions = options . _ . copy ( ) ; remoteOptions . args = args ; remoteOptions . alias = undefined ; socket . on ( 'disconnect' , setSlaveExitedToTrue ) ; socket . emit ( 'message' , { command : 'init' , options : remoteOptions , count : 1 , path : remoteOptions . remoteFilepath , } ) ; }
Initializes a new remote slave, by setting up events.
Initializes a new remote slave by setting up events .
/**
     * Emitted when the slave fails to establish a socket connection with the host
     * @event remote connection failed
     */ // Should the slave spawn fail, cleanup references and call the onSpawnError handler. // If the remote closes or is killed, ensure we do the same local. // Handle responses from the remote slave... // Handle stdout passed from the slave to the master proxy server to here... // Handle stderr passed from the slave to the master proxy server to here... // Set the slave's PID /**
     * Emitted when the slave has been spawned by the master proxy server and the process id is now available.
     * This establishes a "handshake" between the server and this process, that indicates the slave is ready for work.
     * @event remote handshake
     * @argument {Number} id The slave's process id
     */ // Already spawned handle as error... // Throw an error if and only if a user has *not* subscribed to the "uncaughtException" event // and the user doesn't already have a socket.on('error') lietener installed. // Not yet spawned, handle as spawn error... // Set connected to false when the child closes, exits, or is disconnected. // Don't want/need aliases on remote master, could cause conflict... // Send the message to the socket to initialize the slave.