lib/wait-port.js
function tryConnect(options, timeout) {
  return new Promise((resolve, reject) => {
    try {
      const socket = createConnectionWithTimeout(options, timeout, (err) => {
        if (err) {
          if (err.code === 'ECONNREFUSED') {
            //  We successfully *tried* to connect, so resolve with false so
            //  that we try again.
            debug('Socket not open: ECONNREFUSED');
            socket.destroy();
            return resolve(false);
          } else if (err.code === 'ECONNTIMEOUT') {
            //  We've successfully *tried* to connect, but we're timing out
            //  establishing the connection. This is not ideal (either
            //  the port is open or it ain't).
            debug('Socket not open: ECONNTIMEOUT');
            socket.destroy();
            return resolve(false);
          } else if (err.code === 'ECONNRESET') {
            //  This can happen if the target server kills its connection before
            //  we can read from it, we can normally just try again.
            debug('Socket not open: ECONNRESET');
            socket.destroy();
            return resolve(false);
          }

          //  Trying to open the socket has resulted in an error we don't
          //  understand. Better give up.
          debug(`Unexpected error trying to open socket: ${err}`);
          socket.destroy();
          return reject(err);
        }
      
        //  Boom, we connected!
        debug('Socket connected!');

        //  If we are not dealing with http, we're done.
        if (options.protocol !== 'http') {
          //  Disconnect, stop the timer and resolve.
          socket.destroy();
          return resolve(true);
        }

        //  TODO: we should only use the portion of the timeout for this interval which is still left to us.

        //  Now we've got to wait for a HTTP response.
        checkHttp(socket, options, timeout, (err) => {
          if (err) {
            if (err.code === 'EREQTIMEOUT') {
              debug('HTTP error: EREQTIMEOUT');
              socket.destroy();
              return resolve(false);
            } else if (err.code === 'ERESPONSE') {
              debug('HTTP error: ERESPONSE');
              socket.destroy();
              return resolve(false);
            }
            debug(`Unexpected error checking http response: ${err}`);
            socket.destroy();
            return reject(err);
          }

          socket.destroy();
          return resolve(true);
        });
      });
    } catch (err) {
      //  Trying to open the socket has resulted in an exception we don't
      //  understand. Better give up.
      debug(`Unexpected exception trying to open socket: ${err}`);
      return reject(err);
    }
  });
}
function tryConnect(options, timeout) {
  return new Promise((resolve, reject) => {
    try {
      const socket = createConnectionWithTimeout(options, timeout, (err) => {
        if (err) {
          if (err.code === 'ECONNREFUSED') {
            //  We successfully *tried* to connect, so resolve with false so
            //  that we try again.
            debug('Socket not open: ECONNREFUSED');
            socket.destroy();
            return resolve(false);
          } else if (err.code === 'ECONNTIMEOUT') {
            //  We've successfully *tried* to connect, but we're timing out
            //  establishing the connection. This is not ideal (either
            //  the port is open or it ain't).
            debug('Socket not open: ECONNTIMEOUT');
            socket.destroy();
            return resolve(false);
          } else if (err.code === 'ECONNRESET') {
            //  This can happen if the target server kills its connection before
            //  we can read from it, we can normally just try again.
            debug('Socket not open: ECONNRESET');
            socket.destroy();
            return resolve(false);
          }

          //  Trying to open the socket has resulted in an error we don't
          //  understand. Better give up.
          debug(`Unexpected error trying to open socket: ${err}`);
          socket.destroy();
          return reject(err);
        }
      
        //  Boom, we connected!
        debug('Socket connected!');

        //  If we are not dealing with http, we're done.
        if (options.protocol !== 'http') {
          //  Disconnect, stop the timer and resolve.
          socket.destroy();
          return resolve(true);
        }

        //  TODO: we should only use the portion of the timeout for this interval which is still left to us.

        //  Now we've got to wait for a HTTP response.
        checkHttp(socket, options, timeout, (err) => {
          if (err) {
            if (err.code === 'EREQTIMEOUT') {
              debug('HTTP error: EREQTIMEOUT');
              socket.destroy();
              return resolve(false);
            } else if (err.code === 'ERESPONSE') {
              debug('HTTP error: ERESPONSE');
              socket.destroy();
              return resolve(false);
            }
            debug(`Unexpected error checking http response: ${err}`);
            socket.destroy();
            return reject(err);
          }

          socket.destroy();
          return resolve(true);
        });
      });
    } catch (err) {
      //  Trying to open the socket has resulted in an exception we don't
      //  understand. Better give up.
      debug(`Unexpected exception trying to open socket: ${err}`);
      return reject(err);
    }
  });
}
function tryConnect ( options , timeout ) { return new Promise ( ( resolve , reject ) => { try { const socket = createConnectionWithTimeout ( options , timeout , ( err ) => { if ( err ) { if ( err . code === 'ECONNREFUSED' ) { //  We successfully *tried* to connect, so resolve with false so //  that we try again. debug ( 'Socket not open: ECONNREFUSED' ) ; socket . destroy ( ) ; return resolve ( false ) ; } else if ( err . code === 'ECONNTIMEOUT' ) { //  We've successfully *tried* to connect, but we're timing out //  establishing the connection. This is not ideal (either //  the port is open or it ain't). debug ( 'Socket not open: ECONNTIMEOUT' ) ; socket . destroy ( ) ; return resolve ( false ) ; } else if ( err . code === 'ECONNRESET' ) { //  This can happen if the target server kills its connection before //  we can read from it, we can normally just try again. debug ( 'Socket not open: ECONNRESET' ) ; socket . destroy ( ) ; return resolve ( false ) ; } //  Trying to open the socket has resulted in an error we don't //  understand. Better give up. debug ( ` ${ err } ` ) ; socket . destroy ( ) ; return reject ( err ) ; } //  Boom, we connected! debug ( 'Socket connected!' ) ; //  If we are not dealing with http, we're done. if ( options . protocol !== 'http' ) { //  Disconnect, stop the timer and resolve. socket . destroy ( ) ; return resolve ( true ) ; } //  TODO: we should only use the portion of the timeout for this interval which is still left to us. //  Now we've got to wait for a HTTP response. checkHttp ( socket , options , timeout , ( err ) => { if ( err ) { if ( err . code === 'EREQTIMEOUT' ) { debug ( 'HTTP error: EREQTIMEOUT' ) ; socket . destroy ( ) ; return resolve ( false ) ; } else if ( err . code === 'ERESPONSE' ) { debug ( 'HTTP error: ERESPONSE' ) ; socket . destroy ( ) ; return resolve ( false ) ; } debug ( ` ${ err } ` ) ; socket . destroy ( ) ; return reject ( err ) ; } socket . destroy ( ) ; return resolve ( true ) ; } ) ; } ) ; } catch ( err ) { //  Trying to open the socket has resulted in an exception we don't //  understand. Better give up. debug ( ` ${ err } ` ) ; return reject ( err ) ; } } ) ; }
This function attempts to open a connection, given a limited time window. This is the function which we will run repeatedly until we connect.
This function attempts to open a connection given a limited time window . This is the function which we will run repeatedly until we connect .