-
-
Notifications
You must be signed in to change notification settings - Fork 64
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 8, refactor TypeScript definition to CommonJS compati…
…ble export (#29)
- Loading branch information
1 parent
5ed7b68
commit 1ec2a88
Showing
6 changed files
with
86 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,65 @@ | ||
export interface Options { | ||
/** | ||
* A preferred port or an iterable of preferred ports to use. | ||
*/ | ||
readonly port?: number | Iterable<number>, | ||
/// <reference types="node"/> | ||
import {Omit} from 'type-fest'; | ||
import {ListenOptions} from 'net'; | ||
|
||
/** | ||
* The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address. | ||
*/ | ||
readonly host?: string; | ||
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; | ||
} | ||
} | ||
|
||
declare const getPort: { | ||
/** | ||
* Get an available TCP port number. | ||
* | ||
* @returns Port number. | ||
*/ | ||
(options?: Options): Promise<number>; | ||
|
||
/** | ||
* Make a range of ports `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. | ||
*/ | ||
makeRange(from: number, to: number): Iterable<number>; | ||
} | ||
/** | ||
Get an available TCP port number. | ||
@returns Port number. | ||
@example | ||
``` | ||
import getPort = require('get-port'); | ||
(async () => { | ||
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 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>; | ||
|
||
/** | ||
Make a range of ports `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. | ||
@example | ||
``` | ||
import getPort = require('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 default getPort; | ||
export = getPort; |
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-check'; | ||
import getPort from '.'; | ||
|
||
expectType<number>(await getPort()); | ||
expectType<number>(await getPort({port: 3000})); | ||
expectType<number>(await getPort({port: [3000, 3001, 3002]})); | ||
expectType<number>(await getPort({host: 'https://localhost'})); | ||
import {expectType} from 'tsd'; | ||
import getPort = require('.'); | ||
|
||
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)); |
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