wait-on

repository·master·Indexed 24 days ago

https://github.com/jeffbski/wait-on

A cross-platform command line utility and Node.js API (version 9.1.0) used to wait for files, ports, sockets, and http(s) resources to become available. It is commonly used to synchronize tasks in build pipelines or service orchestration, supporting resource prefixes such as file:, http:, https:, tcp:, and socket:.

Tokens
3.4K
Snippets
7
Records
12
Agent score
75%

What's inside wait-on

  1. Use wait-on CLI to wait for resources

    master

    The wait-on CLI utility waits for files, ports, sockets, or HTTP(S) resources to become available. It exits with code 0 when all resources are ready, or a non-zero code if interrupted or timed out. You can combine it with other commands in a shell using && to run a command only after the resources are ready.

    Resource Types and Prefixes: Resources are identified by prefixes. If no prefix is provided, it is assumed to be a file.

    • file:: A regular file (default). Example: file:/path/to/file
    • http:: HTTP HEAD returns a 2XX response. Example: http://m.com:90/foo
    • https:: HTTPS HEAD returns a 2XX response. Example: https://my/bar
    • http-get:: HTTP GET returns a 2XX response. Example: http-get://m.com:90/foo
    • https-get:: HTTPS GET returns a 2XX response. Example: https-get://my/bar
    • tcp:: A TCP port is listening. Example: tcp:1.2.3.4:9000 or tcp:foo.com:700
    • socket:: A Domain Socket is listening. Example: socket:/path/to/sock
      • Note for HTTP over socket: Use the format http://unix:SOCK_PATH:URL_PATH. Example: http://unix:/path/to/sock:http://server/foo/bar
  2. Use wait-on via CLI

    master

    The wait-on CLI utility waits for specified resources to become available. Once all resources are ready, it exits with code 0, allowing you to chain subsequent commands using &&. If resources are not available within the timeout or are interrupted, it exits with a non-zero code, preventing the next command from running.

    Common usage patterns include waiting for files, HTTP endpoints, TCP ports, or Unix domain sockets.

    wait-on file1 && NEXT_CMD # wait for file1, then exec NEXT_CMD
    wait-on f1 f2 && NEXT_CMD # wait for both f1 and f2, then exec NEXT_CMD
    wait-on http://localhost:8000/foo && NEXT_CMD # wait for http 2XX HEAD
    wait-on tcp:4000 && NEXT_CMD # wait for service to listen on a TCP port
    wait-on socket:/path/mysock # wait for service to listen on domain socket
  3. Configure wait-on via a config file

    master
    You can specify options using a JavaScript or JSON configuration file via the --config flag. This is particularly useful for complex HTTP(S) options. The contents of the config file are required and merged with any command line options provided.
  4. Format time intervals in wait-on CLI

    master

    When using flags like --timeout, --interval, --httpTimeout, or --tcpTimeout, you can provide values as strings with time units. The CLI parses these into milliseconds.

    Supported units:

    • (no unit) or ms: Milliseconds (e.g., 500 or 500ms)
    • s: Seconds (e.g., 5s)
    • m: Minutes (e.g., 1m)
    • h: Hours (e.g., 1h)
  5. Use the wait-on Node.js API

    master

    You can use wait-on programmatically in your Node.js applications. It supports callbacks, Promises, and async/await syntax. The waitOn(opts) function accepts an options object to configure resources and polling behavior.

    var waitOn = require('wait-on');
    var opts = {
      resources: [
        'file1',
        'http://foo.com:8000/bar',
        'tcp:foo.com:8000'
      ],
      delay: 1000,
      interval: 100,
      timeout: 30000
    };
    
    // Usage with async await
    try {
      await waitOn(opts);
      // once here, all resources are available
    } catch (err) {
      handleError(err);
    }
  6. Configure HTTP(s) options in wait-on

    master

    When waiting for HTTP or HTTPS resources via the Node.js API, you can pass advanced configuration options (similar to TLS options or Axios config) within the opts object.

    var opts = {
      resources: ['http://foo.com:8000/bar'],
      // http options
      ca: [/* strings or binaries */],
      cert: [/* strings or binaries */],
      key: [/* strings or binaries */],
      passphrase: 'yourpassphrase',
      proxy: {
        host: '127.0.0.1',
        port: 9000,
        auth: { username: 'mikeymike', password: 'rapunz3l' }
      },
      auth: { user: 'theuser', pass: 'thepassword' },
      strictSSL: false,
      followRedirect: true,
      headers: { 'x-custom': 'headers' },
      validateStatus: function (status) {
        return status >= 200 && status < 300;
      }
    };
  7. Resource prefix types for wait-on

    master

    When using the CLI or the Node.js API, you specify the type of resource using a prefix. If no prefix is provided, the resource is assumed to be a file.

    file:      - regular file (also default type). ex: file:/path/to/file
    http:      - HTTP HEAD returns 2XX response. ex: http://m.com:90/foo
    https:     - HTTPS HEAD returns 2XX response. ex: https://my/bar
    http-get:  - HTTP GET returns 2XX response. ex: http://m.com:90/foo
    https-get: - HTTPS GET returns 2XX response. ex: https://my/bar
    tcp:       - TCP port is listening. ex: 1.2.3.4:9000 or foo.com:700
    socket:    - Domain Socket is listening. ex: socket:/path/to/sock
                For http over socket, use http://unix:SOCK_PATH:URL_PATH
                like http://unix:/path/to/sock:http://server/foo/bar
  8. Reference: wait-on CLI options

    master

    The following options are available for the wait-on command line utility:

    -c, --config
    
    js or json config file, useful for http(s) options
    
    -d, --delay
    
    Initial delay before checking for resources in ms, default 0
    
    --httpTimeout
    
    Maximum time in ms to wait for an HTTP HEAD/GET request, default 0
      which results in using the OS default
    
    -i, --interval
    
    Interval to poll resources in ms, default 250ms
    
    -l, --log
    
    Log resources begin waited on and when complete or errored
    
    -r, --reverse
    
    Reverse operation, wait for resources to NOT be available
    
    -s, --simultaneous
    
    Simultaneous / Concurrent connections to a resource, default Infinity
      Setting this to 1 would delay new requests until previous one has completed.
      Used to limit the number of connections attempted to a resource at a time.
    
    -t, --timeout
    
    Maximum time in ms to wait before exiting with failure (1) code, 
      default Infinity
      Use postfix 'ms', 's', 'm' or 'h' to change the unit.
    
    --tcpTimeout
    
    Maximum time in ms for tcp connect, default 300ms
      Use postfix 'ms', 's', 'm' or 'h' to change the unit.
    
    --httpTimeout
    
    Maximum time to wait for the HTTP request, default Infinity
      Use postfix 'ms', 's', 'm' or 'h' to change the unit.
    
    -v, --verbose
    
    Enable debug output to stdout
    
    -w, --window
    
    Stability window, the time in ms defining the window of time that
      resource needs to have not changed (file size/availability) before
      signaling success, default 750ms. If less than interval, it will be
      reset to the value of interval. This is only used for files, other
      resources are considered available on first detection.
    
    -h, --help
    
    Show this message
  9. CLI Options for wait-on

    master

    The wait-on CLI supports several options to control behavior, timeouts, and polling.

    -c, --config          js or json config file
    -d, --delay           Initial delay before checking for resources in ms, default 0
    --httpTimeout         Maximum time in ms to wait for an HTTP HEAD/GET request, default 0
    -i, --interval         Interval to poll resources in ms, default 250ms
    -l, --log              Log resources begin waited on and when complete or errored
    -r, --reverse          Reverse operation, wait for resources to NOT be available
    -s, --simultaneous     Simultaneous / Concurrent connections to a resource, default Infinity
    -t, --timeout          Maximum time in ms to wait before exiting with failure (1), default Infinity
    --tcpTimeout          Maximum time in ms for tcp connect, default 300ms
    -v, --verbose          Enable debug output to stdout
    -w, --window          Stability window, the time in ms defining the window of time that resource needs to have not changed before signalling success, default 750ms
    -h, --help             Show this message
  10. Configure wait-on CLI options

    master

    The following flags can be used to control the behavior of the wait-on CLI. Note that some flags have aliases.

    FlagAliasTypeDescription
    --config-cstringPath to a JS/JSON configuration file.
    --delay-dstringDelay before starting to check resources.
    --interval-istringInterval between checks. Supports time units (ms, s, m, h).
    --log-lbooleanEnable logging.
    --reverse-rbooleanWait for resources to become unavailable instead of available.
    --simultaneous-sbooleanWait for all resources to be available (default is waiting for any).
    --timeout-tstringTotal timeout for the entire operation. Supports time units (ms, s, m, h).
    --verbose-vbooleanEnable verbose output.
    --window-wstringWindow of time for resource availability.
    --httpTimeoutstringSpecific timeout for HTTP resources. Supports time units (ms, s, m, h).
    --tcpTimeoutstringSpecific timeout for TCP/socket resources. Supports time units (ms, s, m, h).
    --help-hbooleanDisplay help/usage information.
  11. Use the wait-on CLI to wait for resources

    master

    The wait-on CLI allows you to pause execution until specific files, ports, sockets, or HTTP(S) resources become available. You can pass resources directly as command-line arguments or via a configuration file.

    Precedence Rules:

    • Resources provided directly as command-line arguments take precedence over resources defined in a configuration file.
    • If no resources are provided via CLI arguments and no resources array is found in the config file, the CLI will display the usage information and exit.