https://github.com/zingerlittlebee/port-selector
select port what you want for NodeJS power by Rust
https://github.com/zingerlittlebee/port-selector
nodejs nodejs-library port rust
Last synced: 3 months ago
JSON representation
select port what you want for NodeJS power by Rust
- Host: GitHub
- URL: https://github.com/zingerlittlebee/port-selector
- Owner: ZingerLittleBee
- License: mit
- Created: 2022-04-28T02:24:02.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-08T17:08:43.000Z (over 2 years ago)
- Last Synced: 2025-03-14T16:04:49.518Z (3 months ago)
- Topics: nodejs, nodejs-library, port, rust
- Language: JavaScript
- Homepage: https://npmjs.com/package/port-selector
- Size: 536 KB
- Stars: 12
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.config.js
- License: LICENSE
Awesome Lists containing this project
README
Language : 🇺🇸 English | [🇨🇳 简体中文](https://github.com/ZingerLittleBee/port-selector/blob/main/README.zh-CN.md)
port-selector
[](https://www.npmjs.com/package/port-selector)
[](https://www.npmjs.com/package/port-selector)
[](https://github.com/ZingerLittleBee/port-selector/actions)
## 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.
## Getting Started
### Prerequisites
- [Node.js](https://nodejs.org) (>= 10.0.0 required, LTS preferred)
### Installation
```bash
npm install port-selector
# or
yarn add port-selector
# or
pnpm i port-selector
``````ts
import {
Selector,
isFree,
isFreeTcp,
isFreeUdp,
randomFreePort,
randomFreeTcpPort,
randomFreeUdpPort,
selectFreePort,
selectFromGivenPort
} from 'port-selector'
```## Goods
[isFree](#isfree) · [isFreeTcp](#isfreetcp) · [isFreeUdp](#isfreeudp) · [randomFreePort](#randomfreeport) · [randomFreeTcpPort](#randomfreetcpport) · [randomFreeUdpPort](#randomfreeudpport) · [selectFromGivenPort](#selectfromgivenport) · [selectFreePort](#selectfreeport)## Documentation
### `isFree`
Check whether the port is not used on TCP and UDP
```ts
function isFree(port: number): boolean
```### `isFreeTcp`
Check whether the port is not used on TCP
```ts
function isFreeTcp(port: number): boolean
```### `isFreeUdp`
Check whether the port is not used on UDP
```ts
function isFreeUdp(port: number): boolean
```### `randomFreePort`
The system randomly assigns available TCP and UDP ports
```ts
function randomFreePort(): number
```### `randomFreeTcpPort`
The system randomly assigns available TCP ports
```ts
function randomFreeTcpPort(): number
```### `randomFreeUdpPort`
The system randomly assigns available UDP ports
```ts
function randomFreeUdpPort(): number
```### `selectFromGivenPort`
Check from `starterPort` and return the first available portReturn if `starterPort` is available; Otherwise `starterPort += 1` until the port is available
```ts
function selectFromGivenPort(starterPort: number): number
```### `selectFreePort`
Gets a matching port based on the `Selector` parameter constraint
```ts
function selectFreePort(selector?: Selector): number
``````ts
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](https://github.com/napi-rs/napi-rs)