2.1.0 • Published 5 months ago

pick-port v2.1.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 months ago

pick-port

Get an available TCP or UDP port for the given IP address.

$ npm install pick-port

Usage

import { pickPort } from 'pick-port';

Get a random UDP port in IP 0.0.0.0:

const port = await pickPort({ type: 'udp' });

Get a TCP port in a specific IP and port range:

const port = await pickPort({
	type: 'tcp',
	ip: '192.168.10.111',
	minPort: 8000,
	maxPort: 9000,
});

API

async pickPort({ type, ip, minPort, maxPort, reserveTimeout }): Promise

Resolves with an available port or rejects with an error otherwise.

OptionTypeDescriptionRequiredDefault
typeString'udp' or 'tcp'.Yes
ipStringIPv4 or IPv6 address for which a free port is requested.No'0.0.0.0'
minPortNumberMinimum port.No10000
maxPortNumberMaximum port.No20000
reserveTimeoutNumberTimeout in seconds during which a returned port will be internally reserved and prevented of being returned on a future call before the timeout has elapsed.No5
  • @returns {Number} A free port.

The reserveTimeout option provides the application with the required time to bind the free port before it is given again on a future call to this library.

Authors

License

ISC