wait-on

A cross-platform command line utility and Node.js API which will wait for f...

README

wait-on - wait for files, ports, sockets, http(s) resources


wait-on is a cross-platform command line utility which will wait for files, ports, sockets, and http(s) resources to become available (or not available using reverse mode). Functionality is also available via a Node.js API. Cross-platform - runs everywhere Node.js runs (linux, unix, mac OS X, windows)

wait-on will wait for period of time for a file to stop growing before triggering availability which is good for monitoring files that are being built. Likewise wait-on will wait for period of time for other resources to remain available before triggering success.

For http(s) resources wait-on will check that the requests are returning 2XX (success) to HEAD or GET requests (after following any redirects).

wait-on can also be used in reverse mode which waits for resources to NOT be available. This is useful in waiting for services to shutdown before continuing. (Thanks @skarbovskiy for adding this feature)
Build Status

Installation


Latest version 4+ requires Node.js 10+

(Node.js v8 users can use wait-on@5.3.0, v4 users can still use wait-on@2.1.2, and older Node.js
engines, use wait-on@1.5.4)

  1. ``` sh
  2. npm install wait-on # local version
  3. OR
  4. npm install -g wait-on # global version
  5. ```

Usage


Use from command line or using Node.js programmatic API.

CLI Usage


Assuming NEXT_CMD is the command to run when resources are available, then wait-on will wait and then exit with a successful exit code (0) once all resources are available, causing NEXT_CMD to be run.

wait-on can also be used in reverse mode, which waits for resources to NOT be available. This is useful in waiting for services to shutdown before continuing. (Thanks @skarbovskiy for adding)

If wait-on is interrupted before all resources are available, it will exit with a non-zero exit code and thus NEXT_CMD will not be run.

  1. ``` sh
  2. wait-on file1 && NEXT_CMD # wait for file1, then exec NEXT_CMD
  3. wait-on f1 f2 && NEXT_CMD # wait for both f1 and f2, the exec NEXT_CMD
  4. wait-on http://localhost:8000/foo && NEXT_CMD # wait for http 2XX HEAD
  5. wait-on https://myserver/foo && NEXT_CMD # wait for https 2XX HEAD
  6. wait-on http-get://localhost:8000/foo && NEXT_CMD # wait for http 2XX GET
  7. wait-on https-get://myserver/foo && NEXT_CMD # wait for https 2XX GET
  8. wait-on tcp:4000 && NEXT_CMD # wait for service to listen on a TCP port
  9. wait-on socket:/path/mysock # wait for service to listen on domain socket
  10. wait-on http://unix:/var/SOCKPATH:/a/foo # wait for http HEAD on domain socket
  11. wait-on http-get://unix:/var/SOCKPATH:/a/foo # wait for http GET on domain socket
  12. ```

  1. ```
  2. Usage: wait-on {OPTIONS} resource [...resource]

  3. Description:

  4.      wait-on is a command line utility which will wait for files, ports,
  5.      sockets, and http(s) resources to become available (or not available
  6.      using reverse flag). Exits with  success code (0) when all resources
  7.      are ready. Non-zero exit code if interrupted or timed out.

  8.      Options may also be specified in a config file (js or json). For
  9.      example --config configFile.js would result in configFile.js being
  10.      required and the resulting object will be merged with any
  11.      command line options before wait-on is called. See exampleConfig.js

  12.      In shell combine with && to conditionally run another command
  13.      once resources are available. ex: wait-on f1 && NEXT_CMD

  14.      resources types are defined by their prefix, if no prefix is
  15.      present, the resource is assumed to be of type 'file'. Resources
  16.      can also be provided in the config file.

  17.      resource prefixes are:

  18.        file:      - regular file (also default type). ex: file:/path/to/file
  19.        http:      - HTTP HEAD returns 2XX response. ex: http://m.com:90/foo
  20.        https:     - HTTPS HEAD returns 2XX response. ex: https://my/bar
  21.        http-get:  - HTTP GET returns 2XX response. ex: http://m.com:90/foo
  22.        https-get: - HTTPS GET returns 2XX response. ex: https://my/bar
  23.        tcp:       - TCP port is listening. ex: 1.2.3.4:9000 or foo.com:700
  24.        socket:    - Domain Socket is listening. ex: socket:/path/to/sock
  25.                     For http over socket, use http://unix:SOCK_PATH:URL_PATH
  26.                     like http://unix:/path/to/sock:/foo/bar or
  27.                          http-get://unix:/path/to/sock:/foo/bar

  28. Standard Options:

  29. -c, --config

  30.   js or json config file, useful for http(s) options and resources

  31. -d, --delay

  32.   Initial delay before checking for resources in ms, default 0

  33. --httpTimeout

  34.   Maximum time in ms to wait for an HTTP HEAD/GET request, default 0
  35.   which results in using the OS default

  36. -i, --interval

  37.   Interval to poll resources in ms, default 250ms

  38. -l, --log

  39.   Log resources begin waited on and when complete or errored

  40. -r, --reverse

  41.   Reverse operation, wait for resources to NOT be available

  42. -s, --simultaneous

  43.   Simultaneous / Concurrent connections to a resource, default Infinity
  44.   Setting this to 1 would delay new requests until previous one has completed.
  45.   Used to limit the number of connections attempted to a resource at a time.

  46. -t, --timeout

  47.   Maximum time in ms to wait before exiting with failure (1) code,
  48.   default Infinity

  49.   --tcpTimeout

  50.    Maximum time in ms for tcp connect, default 300ms

  51. -v, --verbose

  52.   Enable debug output to stdout

  53. -w, --window

  54.   Stability window, the time in ms defining the window of time that
  55.   resource needs to have not changed (file size or availability) before
  56.   signalling success, default 750ms. If less than interval, it will be
  57.   reset to the value of interval. This is only used for files, other
  58.   resources are considered available on first detection.

  59. -h, --help

  60.   Show this message
  61. ```

