Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 12.20 and move to ESM
- Loading branch information
1 parent
c3bbed9
commit c08a4ae
Showing
8 changed files
with
128 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,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>; | ||
|
||
/** | ||
The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address. | ||
*/ | ||
readonly host?: string; | ||
} | ||
} | ||
export interface Options extends Omit<ListenOptions, 'port'> { | ||
/** | ||
A preferred port or an iterable of preferred ports to use. | ||
*/ | ||
readonly port?: number | Iterable<number>; | ||
|
||
declare const getPort: { | ||
/** | ||
Get an available TCP port number. | ||
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; | ||
} | ||
|
||
@returns Port number. | ||
/** | ||
Get an available TCP port number. | ||
@example | ||
``` | ||
import getPort = require('get-port'); | ||
@returns Port number. | ||
(async () => { | ||
console.log(await getPort()); | ||
//=> 51402 | ||
@example | ||
``` | ||
import getPort from 'get-port'; | ||
// Pass in a preferred port | ||
console.log(await getPort({port: 3000})); | ||
// Will use 3000 if available, otherwise fall back to a random port | ||
console.log(await getPort()); | ||
//=> 51402 | ||
// 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 a preferred port | ||
console.log(await getPort({port: 3000})); | ||
// Will use 3000 if available, otherwise fall back to a random port | ||
/** | ||
Make a range of ports `from`...`to`. | ||
// 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>; | ||
|
||
@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. | ||
/** | ||
Generate port numbers in the given range `from`...`to`. | ||
@example | ||
``` | ||
import getPort = require('get-port'); | ||
@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. | ||
(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>; | ||
}; | ||
@example | ||
``` | ||
import getPort, {portNumbers} from 'get-port'; | ||
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import {expectType} from 'tsd'; | ||
import getPort = require('.'); | ||
import getPort, {portNumbers} from './index.js'; | ||
|
||
expectType<Promise<number>>(getPort()); | ||
expectType<Promise<number>>(getPort({port: 3000})); | ||
expectType<Promise<number>>(getPort({port: [3000, 3001, 3002]})); | ||
expectType<Promise<number>>(getPort({host: 'https://localhost'})); | ||
expectType<Promise<number>>(getPort({ipv6Only: true})); | ||
expectType<Iterable<number>>(getPort.makeRange(1024, 1025)); | ||
expectType<Iterable<number>>(portNumbers(1024, 1025)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.