Comparing version 0.2.4 to 0.2.6
@@ -7,4 +7,5 @@ #!/usr/bin/env node | ||
const extractTarget = require('../lib/extract-target'); | ||
const ConnectionError = require('../lib/errors/connection-error'); | ||
const TargetError = require('../lib/errors/target-error'); | ||
const ValidationError = require('../lib/errors/validation-error'); | ||
const ConnectionError = require('../lib/errors/connection-error'); | ||
const waitPort = require('../lib/wait-port'); | ||
@@ -19,40 +20,41 @@ | ||
.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; | ||
.action(async (target) => { | ||
try { | ||
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}`); | ||
debug(`Timeout: ${timeout}`); | ||
debug(`Target: ${target} => ${protocol}://${host}:${port}${path}`); | ||
debug(`waitForDns: ${waitForDns}`); | ||
const params = { | ||
timeout, | ||
protocol, | ||
host, | ||
port, | ||
path, | ||
output, | ||
waitForDns, | ||
}; | ||
const params = { | ||
timeout, | ||
protocol, | ||
host, | ||
port, | ||
path, | ||
output, | ||
waitForDns, | ||
}; | ||
waitPort(params) | ||
.then((open) => { | ||
process.exit(open ? 0 : 1); | ||
}) | ||
.catch((err) => { | ||
// Show validation errors in red. | ||
if (err instanceof ValidationError) { | ||
console.error(`\n\n ${chalk.red(err.message)}`); | ||
process.exit(2); | ||
} else if (err instanceof ConnectionError) { | ||
console.error(`\n\n ${chalk.red(err.message)}`); | ||
process.exit(4); | ||
} else { | ||
console.error(`Unknown error occurred waiting for ${target}: ${err}`); | ||
process.exit(3); | ||
} | ||
}); | ||
const open = await waitPort(params); | ||
process.exit(open ? 0 : 1); | ||
} catch (err) { | ||
// Show validation errors in red. | ||
if (err instanceof ValidationError) { | ||
console.error(chalk.red(`\n Validation Error: ${err.message}`)); | ||
process.exit(2); | ||
} else if (err instanceof ConnectionError) { | ||
console.error(chalk.red(`\n\n Connection Error Error: ${err.message}`)); | ||
process.exit(4); | ||
} else if (err instanceof TargetError) { | ||
console.error(chalk.red(`\n Target Error: ${err.message}`)); | ||
process.exit(4); | ||
} else { | ||
console.error(chalk.red(`\n Unknown error occurred waiting for ${target}: ${err}`)); | ||
process.exit(3); | ||
} | ||
} | ||
}); | ||
@@ -59,0 +61,0 @@ |
@@ -5,2 +5,11 @@ # Changelog | ||
### [0.2.6](https://github.com/dwmkerr/wait-port/compare/v0.2.5...v0.2.6) (2019-10-07) | ||
### Features | ||
* better error messages for invalid targets ([e4a2f31](https://github.com/dwmkerr/wait-port/commit/e4a2f31)), closes [#43](https://github.com/dwmkerr/wait-port/issues/43) | ||
### [0.2.5](https://github.com/dwmkerr/wait-port/compare/v0.2.4...v0.2.5) (2019-10-07) | ||
### [0.2.4](https://github.com/dwmkerr/wait-port/compare/v0.2.3...v0.2.4) (2019-10-03) | ||
@@ -7,0 +16,0 @@ |
interface ServerLocation { | ||
/** The port to wait for */ | ||
port: number; | ||
/** The host to check, if not localhost */ | ||
host?: string; | ||
/** Set to 'http' to test an HTTP request as well */ | ||
protocol?: 'http'; | ||
/** If using the 'http' protocol, the path to check */ | ||
path?: string; | ||
/** The number of milliseconds to wait on each connection attempt | ||
* (defaults to 0) */ | ||
timeout?: number; | ||
/** Whether to wait for DNS to resolve, defaults to false */ | ||
waitForDns?: boolean; | ||
/** Output mode */ | ||
output?: 'dots' | 'silent'; | ||
} | ||
const waitPort: (server: ServerLocation, timeout?: number) => Promise<boolean>; | ||
declare const waitPort: (server: ServerLocation, timeout?: number) => Promise<boolean>; | ||
export default waitPort; |
@@ -0,3 +1,5 @@ | ||
const TargetError = require('./errors/target-error'); | ||
function extractTarget(target) { | ||
if (!target) throw new Error('\'target\' is required'); | ||
if (!target) throw new TargetError('\'target\' is required'); | ||
@@ -17,3 +19,3 @@ // First, check to see if we have a protocol specified. | ||
const split = target.split(':'); | ||
if (split.length > 2) throw new Error('\'target\' is invalid'); | ||
if (split.length > 2) throw new TargetError(`'${target}' is an invalid target, it has more than two ':' symbols`); | ||
@@ -25,3 +27,3 @@ // Grab the host and port (which will still be a string). | ||
// Make sure the port is numeric. | ||
if (!/^[0-9]+$/.test(portString)) throw new Error('\'port\' must be a number'); | ||
if (!/^[0-9]+$/.test(portString)) throw new TargetError(`'${target}' is an invalid target, '${portString}' is not a valid port number - try something like 'host:port'`); | ||
const port = parseInt(portString, 10); | ||
@@ -28,0 +30,0 @@ |
{ | ||
"name": "wait-port", | ||
"version": "0.2.4", | ||
"version": "0.2.6", | ||
"description": "Utility to wait for a TCP port to open.", | ||
@@ -34,5 +34,7 @@ "main": "./lib/wait-port.js", | ||
"devDependencies": { | ||
"codecov": "^3.0.0", | ||
"eslint": "^6.0.0", | ||
"mocha": "^6.0.0", | ||
"codecov": "^3.6.1", | ||
"eslint": "^6.5.1", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-node": "^10.0.0", | ||
"mocha": "^6.2.1", | ||
"mocha-junit-reporter": "^1.23.1", | ||
@@ -44,6 +46,6 @@ "nyc": "^14.1.1", | ||
"dependencies": { | ||
"chalk": "^2.3.0", | ||
"commander": "^3.0.0", | ||
"debug": "^4.1.0" | ||
"chalk": "^2.4.2", | ||
"commander": "^3.0.2", | ||
"debug": "^4.1.1" | ||
} | ||
} |
@@ -88,2 +88,3 @@ # wait-port | ||
| `3` | The address cannot be found (e.g. no DNS entry, or unresolvable). | | ||
| `4` | The target (host and port) is invalid. | | ||
@@ -90,0 +91,0 @@ # API |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
190
1
184085
9
20
423
Updatedchalk@^2.4.2
Updatedcommander@^3.0.2
Updateddebug@^4.1.1