UNPKG

10.1 kBMarkdownView Raw
1# wait-on - wait for files, ports, sockets, http(s) resources
2
3wait-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)
4
5wait-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.
6
7For http(s) resources wait-on will check that the requests are returning 2XX (success) to HEAD or GET requests (after following any redirects).
8
9wait-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)
10
11[![Build Status](https://secure.travis-ci.org/jeffbski/wait-on.png?branch=master)](http://travis-ci.org/jeffbski/wait-on)
12
13## Installation
14
15Latest version 3+ requires Node.js v8.9+
16
17(Node.js v4 users can still use wait-on@2.1.2, and older Node.js
18engines, use wait-on@1.5.4)
19
20```bash
21npm install wait-on # local version
22OR
23npm install -g wait-on # global version
24```
25
26## Usage
27
28Use from command line or using Node.js programmatic API.
29
30### CLI Usage
31
32Assuming 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.
33
34wait-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)
35
36If 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.
37
38```bash
39wait-on file1 && NEXT_CMD # wait for file1, then exec NEXT_CMD
40wait-on f1 f2 && NEXT_CMD # wait for both f1 and f2, the exec NEXT_CMD
41wait-on http://localhost:8000/foo && NEXT_CMD # wait for http 2XX HEAD
42wait-on https://myserver/foo && NEXT_CMD # wait for https 2XX HEAD
43wait-on http-get://localhost:8000/foo && NEXT_CMD # wait for http 2XX GET
44wait-on https-get://myserver/foo && NEXT_CMD # wait for https 2XX GET
45wait-on tcp:4000 && NEXT_CMD # wait for service to listen on a TCP port
46wait-on socket:/path/mysock # wait for service to listen on domain socket
47wait-on http://unix:/var/SOCKPATH:/a/foo # wait for http HEAD on domain socket
48wait-on http-get://unix:/var/SOCKPATH:/a/foo # wait for http GET on domain socket
49```
50
51```
52Usage: wait-on {OPTIONS} resource [...resource]
53
54Description:
55
56 wait-on is a command line utility which will wait for files, ports,
57 sockets, and http(s) resources to become available (or not available
58 using reverse flag). Exits with success code (0) when all resources
59 are ready. Non-zero exit code if interrupted or timed out.
60
61 Options may also be specified in a config file (js or json). For
62 example --config configFile.js would result in configFile.js being
63 required and the resulting object will be merged with any
64 command line options before wait-on is called. See exampleConfig.js
65
66 In shell combine with && to conditionally run another command
67 once resources are available. ex: wait-on f1 && NEXT_CMD
68
69 resources types are defined by their prefix, if no prefix is
70 present, the resource is assumed to be of type 'file'
71
72 resource prefixes are:
73
74 file: - regular file (also default type). ex: file:/path/to/file
75 http: - HTTP HEAD returns 2XX response. ex: http://m.com:90/foo
76 https: - HTTPS HEAD returns 2XX response. ex: https://my/bar
77 http-get: - HTTP GET returns 2XX response. ex: http://m.com:90/foo
78 https-get: - HTTPS GET returns 2XX response. ex: https://my/bar
79 tcp: - TCP port is listening. ex: 1.2.3.4:9000 or foo.com:700
80 socket: - Domain Socket is listening. ex: socket:/path/to/sock
81 For http over socket, use http://unix:SOCK_PATH:URL_PATH
82 like http://unix:/path/to/sock:/foo/bar or
83 http-get://unix:/path/to/sock:/foo/bar
84
85Standard Options:
86
87 -c, --config
88
89 js or json config file, useful for http(s) options
90
91 -d, --delay
92
93 Initial delay before checking for resources in ms, default 0
94
95 --httpTimeout
96
97 Maximum time in ms to wait for an HTTP HEAD/GET request, default 0
98 which results in using the OS default
99
100-i, --interval
101
102 Interval to poll resources in ms, default 250ms
103
104 -l, --log
105
106 Log resources begin waited on and when complete or errored
107
108 -r, --reverse
109
110 Reverse operation, wait for resources to NOT be available
111
112 -s, --simultaneous
113
114 Simultaneous / Concurrent connections to a resource, default Infinity
115 Setting this to 1 would delay new requests until previous one has completed.
116 Used to limit the number of connections attempted to a resource at a time.
117
118 -t, --timeout
119
120 Maximum time in ms to wait before exiting with failure (1) code,
121 default Infinity
122
123 --tcpTimeout
124
125 Maximum time in ms for tcp connect, default 300ms
126
127 -v, --verbose
128
129 Enable debug output to stdout
130
131 -w, --window
132
133 Stability window, the time in ms defining the window of time that
134 resource needs to have not changed (file size or availability) before
135 signalling success, default 750ms. If less than interval, it will be
136 reset to the value of interval. This is only used for files, other
137 resources are considered available on first detection.
138
139 -h, --help
140
141 Show this message
142```
143
144### Node.js API usage
145
146```javascript
147var waitOn = require('wait-on');
148var opts = {
149 resources: [
150 'file1',
151 'http://foo.com:8000/bar',
152 'https://my.com/cat',
153 'http-get://foo.com:8000/bar',
154 'https-get://my.com/cat',
155 'tcp:foo.com:8000',
156 'socket:/my/sock',
157 'http://unix:/my/sock:/my/url',
158 'http-get://unix:/my/sock:/my/url',
159 ],
160 delay: 1000, // initial delay in ms, default 0
161 interval: 100, // poll interval in ms, default 250ms
162 timeout: 30000, // timeout in ms, default Infinity
163 tcpTimeout: 1000, // tcp timeout in ms, default 300ms
164 window: 1000, // stabilization time in ms, default 750ms
165
166 // http options
167 ca: [
168 /* strings or binaries */
169 ],
170 cert: [
171 /* strings or binaries */
172 ],
173 key: [
174 /* strings or binaries */
175 ],
176 passphrase: 'yourpassphrase',
177 auth: {
178 user: 'theuser', // or username
179 pass: 'thepassword', // or password
180 },
181 strictSSL: false,
182 followRedirect: true,
183 headers: {
184 'x-custom': 'headers',
185 },
186 validateStatus: function (status) {
187 return status >= 200 && status < 300; // default if not provided
188 },
189};
190
191// Usage with callback function
192waitOn(opts, function (err) {
193 if (err) {
194 return handleError(err);
195 }
196 // once here, all resources are available
197});
198
199// Usage with promises
200waitOn(opts)
201 .then(function () {
202 // once here, all resources are available
203 })
204 .catch(function (err) {
205 handleError(err);
206 });
207
208// Usage with async await
209try {
210 await waitOn(opts);
211 // once here, all resources are available
212} catch (err) {
213 handleError(err);
214}
215```
216
217waitOn(opts, [cb]) - function which triggers resource checks
218
219- opts.resources - array of string resources to wait for. prefix determines the type of resource with the default type of `file:`
220- opts.delay - optional initial delay in ms, default 0
221- opts.interval - optional poll resource interval in ms, default 250ms
222- opts.log - optional flag which outputs to stdout, remaining resources waited on and when complete or errored
223- opts.reverse - optional flag to reverse operation so checks are for resources being NOT available, default false
224- opts.timeout - optional timeout in ms, default Infinity. Aborts with error.
225- opts.tcpTimeout - optional tcp timeout in ms, default 300ms
226- opts.verbose - optional flag which outputs debug output, default false
227- 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.
228- http(s) specific options, see https://nodejs.org/api/tls.html#tls_tls_connect_options_callback for specific details
229
230 - opts.ca: [ /* strings or binaries */ ],
231 - opts.cert: [ /* strings or binaries */ ],
232 - opts.key: [ /* strings or binaries */ ],
233 - opts.passphrase: 'yourpassphrase',
234 - opts.auth: { user, pass }
235 - opts.strictSSL: false,
236 - opts.followRedirect: false, // defaults to true
237 - opts.headers: { 'x-custom': 'headers' },
238
239- cb(err) - if err is provided then, resource checks did not succeed
240
241## Goals
242
243- simple command line utility and Node.js API for waiting for resources
244- wait for files to stabilize
245- wait for http(s) resources to return 2XX in response to HEAD request
246- wait for http(s) resources to return 2XX in response to GET request
247- wait for services to be listening on tcp ports
248- wait for services to be listening on unix domain sockets
249- configurable initial delay, poll interval, stabilization window, timeout
250- command line utility returns success code (0) when resources are availble
251- 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.
252- cross platform - runs anywhere Node.js runs (linux, unix, mac OS X, windows)
253
254## Why
255
256I 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.
257
258## Get involved
259
260If you have input or ideas or would like to get involved, you may:
261
262- contact me via twitter @jeffbski - <http://twitter.com/jeffbski>
263- open an issue on github to begin a discussion - <https://github.com/jeffbski/wait-on/issues>
264- fork the repo and send a pull request (ideally with tests) - <https://github.com/jeffbski/wait-on>
265
266## License
267
268- [MIT license](http://github.com/jeffbski/wait-on/raw/master/LICENSE)