Socket
Socket
Sign inDemoInstall

pick-port

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pick-port - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

lib/index.d.ts

29

lib/tcp.js

@@ -1,14 +0,15 @@

const net = require('net');
module.exports = async function({ ip, port })
{
const server = net.createServer();
return new Promise((resolve, reject) =>
{
server.unref();
server.on('error', reject);
server.listen({ port, exclusive: true }, ip, () => server.close(resolve));
});
};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reserve = void 0;
const net = require("node:net");
async function reserve(ip, port) {
const server = net.createServer();
await new Promise((resolve, reject) => {
server.unref();
server.on('error', reject);
server.listen({ host: ip, port, exclusive: true }, () => {
server.close(() => resolve());
});
});
}
exports.reserve = reserve;

@@ -1,14 +0,15 @@

const dgram = require('dgram');
module.exports = async function({ ip, port, family })
{
const server = dgram.createSocket(family === 4 ? 'udp4' : 'udp6');
return new Promise((resolve, reject) =>
{
server.unref();
server.on('error', reject);
server.bind({ port, exclusive: true }, ip, () => server.close(resolve));
});
};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reserve = void 0;
const dgram = require("node:dgram");
async function reserve(ip, port, family) {
const server = dgram.createSocket(family === 4 ? 'udp4' : 'udp6');
await new Promise((resolve, reject) => {
server.unref();
server.on('error', reject);
server.bind({ address: ip, port, exclusive: true }, () => {
server.close(() => resolve());
});
});
}
exports.reserve = reserve;
{
"name": "pick-port",
"version": "1.0.1",
"description": "Get a free TCP or UDP port for the given IP address",
"author": "José Luis Millán <jmillan@aliax.net> (https://github.com/jmillan)",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/jmillan/pick-port.git"
},
"bugs": {
"url": "https://github.com/jmillan/pick-port/issues"
},
"main": "index.js",
"keywords": [
"network",
"port",
"tcp",
"udp",
"pick",
"allocate",
"free"
],
"devDependencies": {
"eslint": "^7.17.0"
},
"scripts": {
"lint": "node npm-scripts.js lint",
"release": "node npm-scripts.js release"
},
"dependencies": {
"debug": "^4.3.1"
}
"name": "pick-port",
"version": "2.0.0",
"description": "Get a free TCP or UDP port for the given IP address",
"author": "José Luis Millán <jmillan@aliax.net> (https://github.com/jmillan)",
"contributors": [
"Iñaki Baz Castillo <ibc@aliax.net> (https://inakibaz.me)"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/versatica/pick-port.git"
},
"bugs": {
"url": "https://github.com/versatica/pick-port/issues"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"npm-scripts.mjs",
"lib"
],
"engines": {
"node": ">=16"
},
"keywords": [
"nodejs",
"network",
"port",
"tcp",
"udp"
],
"scripts": {
"prepare": "node npm-scripts.mjs prepare",
"typescript:build": "node npm-scripts.mjs typescript:build",
"typescript:watch": "node npm-scripts.mjs typescript:watch",
"lint": "node npm-scripts.mjs lint",
"format": "node npm-scripts.mjs format",
"test": "node npm-scripts.mjs test",
"release:check": "node npm-scripts.mjs release:check",
"release": "node npm-scripts.mjs release"
},
"jest": {
"verbose": true,
"testEnvironment": "node",
"testRegex": "src/test/test.*\\.ts",
"transform": {
"^.*\\.ts$": [
"ts-jest",
{
"diagnostics": {
"ignoreCodes": [
"TS151001"
]
}
}
]
},
"coveragePathIgnorePatterns": [
"src/test"
],
"cacheDirectory": ".cache/jest"
},
"dependencies": {
"debug": "^4.3.4"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.8",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^27.6.1",
"eslint-plugin-prettier": "^5.1.2",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"prettier": "^3.1.1",
"ts-jest": "^29.1.1",
"typescript": "^5.3.3"
}
}
# pick-port
[![][npm-shield-pick-port]][npm-pick-port]
[![][github-actions-shield-pick-port]][github-actions-pick-port]
Get an available TCP or UDP port for the given IP address.

@@ -9,7 +12,6 @@

## Usage
```js
const pickPort = require('pick-port');
```ts
import { pickPort } from 'pick-port';
```

@@ -19,4 +21,4 @@

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

@@ -26,34 +28,41 @@

```js
const port = await =
pickPort({ type: 'tcp', ip: '192.168.10.111', minPort: 8000, maxPort: 9000 })
```ts
const port = await pickPort({
type: 'tcp',
ip: '192.168.10.111',
minPort: 8000,
maxPort: 9000,
});
```
## API
### await pickPort({ type, ip, minPort, maxPort, reserveTimeout })
### async pickPort({ type, ip, minPort, maxPort, reserveTimeout }): Promise<number>
Async function. Resolves with an available port or rejects with an error otherwise.
Resolves with an available port or rejects with an error otherwise.
| Option | Type | Description | Required | Default |
| ------------- | ------ | ------------- | :---: | ------- |
| `type` | String | 'udp' or 'tcp'. | No | 'udp' |
| `ip` | String | IPv4 or IPv6 address for which a free port is requested. | No | '0.0.0.0' |
| `minPort` | Number | Minimum port. | No | 10000 |
| `maxPort` | Number | Maximum port. | No | 20000 |
| `reserveTimeout` | Number | Timeout 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. | No | 5 |
| Option | Type | Description | Required | Default |
| ---------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------: | --------- |
| `type` | String | 'udp' or 'tcp'. | Yes | |
| `ip` | String | IPv4 or IPv6 address for which a free port is requested. | No | '0.0.0.0' |
| `minPort` | Number | Minimum port. | No | 10000 |
| `maxPort` | Number | Maximum port. | No | 20000 |
| `reserveTimeout` | Number | Timeout 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. | No | 5 |
* `@returns` {Number} A free port.
- `@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
## Author
- José Luis Millán [[github](https://github.com/jmillan/)]
- Iñaki Baz Castillo [[website](https://inakibaz.me)|[github](https://github.com/ibc/)]
* José Luis Millán [[github](https://github.com/jmillan/)]
## License
[ISC](./LICENSE)
[npm-shield-pick-port]: https://img.shields.io/npm/v/pick-port.svg
[npm-pick-port]: https://npmjs.org/package/pick-port
[github-actions-shield-pick-port]: https://github.com/versatica/pick-port/actions/workflows/pick-port.yaml/badge.svg
[github-actions-pick-port]: https://github.com/versatica/pick-port/actions/workflows/pick-port.yaml
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc