What is wait-port?
The wait-port npm package is a utility that allows you to wait for a port on a host to become open. This is particularly useful in scenarios where you need to ensure that a service is up and running before proceeding with the next steps in your application or script. It can be used programmatically within your Node.js applications or as a command-line tool.
What are wait-port's main functionalities?
Waiting for a port programmatically
This feature allows you to wait for a specific port on a host to become open by using the wait-port package programmatically within your Node.js application. The `waitPort` function returns a promise that resolves to a boolean indicating whether the port opened before the timeout.
const waitPort = require('wait-port');
const params = {
host: 'localhost',
port: 3000
};
waitPort(params).then((open) => {
if(open) console.log('The port is now open!');
else console.log('The port did not open before the timeout.');
});
Using wait-port as a CLI tool
wait-port can also be used as a command-line tool. This example demonstrates how to wait for port 3000 on localhost to become open by using wait-port in the terminal. It's useful for scripting and automation tasks outside of Node.js applications.
wait-port --host localhost --port 3000
Other packages similar to wait-port
wait-on
wait-on is a similar package that provides more general functionality for waiting on resources to become available. It supports waiting on file system paths, HTTP(S) resources, and TCP ports. Compared to wait-port, wait-on offers a broader range of features but with a similar core functionality of waiting for ports.
tcp-port-used
tcp-port-used is a package that allows you to check if a TCP port is already in use. It can be used to wait for a port to become free or to check if a port is currently occupied. While it offers similar functionality for checking port status, it doesn't provide a built-in mechanism to wait for a port to open, making it slightly less convenient for scenarios where waiting is required.
wait-port
![codecov](https://codecov.io/gh/dwmkerr/wait-port/branch/master/graph/badge.svg)
Simple binary to wait for a port to open. Useful for docker-compose and general server side activities.
Usage
To wait for a port to open, just use:
$ wait-port localhost:3000
To wait for a port to open, but limit to a certain timeout, use:
$ wait-port -t 10000 localhost:3000
Parameters
The following parameters are accepted:
Parameter | Usage |
---|
<target> | Required. The target to test for. Can be just a port, a colon and port (as one would use with httpie or host and port. Examples: 8080 , :3000 , 127.0.0.1:443 . |
--timeout, -t | Optional. Timeout (in milliseconds). |
Error Codes
The following error codes are returned:
Code | Meaning |
---|
0 | The specified port on the host is accepting connections. |
1 | A timeout occured waiting for the port to open. |
2 | Un unknown error occured waiting for the port to open. The program cannot establish whether the port is open or not. |
API
You can use wait-port
programmatically:
const waitPort = require('wait-port');
const params = {
host: 'google.com',
port: 443,
};
waitPort(params)
.then((open) => {
if (open) console.log('The port is now open!');
else console.log('The port did not open before the timeout...');
})
.catch((err) => {
console.err(`An unknown error occured while waiting for the port: ${err}`);
});
The CLI is a very shallow wrapper around this function. The params
object takes the following parameters:
CLI Parameter | API Parameter | Notes |
---|
<target> | host | Optional. Defaults to localhost . |
<target> | port | Required. Port to wait for. |
--timeout, -t | timeout | Optional. Defaults to 0 . Timeout (in milliseconds). If 0 , then the operation will never timeout. |
Developer Guide
This module uses:
| Name | Usage |
| commander.js
| Utility for building commandline apps. |
| debug
| Utility for debug output. |
R mocha
/ nyc
| Test runner / coverage. |
Debugging
This module use debug
for debug output. Set DEBUG=wait-port
to see detailed diagnostic information:
DEBUG=wait-port wait-for -t 10000 localhost:6234
This will also work for any code which uses the API.
Testing
Run unit tests with npm test
. Coverage is reported to artifacts/coverage
.
Debug unit tests with npm run debug
. Add a debugger
statement to the line you are interested in, and consider limiting scope with .only
.
Testing the CLI
Don't install the package to test the CLI. Instead, in the project folder run npm link
. Now go to whatever folder you want to use the module in and run npm link wait-port
. It will symlink the package and binary. See npm link
for more details.
Releasing
Kick out a new release with:
npm version patch
git push --tags
npm publish
Timeouts
The timeout option for waitPort
is used terminate attempts to open the socket after a certain amount of time has passed. Please note that operations can take significantly longer than the timeout. For example:
const promise = waitPort({ port: 9000, interval: 10000 }, 2000);
In this case, the socket will only attempt to connect every ten seconds. So on the first iteration, the timeout is not reached, then another iteration will be scheduled for after ten seconds, meaning the timeout will happen eight seconds later than one might expect.
The waitPort
promise may take up to interval
milliseconds greater than timeout
to resolve.
TODO
Some more tasks to complete: