Skip to content

Commit

Permalink
feat: add the 'wait-for-dns' flag
Browse files Browse the repository at this point in the history
Allows the user to wait even if the address cannot be resolved; useful
if you know you will have to wait for DNS records to be created. Closes #41
  • Loading branch information
dwmkerr committed Oct 3, 2019
1 parent d617780 commit ae3f478
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The following parameters are accepted:
| `<target>` | 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

Expand Down Expand Up @@ -108,6 +109,7 @@ The CLI is a very shallow wrapper around this function. The `params` object take
| `<target>` | `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

Expand Down
7 changes: 6 additions & 1 deletion bin/wait-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ 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('<target>')
.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,
protocol,
host,
port,
path,
output
output,
waitForDns,
};

waitPort(params)
Expand Down Expand Up @@ -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('');
});

Expand Down
7 changes: 6 additions & 1 deletion lib/validate-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -50,7 +54,8 @@ function validateParameters(params) {
path,
interval,
timeout,
output
output,
waitForDns,
};
}

Expand Down
20 changes: 19 additions & 1 deletion lib/validate-parameters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('validateParameters', () => {
port: 8080,
interval: 2000,
timeout: 0,
output: 'silent'
output: 'silent',
waitForDns: false,
};
};

Expand Down Expand Up @@ -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.*/);

});

});

0 comments on commit ae3f478

Please sign in to comment.