@go22/port0
v1.0.0
Published
Check whether a TCP port is in use and find a random available port.
Downloads
115
Readme
@go22/port0
Check whether a TCP port is in use and find a random available port. Requires Node.js 18 or newer.
Install
npm install @go22/port0Usage
import { genPort, isPortInUse } from '@go22/port0';
await isPortInUse(3000); // true or false
await isPortInUse('3000'); // true or false
await genPort(); // random available port from 1024 through 65535
await genPort(3000); // random available port from 3000 through 65535
await genPort(3000, 4000); // random available port from 3000 through 4000Both range boundaries are inclusive. genPort checks each candidate no more than once and rejects with a RangeError if every port in the range is in use.
Availability is inherently a point-in-time check. Another process can claim a returned port before your application binds to it.
Because of this race condition, the intended use is to bind or connect with the randomly generated port immediately, minimizing the risk of a collision.
License
MIT
