• Stars
    star
    1,860
  • Rank 24,935 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

wait-on is a cross-platform command line utility and Node.js API which will wait for files, ports, sockets, and http(s) resources to become available

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 wait-on version 7+ requires Node.js 12+ wait-on versions 4-6 requires Node.js 10+

(Node.js v8 users can use [email protected], v4 users can still use [email protected], and older Node.js engines, use [email protected])

npm install wait-on # local version
OR
npm install -g wait-on # global version

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.

wait-on file1 && NEXT_CMD # wait for file1, then exec NEXT_CMD
wait-on f1 f2 && NEXT_CMD # wait for both f1 and f2, the exec NEXT_CMD
wait-on http://localhost:8000/foo && NEXT_CMD # wait for http 2XX HEAD
wait-on https://myserver/foo && NEXT_CMD # wait for https 2XX HEAD
wait-on http-get://localhost:8000/foo && NEXT_CMD # wait for http 2XX GET
wait-on https-get://myserver/foo && NEXT_CMD # wait for https 2XX GET
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
wait-on http://unix:/var/SOCKPATH:/a/foo # wait for http HEAD on domain socket
wait-on http-get://unix:/var/SOCKPATH:/a/foo # wait for http GET on domain socket
Usage: wait-on {OPTIONS} resource [...resource]

Description:

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

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

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

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

     resource prefixes are:

       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:/foo/bar or
                         http-get://unix:/path/to/sock:/foo/bar

Standard Options:

 -c, --config

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

 -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

  --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 (file size or availability) before
  signalling 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

Node.js API usage

var waitOn = require('wait-on');
var opts = {
  resources: [
    'file1',
    'http://foo.com:8000/bar',
    'https://my.com/cat',
    'http-get://foo.com:8000/bar',
    'https-get://my.com/cat',
    'tcp:foo.com:8000',
    'socket:/my/sock',
    'http://unix:/my/sock:/my/url',
    'http-get://unix:/my/sock:/my/url'
  ],
  delay: 1000, // initial delay in ms, default 0
  interval: 100, // poll interval in ms, default 250ms
  simultaneous: 1, // limit to 1 connection per resource at a time
  timeout: 30000, // timeout in ms, default Infinity
  tcpTimeout: 1000, // tcp timeout in ms, default 300ms
  window: 1000, // stabilization time in ms, default 750ms

  // http options
  ca: [
    /* strings or binaries */
  ],
  cert: [
    /* strings or binaries */
  ],
  key: [
    /* strings or binaries */
  ],
  passphrase: 'yourpassphrase',
  proxy: false /* OR proxy config as defined in axios.
  If not set axios detects proxy from env vars http_proxy and https_proxy
  https://github.com/axios/axios#config-defaults
  {
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  } */,
  auth: {
    user: 'theuser', // or username
    pass: 'thepassword' // or password
  },
  strictSSL: false,
  followRedirect: true,
  headers: {
    'x-custom': 'headers'
  },
  validateStatus: function (status) {
    return status >= 200 && status < 300; // default if not provided
  }
};

// Usage with callback function
waitOn(opts, function (err) {
  if (err) {
    return handleError(err);
  }
  // once here, all resources are available
});

// Usage with promises
waitOn(opts)
  .then(function () {
    // once here, all resources are available
  })
  .catch(function (err) {
    handleError(err);
  });

// Usage with async await
try {
  await waitOn(opts);
  // once here, all resources are available
} catch (err) {
  handleError(err);
}

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
  // example proxy object
  {
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  }
  • 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:

License

More Repositories

1

redux-logic

Redux middleware for organizing all your business logic. Intercept actions and perform async processing.
JavaScript
1,807
star
2

bench-rest

bench-rest - benchmark REST (HTTP/HTTPS) API's. node.js client module for easy load testing / benchmarking REST API's using a simple structure/DSL can create REST flows with setup and teardown and returns (measured) metrics.
JavaScript
303
star
3

joi-browser

joi validation bundled for the browser (deprecated) - use joi@16+
JavaScript
273
star
4

pkglink

Space saving Node.js package hard linker. pkglink locates common JavaScript/Node.js packages from your node_modules directories and hard links the package files so they share disk space.
JavaScript
214
star
5

microservices

Microservices example code using Node.js, Redis, Hapi
JavaScript
150
star
6

autoflow

