Language : 🇺🇸 English | 🇨🇳 简体中文

port-selector

NPM Version NPM Downloads Bundle Size

GitHub Actions CI License

Overview

port-selector is a cross-platform NodeJS library implemented in Rust. It mainly provides port availability checking and filtering ports based on filter conditions.

Install

npm install port-selector
# or
yarn add port-selector
# or
pnpm i port-selector

import {
    Selector,
    isFree,
    isFreeTcp,
    isFreeUdp,
    randomFreePort,
    randomFreeTcpPort,
    randomFreeUdpPort,
    selectFreePort,
    selectFromGivenPort
} from 'port-selector'

Goods

isFree · isFreeTcp · isFreeUdp · randomFreePort · randomFreeTcpPort · randomFreeUdpPort · selectFromGivenPort · selectFreePort

Documentation

isFree

Check whether the port is not used on TCP and UDP

function isFree(port: number): boolean

isFreeTcp

Check whether the port is not used on TCP

function isFreeTcp(port: number): boolean

isFreeUdp

Check whether the port is not used on UDP

function isFreeUdp(port: number): boolean

randomFreePort

The system randomly assigns available TCP and UDP ports

function randomFreePort(): number

randomFreeTcpPort

The system randomly assigns available TCP ports

function randomFreeTcpPort(): number

randomFreeUdpPort

The system randomly assigns available UDP ports

function randomFreeUdpPort(): number

selectFromGivenPort

Check 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): number

selectFreePort

Gets a matching port based on the Selector parameter constraint

function selectFreePort(selector?: Selector): number

export 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
}

Thanks

napi-rs

GitHub

View Github