Node.js API usage


  1. ``` js
  2. var waitOn = require('wait-on');
  3. var opts = {
  4.   resources: [
  5.     'file1',
  6.     'http://foo.com:8000/bar',
  7.     'https://my.com/cat',
  8.     'http-get://foo.com:8000/bar',
  9.     'https-get://my.com/cat',
  10.     'tcp:foo.com:8000',
  11.     'socket:/my/sock',
  12.     'http://unix:/my/sock:/my/url',
  13.     'http-get://unix:/my/sock:/my/url'
  14.   ],
  15.   delay: 1000, // initial delay in ms, default 0
  16.   interval: 100, // poll interval in ms, default 250ms
  17.   simultaneous: 1, // limit to 1 connection per resource at a time
  18.   timeout: 30000, // timeout in ms, default Infinity
  19.   tcpTimeout: 1000, // tcp timeout in ms, default 300ms
  20.   window: 1000, // stabilization time in ms, default 750ms

  21.   // http options
  22.   ca: [
  23.     /* strings or binaries */
  24.   ],
  25.   cert: [
  26.     /* strings or binaries */
  27.   ],
  28.   key: [
  29.     /* strings or binaries */
  30.   ],
  31.   passphrase: 'yourpassphrase',
  32.   proxy: false /* OR proxy config as defined in axios.
  33.   If not set axios detects proxy from env vars http_proxy and https_proxy
  34.   https://github.com/axios/axios#config-defaults
  35.   {
  36.     host: '127.0.0.1',
  37.     port: 9000,
  38.     auth: {
  39.       username: 'mikeymike',
  40.       password: 'rapunz3l'
  41.     }
  42.   } */,
  43.   auth: {
  44.     user: 'theuser', // or username
  45.     pass: 'thepassword' // or password
  46.   },
  47.   strictSSL: false,
  48.   followRedirect: true,
  49.   headers: {
  50.     'x-custom': 'headers'
  51.   },
  52.   validateStatus: function (status) {
  53.     return status >= 200 && status < 300; // default if not provided
  54.   }
  55. };

  56. // Usage with callback function
  57. waitOn(opts, function (err) {
  58.   if (err) {
  59.     return handleError(err);
  60.   }
  61.   // once here, all resources are available
  62. });

  63. // Usage with promises
  64. waitOn(opts)
  65.   .then(function () {
  66.     // once here, all resources are available
  67.   })
  68.   .catch(function (err) {
  69.     handleError(err);
  70.   });

  71. // Usage with async await
  72. try {
  73.   await waitOn(opts);
  74.   // once here, all resources are available
  75. } catch (err) {
  76.   handleError(err);
  77. }
  78. ```

waitOn(opts, [cb]) - function which triggers resource checks

- opts.resources - array of string resources to wait for. prefix determines the type of resource with the default type of file:
- opts.delay - optional initial delay in ms, default 0
- opts.interval - optional poll resource interval in ms, default 250ms
- opts.log - optional flag which outputs to stdout, remaining resources waited on and when complete or errored
- opts.resources - optional array of string resources to wait for if none are specified via command line
- opts.reverse - optional flag to reverse operation so checks are for resources being NOT available, default false
- opts.simultaneous - optional count to limit concurrent connections per resource at a time, setting to 1 waits for previous connection to succeed, fail, or timeout before sending another, default infinity
- opts.timeout - optional timeout in ms, default Infinity. Aborts with error.
- opts.tcpTimeout - optional tcp timeout in ms, default 300ms
- opts.verbose - optional flag which outputs debug output, default false
- opts.window - optional stabilization time in ms, default 750ms. Waits this amount of time for file sizes to stabilize or other resource availability to remain unchanged.
- http(s) specific options, see https://nodejs.org/api/tls.html#tls_tls_connect_options_callback for specific details

  - opts.ca: [ / strings or binaries / ],
  - opts.cert: [ / strings or binaries / ],
  - opts.key: [ / strings or binaries / ],
  - opts.passphrase: 'yourpassphrase',
  - opts.proxy: undefined, false, or object as defined in axios. Default is undefined. If not set axios detects proxy from env vars http_proxy and https_proxy. https://github.com/axios/axios#config-defaults

  1. ``` js
  2.   // example proxy object
  3.   {
  4.     host: '127.0.0.1',
  5.     port: 9000,
  6.     auth: {
  7.       username: 'mikeymike',
  8.       password: 'rapunz3l'
  9.     }
  10.   }
  11. ```

- opts.auth: { user, pass }
- opts.strictSSL: false,
- opts.followRedirect: false, // defaults to true
- opts.headers: { 'x-custom': 'headers' },

- cb(err) - if err is provided then, resource checks did not succeed

Goals


- simple command line utility and Node.js API for waiting for resources
- wait for files to stabilize
- wait for http(s) resources to return 2XX in response to HEAD request
- wait for http(s) resources to return 2XX in response to GET request
- wait for services to be listening on tcp ports
- wait for services to be listening on unix domain sockets
- configurable initial delay, poll interval, stabilization window, timeout
- command line utility returns success code (0) when resources are availble
- command line utility that can also wait for resources to not be available using reverse flag. This is useful for waiting for services to shutdown before continuing.
- cross platform - runs anywhere Node.js runs (linux, unix, mac OS X, windows)

Why


I frequently need to wait on build tasks to complete or services to be available before starting next command, so this project makes that easier and is portable to everywhere Node.js runs.

Get involved


If you have input or ideas or would like to get involved, you may:

- contact me via twitter @jeffbski - - open an issue on github to begin a discussion - - fork the repo and send a pull request (ideally with tests) -

License