autoflow (formerly react) is a javascript module to make it easier to work with asynchronous code, by reducing boilerplate code and improving error and exception handling while allowing variable and task dependencies when defining flow.
JavaScript
97
star
7

redux-logic-test

redux-logic test utilities to facilitate the testing of logic. Create mock store
JavaScript
37
star
8

redis-rstream

redis-rstream is a node.js redis read stream which streams binary or utf8 data in chunks from a redis key using an existing redis client (streams2). Tested with mranney/node_redis client.
JavaScript
33
star
9

digest-stream

Simple node.js pass-through stream (RW) which calculates the a crypto digest (sha/md5 hash) of a stream and also the length. Pipe your stream through this to get digest and length. (streams2)
JavaScript
30
star
10

markdown-it-modify-token

markdown-it plugin for modifying tokens in a markdown document. It can for example modify content or attributes for certain type of elements like links or images.
JavaScript
25
star
11

redis-wstream

redis-wstream is a node.js redis write stream which streams binary or utf8 data into a redis key using an existing redis client (streams2). Tested with mranney/node_redis client.
JavaScript
20
star
12

pass-stream

pass-stream - pass-through node.js stream which can filter/adapt and pause data
JavaScript
17
star
13

base-react-min

Minimal React.js boilerplate with an auto build environment
JavaScript
12
star
14

ensure-array

Ensure that a javascript object is an array, coerce if necessary. Move error checking out of your node.js javascript code.
JavaScript
12
star
15

autoflow-graphviz

autflow-graphviz is a plugin for autflow, the flow control rules engine, which allows react to use graphviz to generate flow diagrams for the dependencies
JavaScript
7
star
16

accum

Simple stream sink which accumulates or collects the data from a stream. Pipe your stream into this to get all the data as buffer, string, or raw array. (streams2)
JavaScript
6
star
17

hapi-bunyan-lite

Minimalistic Bunyan logging integration for Hapi - forwards Hapi log events to bunyan
JavaScript
6
star
18

ngzip

ngzip is a portable streaming stdio gzip command line utility that uses Node.js zlib. It should run anywhere Node.js runs including windows, *nix, mac.
JavaScript
5
star
19

tapper

Tapper (tapr) is a node.js tap runner which improves formatting and allows stdout and stderr mixed in with the tap output. Also optionally adds color to the output
JavaScript
4
star
20

joi-full

joi object schema validation with extensions as universal/isomorphic libarary for Node.js and bundled for the browser (babelified and bundled)
JavaScript
4
star
21

autoflow-deferred

autoflow-deferred is a plugin for react, the flow control rules engine, which adds integration with jQuery-style Deferred promises
JavaScript
4
star
22

jsconf-react

JSConf 2015 React.js training workshop
JavaScript
3
star
23

length-stream

Simple pass-through node.js stream (RW) which accumulates the length of the stream. (streams2)
JavaScript
3
star
24

git-clone-init

Create new project from clone - clone repo, rm .git, git init, sed files, git commit
Shell
3
star
25

autoflow-q

autoflow-q is a plugin for autoflow, the flow control rules engine, which adds integration with Q-style Deferred promises https://github.com/kriskowal/q
JavaScript
3
star
26

redux-util

redux utilities for easily constructing action creators and reducers
JavaScript
2
star
27

redux-logic-examples

Shell
2
star
28

function.bind.js

Polyfill old webkit and iOS browsers with Function.prototype.bind
JavaScript
2
star
29

modulize

Safe, easy method override/extension without manual alias_method chaining
Ruby
2
star
30

base-react-slim

base boilerplate react environment with auto-build env w/o testing framework
JavaScript
2
star
31

chai-stack

Light wrapper around chaijs which automatically sets chai.Assertion.includeStack = true
JavaScript
2
star
32

jsconf-react-win

JSConf 2015 React.js training workshop (Windows version)
JavaScript
2
star
33

joi-date-extensions-browser

joi-date-extensions bundled for the browser
JavaScript
1
star
34

base-react-min-win

Minimal React.js boilerplate with an auto build environment for Windows
JavaScript
1
star
35

feature-u-bookmark-notes

Simple React.js, redux, redux-logic app using feature-u to showcase feature-based development
JavaScript
1
star
36

waverider

waverider CMS - lightweight fast CMS/blog with realtime edit and preview written in javascript for node.js
JavaScript
1
star