Comparing version 5.1.1 to 6.0.0
@@ -1,64 +0,55 @@ | ||
/// <reference types="node"/> | ||
import {ListenOptions} from 'net'; | ||
import {ListenOptions} from 'node:net'; | ||
declare namespace getPort { | ||
interface Options extends Omit<ListenOptions, 'port'> { | ||
/** | ||
A preferred port or an iterable of preferred ports to use. | ||
*/ | ||
readonly port?: number | Iterable<number>; | ||
export interface Options extends Omit<ListenOptions, 'port'> { | ||
/** | ||
A preferred port or an iterable of preferred ports to use. | ||
*/ | ||
readonly port?: number | Iterable<number>; | ||
/** | ||
The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address. | ||
*/ | ||
readonly host?: string; | ||
} | ||
/** | ||
The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address. | ||
By default, it checks availability on all local addresses defined in [OS network interfaces](https://nodejs.org/api/os.html#os_os_networkinterfaces). If this option is set, it will only check the given host. | ||
*/ | ||
readonly host?: string; | ||
} | ||
declare const getPort: { | ||
/** | ||
Get an available TCP port number. | ||
/** | ||
Get an available TCP port number. | ||
@returns Port number. | ||
@returns Port number. | ||
@example | ||
``` | ||
import getPort = require('get-port'); | ||
@example | ||
``` | ||
import getPort from 'get-port'; | ||
(async () => { | ||
console.log(await getPort()); | ||
//=> 51402 | ||
console.log(await getPort()); | ||
//=> 51402 | ||
// Pass in a preferred port | ||
console.log(await getPort({port: 3000})); | ||
// Will use 3000 if available, otherwise fall back to a random port | ||
// Pass in a preferred port | ||
console.log(await getPort({port: 3000})); | ||
// Will use 3000 if available, otherwise fall back to a random port | ||
// Pass in an array of preferred ports | ||
console.log(await getPort({port: [3000, 3001, 3002]})); | ||
// Will use any element in the preferred ports array if available, otherwise fall back to a random port | ||
})(); | ||
``` | ||
*/ | ||
(options?: getPort.Options): Promise<number>; | ||
// Pass in an array of preferred ports | ||
console.log(await getPort({port: [3000, 3001, 3002]})); | ||
// Will use any element in the preferred ports array if available, otherwise fall back to a random port | ||
``` | ||
*/ | ||
export default function getPort(options?: Options): Promise<number>; | ||
/** | ||
Make a range of ports `from`...`to`. | ||
/** | ||
Generate port numbers in the given range `from`...`to`. | ||
@param from - First port of the range. Must be in the range `1024`...`65535`. | ||
@param to - Last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`. | ||
@returns The ports in the range. | ||
@param from - The first port of the range. Must be in the range `1024`...`65535`. | ||
@param to - The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`. | ||
@returns The port numbers in the range. | ||
@example | ||
``` | ||
import getPort = require('get-port'); | ||
@example | ||
``` | ||
import getPort, {portNumbers} from 'get-port'; | ||
(async () => { | ||
console.log(await getPort({port: getPort.makeRange(3000, 3100)})); | ||
// Will use any port from 3000 to 3100, otherwise fall back to a random port | ||
})(); | ||
``` | ||
*/ | ||
makeRange(from: number, to: number): Iterable<number>; | ||
}; | ||
export = getPort; | ||
console.log(await getPort({port: portNumbers(3000, 3100)})); | ||
// Will use any port from 3000 to 3100, otherwise fall back to a random port | ||
``` | ||
*/ | ||
export function portNumbers(from: number, to: number): Iterable<number>; |
79
index.js
@@ -1,3 +0,3 @@ | ||
'use strict'; | ||
const net = require('net'); | ||
import net from 'node:net'; | ||
import os from 'node:os'; | ||
@@ -12,3 +12,3 @@ class Locked extends Error { | ||
old: new Set(), | ||
young: new Set() | ||
young: new Set(), | ||
}; | ||
@@ -24,14 +24,50 @@ | ||
const getAvailablePort = options => new Promise((resolve, reject) => { | ||
const server = net.createServer(); | ||
server.unref(); | ||
server.on('error', reject); | ||
server.listen(options, () => { | ||
const {port} = server.address(); | ||
server.close(() => { | ||
resolve(port); | ||
const getLocalHosts = () => { | ||
const interfaces = os.networkInterfaces(); | ||
// Add undefined value for createServer function to use default host, | ||
// and default IPv4 host in case createServer defaults to IPv6. | ||
const results = new Set([undefined, '0.0.0.0']); | ||
for (const _interface of Object.values(interfaces)) { | ||
for (const config of _interface) { | ||
results.add(config.address); | ||
} | ||
} | ||
return results; | ||
}; | ||
const checkAvailablePort = options => | ||
new Promise((resolve, reject) => { | ||
const server = net.createServer(); | ||
server.unref(); | ||
server.on('error', reject); | ||
server.listen(options, () => { | ||
const {port} = server.address(); | ||
server.close(() => { | ||
resolve(port); | ||
}); | ||
}); | ||
}); | ||
}); | ||
const getAvailablePort = async (options, hosts) => { | ||
if (options.host || options.port === 0) { | ||
return checkAvailablePort(options); | ||
} | ||
for (const host of hosts) { | ||
try { | ||
await checkAvailablePort({port: options.port, host}); // eslint-disable-line no-await-in-loop | ||
} catch (error) { | ||
if (!['EADDRNOTAVAIL', 'EINVAL'].includes(error.code)) { | ||
throw error; | ||
} | ||
} | ||
} | ||
return options.port; | ||
}; | ||
const portCheckSequence = function * (ports) { | ||
@@ -45,3 +81,3 @@ if (ports) { | ||
module.exports = async options => { | ||
export default async function getPorts(options) { | ||
let ports; | ||
@@ -65,5 +101,7 @@ | ||
const hosts = getLocalHosts(); | ||
for (const port of portCheckSequence(ports)) { | ||
try { | ||
let availablePort = await getAvailablePort({...options, port}); // eslint-disable-line no-await-in-loop | ||
let availablePort = await getAvailablePort({...options, port}, hosts); // eslint-disable-line no-await-in-loop | ||
while (lockedPorts.old.has(availablePort) || lockedPorts.young.has(availablePort)) { | ||
@@ -74,6 +112,7 @@ if (port !== 0) { | ||
availablePort = await getAvailablePort({...options, port}); // eslint-disable-line no-await-in-loop | ||
availablePort = await getAvailablePort({...options, port}, hosts); // eslint-disable-line no-await-in-loop | ||
} | ||
lockedPorts.young.add(availablePort); | ||
return availablePort; | ||
@@ -88,5 +127,5 @@ } catch (error) { | ||
throw new Error('No available ports found'); | ||
}; | ||
} | ||
module.exports.makeRange = (from, to) => { | ||
export function portNumbers(from, to) { | ||
if (!Number.isInteger(from) || !Number.isInteger(to)) { | ||
@@ -96,7 +135,7 @@ throw new TypeError('`from` and `to` must be integer numbers'); | ||
if (from < 1024 || from > 65535) { | ||
if (from < 1024 || from > 65_535) { | ||
throw new RangeError('`from` must be between 1024 and 65535'); | ||
} | ||
if (to < 1024 || to > 65536) { | ||
if (to < 1024 || to > 65_536) { | ||
throw new RangeError('`to` must be between 1024 and 65536'); | ||
@@ -116,2 +155,2 @@ } | ||
return generator(from, to); | ||
}; | ||
} |
{ | ||
"name": "get-port", | ||
"version": "5.1.1", | ||
"version": "6.0.0", | ||
"description": "Get an available port", | ||
@@ -11,6 +11,8 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -42,7 +44,8 @@ "scripts": { | ||
"devDependencies": { | ||
"@types/node": "^12.12.21", | ||
"ava": "^2.4.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.25.3" | ||
"@types/node": "^16.10.2", | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"typescript": "^4.4.3", | ||
"xo": "^0.45.0" | ||
} | ||
} |
@@ -1,10 +0,10 @@ | ||
# get-port [![Build Status](https://travis-ci.org/sindresorhus/get-port.svg?branch=master)](https://travis-ci.org/sindresorhus/get-port) | ||
# get-port | ||
> Get an available [TCP port](https://en.wikipedia.org/wiki/Port_(computer_networking)) | ||
> Get an available [TCP port](https://en.wikipedia.org/wiki/Port_(computer_networking)). | ||
## Install | ||
```sh | ||
npm install get-port | ||
``` | ||
$ npm install get-port | ||
``` | ||
@@ -14,8 +14,6 @@ ## Usage | ||
```js | ||
const getPort = require('get-port'); | ||
import getPort from 'get-port'; | ||
(async () => { | ||
console.log(await getPort()); | ||
//=> 51402 | ||
})(); | ||
console.log(await getPort()); | ||
//=> 51402 | ||
``` | ||
@@ -26,6 +24,6 @@ | ||
```js | ||
(async () => { | ||
console.log(await getPort({port: 3000})); | ||
// Will use 3000 if available, otherwise fall back to a random port | ||
})(); | ||
import getPort from 'get-port'; | ||
console.log(await getPort({port: 3000})); | ||
// Will use 3000 if available, otherwise fall back to a random port | ||
``` | ||
@@ -36,15 +34,15 @@ | ||
```js | ||
(async () => { | ||
console.log(await getPort({port: [3000, 3001, 3002]})); | ||
// Will use any element in the preferred ports array if available, otherwise fall back to a random port | ||
})(); | ||
import getPort from 'get-port'; | ||
console.log(await getPort({port: [3000, 3001, 3002]})); | ||
// Will use any element in the preferred ports array if available, otherwise fall back to a random port | ||
``` | ||
Use the `makeRange()` helper in case you need a port in a certain range: | ||
Use the `portNumbers()` helper in case you need a port in a certain range: | ||
```js | ||
(async () => { | ||
console.log(await getPort({port: getPort.makeRange(3000, 3100)})); | ||
// Will use any port from 3000 to 3100, otherwise fall back to a random port | ||
})(); | ||
import getPort, {portNumbers} from 'get-port'; | ||
console.log(await getPort({port: portNumbers(3000, 3100)})); | ||
// Will use any port from 3000 to 3100, otherwise fall back to a random port | ||
``` | ||
@@ -74,8 +72,10 @@ | ||
### getPort.makeRange(from, to) | ||
By default, it checks availability on all local addresses defined in [OS network interfaces](https://nodejs.org/api/os.html#os_os_networkinterfaces). If this option is set, it will only check the given host. | ||
Make a range of ports `from`...`to`. | ||
### portNumbers(from, to) | ||
Returns an `Iterable` for ports in the given range. | ||
Generate port numbers in the given range `from`...`to`. | ||
Returns an `Iterable` for port numbers in the given range. | ||
#### from | ||
@@ -85,3 +85,3 @@ | ||
First port of the range. Must be in the range `1024`...`65535`. | ||
The first port of the range. Must be in the range `1024`...`65535`. | ||
@@ -92,3 +92,3 @@ #### to | ||
Last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`. | ||
The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`. | ||
@@ -95,0 +95,0 @@ ## Beware |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
9982
159
0
Yes
5