Language : 🇺🇸 English | 🇨🇳 简体中文
port-selector is a cross-platform NodeJS library implemented in Rust. It mainly provides port availability checking and filtering ports based on filter conditions.
- Node.js (>= 10.0.0 required, LTS preferred)
npm install port-selector
# or
yarn add port-selector
# or
pnpm i port-selectorimport {
Selector,
isFree,
isFreeTcp,
isFreeUdp,
randomFreePort,
randomFreeTcpPort,
randomFreeUdpPort,
selectFreePort,
selectFromGivenPort
} from 'port-selector'isFree · isFreeTcp · isFreeUdp · randomFreePort · randomFreeTcpPort · randomFreeUdpPort · selectFromGivenPort · selectFreePort
Check whether the port is not used on TCP and UDP
function isFree(port: number): booleanCheck whether the port is not used on TCP
function isFreeTcp(port: number): booleanCheck whether the port is not used on UDP
function isFreeUdp(port: number): booleanThe system randomly assigns available TCP and UDP ports
function randomFreePort(): numberThe system randomly assigns available TCP ports
function randomFreeTcpPort(): numberThe system randomly assigns available UDP ports
function randomFreeUdpPort(): numberCheck from starterPort and return the first available port
Return if starterPort is available; Otherwise starterPort += 1 until the port is available
function selectFromGivenPort(starterPort: number): numberGets a matching port based on the Selector parameter constraint
function selectFreePort(selector?: Selector): numberexport type Selector = {
// Check whether the port is available on TCP.
// The default value is true.
checkTcp?: boolean
// Check whether the port is available on UDP.
// The default value is true.
checkUdp?: boolean
// Set the generated port range, starting value
// The default value is 0.
portFrom?: number
// Set the generated port range, end value
// The default value is 65535.
portTo?: number
// Maximum number of random times. Default value: 100
// If no available port number is found within the maximum random number of loops, None is returned
maxRandomTimes?: number
}