diff --git a/README.md b/README.md index b4d7d58..135e242 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ The following parameters are accepted: | `` | Required. The target to test for. Can be just a port, a colon and port (as one would use with [httpie](https://httpie.org/) or host and port. Examples: `8080`, `:3000`, `127.0.0.1:443`. | | `--output, -o` | Optional. Output style to use. Can be `dots` (default) or `silent` (no output). | | `--timeout, -t` | Optional. Timeout (in milliseconds). | +| `--wait-for-dns` | Optional. Do not error if the response is `ENOTFOUND`, just keep on waiting (useful if you are waiting for a DNS record to also be created). | ### Error Codes @@ -108,6 +109,7 @@ The CLI is a very shallow wrapper around this function. The `params` object take | `` | `port` | Required. Port to wait for. | | `--output` | `output` | Optional. Defaults to `dots`. Output style to use. `silent` also accepted. | | `--timeout, -t` | `timeout` | Optional. Defaults to `0`. Timeout (in milliseconds). If `0`, then the operation will never timeout. | +| `--wait-for-dns` | `waitForDns` | Optional. Defaults to `false`. | # Developer Guide diff --git a/bin/wait-port.js b/bin/wait-port.js index e2e6cec..58a2d93 100755 --- a/bin/wait-port.js +++ b/bin/wait-port.js @@ -13,15 +13,18 @@ program .description('Wait for a target to accept connections, e.g: wait-port localhost:8080') .option('-t, --timeout [n]', 'Timeout', parseInt) .option('-o, --output [mode]', 'Output mode (silent, dots). Default is silent.') + .option('--wait-for-dns', 'Do not fail on ENOTFOUND, meaning you can wait for DNS record creation. Default is false.') .arguments('') .action((target) => { // Validate the parameters (extractTarget) will throw if target is invalid). const { protocol, host, port, path } = extractTarget(target); const timeout = program.timeout || 0; const output = program.output; + const waitForDns = program.waitForDns; debug(`Timeout: ${timeout}`); debug(`Target: ${target} => ${protocol}://${host}:${port}${path}`); + debug(`waitForDns: ${waitForDns}`); const params = { timeout, @@ -29,7 +32,8 @@ program host, port, path, - output + output, + waitForDns, }; waitPort(params) @@ -59,6 +63,7 @@ program.on('--help', () => { console.log(' $ wait-port -t 10 :8080'); console.log(' $ wait-port google.com:443'); console.log(' $ wait-port http://localhost:5000/healthcheck'); + console.log(' $ wait-port --wait-for-dns http://mynewdomain.com:80'); console.log(''); }); diff --git a/lib/validate-parameters.js b/lib/validate-parameters.js index 6209186..ca23004 100644 --- a/lib/validate-parameters.js +++ b/lib/validate-parameters.js @@ -32,6 +32,10 @@ function validateParameters(params) { if (!Number.isInteger(timeout)) throw new ValidationError('\'timeout\' must be a number.'); if (timeout < 0) throw new ValidationError('\'timeout\' must be greater or equal to 0.'); + // Validate and coerce the wait-for-dns parameter. + const waitForDns = params.waitForDns || false; + if (typeof waitForDns !== 'boolean') throw new ValidationError('\'wait-for-dns\' must be a boolean.'); + // Coerce the output. const output = params.output || 'dots'; @@ -50,7 +54,8 @@ function validateParameters(params) { path, interval, timeout, - output + output, + waitForDns, }; } diff --git a/lib/validate-parameters.spec.js b/lib/validate-parameters.spec.js index abb7b0a..39ea9bd 100644 --- a/lib/validate-parameters.spec.js +++ b/lib/validate-parameters.spec.js @@ -8,7 +8,8 @@ describe('validateParameters', () => { port: 8080, interval: 2000, timeout: 0, - output: 'silent' + output: 'silent', + waitForDns: false, }; }; @@ -126,4 +127,21 @@ describe('validateParameters', () => { }); + it('should default the wait-for-dns flag to false', () => { + + const params = validParams(); + delete params.waitForDns; + const validatedParams = validateParameters(params); + assert.equal(validatedParams.waitForDns, false); + + }); + + it('should throw if the wait-for-dns parameter is not a boolean', () => { + + const params = validParams(); + params.waitForDns = 'not-a-boolean'; + assert.throws(() => validateParameters(params), /wait-for-dns.*boolean.*/); + + }); + });