UNPKG

11.3 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://travis-ci.com/jeffbski/wait-on.svg?branch=master)](https://travis-ci.com/jeffbski/wait-on)
12
13## Installation
14
15Latest version 4+ requires Node.js 10+
16
17(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
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'. Resources
71 can also be provided in the config file.
72
73 resource prefixes are:
74
75 file: - regular file (also default type). ex: file:/path/to/file
76 http: - HTTP HEAD returns 2XX response. ex: http://m.com:90/foo
77 https: - HTTPS HEAD returns 2XX response. ex: https://my/bar
78 http-get: - HTTP GET returns 2XX response. ex: http://m.com:90/foo
79 https-get: - HTTPS GET returns 2XX response. ex: https://my/bar
80 tcp: - TCP port is listening. ex: 1.2.3.4:9000 or foo.com:700
81 socket: - Domain Socket is listening. ex: socket:/path/to/sock
82 For http over socket, use http://unix:SOCK_PATH:URL_PATH
83 like http://unix:/path/to/sock:/foo/bar or
84 http-get://unix:/path/to/sock:/foo/bar
85
86Standard Options:
87
88 -c, --config
89
90 js or json config file, useful for http(s) options and resources
91
92 -d, --delay
93
94 Initial delay before checking for resources in ms, default 0
95
96 --httpTimeout
97
98 Maximum time in ms to wait for an HTTP HEAD/GET request, default 0
99 which results in using the OS default
100
101-i, --interval
102
103 Interval to poll resources in ms, default 250ms
104
105 -l, --log
106
107 Log resources begin waited on and when complete or errored
108
109 -r, --reverse
110
111 Reverse operation, wait for resources to NOT be available
112
113 -s, --simultaneous
114
115 Simultaneous / Concurrent connections to a resource, default Infinity
116 Setting this to 1 would delay new requests until previous one has completed.
117 Used to limit the number of connections attempted to a resource at a time.
118
119 -t, --timeout
120
121 Maximum time in ms to wait before exiting with failure (1) code,
122 default Infinity
123
124 --tcpTimeout
125
126 Maximum time in ms for tcp connect, default 300ms
127
128 -v, --verbose
129
130 Enable debug output to stdout
131
132 -w, --window
133
134 Stability window, the time in ms defining the window of time that
135 resource needs to have not changed (file size or availability) before
136 signalling success, default 750ms. If less than interval, it will be
137 reset to the value of interval. This is only used for files, other
138 resources are considered available on first detection.
139
140 -h, --help
141
142 Show this message
143```
144
145### Node.js API usage
146
147```javascript
148var waitOn = require('wait-on');
149var opts = {
150 resources: [
151 'file1',
152 'http://foo.com:8000/bar',
153 'https://my.com/cat',
154 'http-get://foo.com:8000/bar',
155 'https-get://my.com/cat',
156 'tcp:foo.com:8000',
157 'socket:/my/sock',
158 'http://unix:/my/sock:/my/url',
159 'http-get://unix:/my/sock:/my/url'
160 ],
161 delay: 1000, // initial delay in ms, default 0
162 interval: 100, // poll interval in ms, default 250ms
163 simultaneous: 1, // limit to 1 connection per resource at a time
164 timeout: 30000, // timeout in ms, default Infinity
165 tcpTimeout: 1000, // tcp timeout in ms, default 300ms
166 window: 1000, // stabilization time in ms, default 750ms
167
168 // http options
169 ca: [
170 /* strings or binaries */
171 ],
172 cert: [
173 /* strings or binaries */
174 ],
175 key: [
176 /* strings or binaries */
177 ],
178 passphrase: 'yourpassphrase',
179 proxy: false /* OR proxy config as defined in axios.
180 If not set axios detects proxy from env vars http_proxy and https_proxy
181 https://github.com/axios/axios#config-defaults
182 {
183 host: '127.0.0.1',
184 port: 9000,
185 auth: {
186 username: 'mikeymike',
187 password: 'rapunz3l'
188 }
189 } */,
190 auth: {
191 user: 'theuser', // or username
192 pass: 'thepassword' // or password
193 },
194 strictSSL: false,
195 followRedirect: true,
196 headers: {
197 'x-custom': 'headers'
198 },
199 validateStatus: function (status) {
200 return status >= 200 && status < 300; // default if not provided
201 }
202};
203
204// Usage with callback function
205waitOn(opts, function (err) {
206 if (err) {
207 return handleError(err);
208 }
209 // once here, all resources are available
210});
211
212// Usage with promises
213waitOn(opts)
214 .then(function () {
215 // once here, all resources are available
216 })
217 .catch(function (err) {
218 handleError(err);
219 });
220
221// Usage with async await
222try {
223 await waitOn(opts);
224 // once here, all resources are available
225} catch (err) {
226 handleError(err);
227}
228```
229
230waitOn(opts, [cb]) - function which triggers resource checks
231
232- opts.resources - array of string resources to wait for. prefix determines the type of resource with the default type of `file:`
233- opts.delay - optional initial delay in ms, default 0
234- opts.interval - optional poll resource interval in ms, default 250ms
235- opts.log - optional flag which outputs to stdout, remaining resources waited on and when complete or errored
236- opts.resources - optional array of string resources to wait for if none are specified via command line
237- opts.reverse - optional flag to reverse operation so checks are for resources being NOT available, default false
238- 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
239- opts.timeout - optional timeout in ms, default Infinity. Aborts with error.
240- opts.tcpTimeout - optional tcp timeout in ms, default 300ms
241- opts.verbose - optional flag which outputs debug output, default false
242- 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.
243- http(s) specific options, see https://nodejs.org/api/tls.html#tls_tls_connect_options_callback for specific details
244
245 - opts.ca: [ /* strings or binaries */ ],
246 - opts.cert: [ /* strings or binaries */ ],
247 - opts.key: [ /* strings or binaries */ ],
248 - opts.passphrase: 'yourpassphrase',
249 - 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
250
251```js
252 // example proxy object
253 {
254 host: '127.0.0.1',
255 port: 9000,
256 auth: {
257 username: 'mikeymike',
258 password: 'rapunz3l'
259 }
260 }
261```
262
263- opts.auth: { user, pass }
264- opts.strictSSL: false,
265- opts.followRedirect: false, // defaults to true
266- opts.headers: { 'x-custom': 'headers' },
267
268- cb(err) - if err is provided then, resource checks did not succeed
269
270## Goals
271
272- simple command line utility and Node.js API for waiting for resources
273- wait for files to stabilize
274- wait for http(s) resources to return 2XX in response to HEAD request
275- wait for http(s) resources to return 2XX in response to GET request
276- wait for services to be listening on tcp ports
277- wait for services to be listening on unix domain sockets
278- configurable initial delay, poll interval, stabilization window, timeout
279- command line utility returns success code (0) when resources are availble
280- 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.
281- cross platform - runs anywhere Node.js runs (linux, unix, mac OS X, windows)
282
283## Why
284
285I 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.
286
287## Get involved
288
289If you have input or ideas or would like to get involved, you may:
290
291- contact me via twitter @jeffbski - <http://twitter.com/jeffbski>
292- open an issue on github to begin a discussion - <https://github.com/jeffbski/wait-on/issues>
293- fork the repo and send a pull request (ideally with tests) - <https://github.com/jeffbski/wait-on>
294
295## License
296
297- [MIT license](http://github.com/jeffbski/wait-on/raw/master/LICENSE)