Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 2, 2019
1 parent b733aa2 commit 0e5745a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions index.d.ts
@@ -1,17 +1,17 @@
export type Options = Readonly<{
export interface Options {
/**
* A preferred port or an array of preferred ports to use.
*/
port?: number | ReadonlyArray<number>,
readonly port?: number | ReadonlyArray<number>,

/**
* The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
*/
host?: string;
}>;
readonly host?: string;
}

/**
* Get an available port number.
* Get an available TCP port number.
*
* @returns Port number.
*/
Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -31,3 +31,5 @@ const getPort = options => {
module.exports = options => options ?
getPort(options).catch(() => getPort(Object.assign(options, {port: 0}))) :
getPort({port: 0});

module.exports.default = module.exports;
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -37,9 +37,9 @@
"chosen"
],
"devDependencies": {
"ava": "*",
"ava": "^1.2.1",
"pify": "^3.0.0",
"tsd-check": "^0.3.0",
"xo": "*"
"xo": "^0.24.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -51,7 +51,7 @@ Type: `Object`

##### port

Type: `number` `number[]`
Type: `number | number[]`

A preferred port or an array of preferred ports to use.

Expand Down
20 changes: 9 additions & 11 deletions test.js
@@ -1,10 +1,10 @@
import net from 'net';
import test from 'ava';
import pify from 'pify';
import m from '.';
import getPort from '.';

test('port can be bound when promise resolves', async t => {
const port = await m();
const port = await getPort();
t.is(typeof port, 'number');
t.true(port > 0);

Expand All @@ -16,26 +16,24 @@ test('port can be bound when promise resolves', async t => {

test('preferred port', async t => {
const desiredPort = 8080;
const port = await m({port: desiredPort});
const port = await getPort({port: desiredPort});

t.is(port, desiredPort);
});

test('preferred port unavailable', async t => {
t.plan(3);

const desiredPort = 8282;
const server = net.createServer();
await pify(server.listen.bind(server))(desiredPort);

const port = await m({port: desiredPort});
const port = await getPort({port: desiredPort});
t.is(typeof port, 'number');
t.true(port > 0);
t.not(port, desiredPort);
});

test('port can be bound to IPv4 host when promise resolves', async t => {
const port = await m({host: '0.0.0.0'});
const port = await getPort({host: '0.0.0.0'});
t.is(typeof port, 'number');
t.true(port > 0);

Expand All @@ -47,7 +45,7 @@ test('port can be bound to IPv4 host when promise resolves', async t => {

test('preferred port given IPv4 host', async t => {
const desiredPort = 8080;
const port = await m({
const port = await getPort({
port: desiredPort,
host: '0.0.0.0'
});
Expand All @@ -57,7 +55,7 @@ test('preferred port given IPv4 host', async t => {

test('preferred ports', async t => {
const desiredPorts = [9910, 9912, 9913];
const port = await m({
const port = await getPort({
port: desiredPorts,
host: '0.0.0.0'
});
Expand All @@ -71,7 +69,7 @@ test('first port in preferred ports array is unavailable', async t => {
const server = net.createServer();
await pify(server.listen.bind(server))(desiredPorts[0]);

const port = await m({
const port = await getPort({
port: desiredPorts
});

Expand All @@ -86,7 +84,7 @@ test('all preferred ports in array are unavailable', async t => {
const server2 = net.createServer();
await pify(server2.listen.bind(server2))(desiredPorts[1]);

const port = await m({
const port = await getPort({
port: desiredPorts
});

Expand Down

0 comments on commit 0e5745a

Please sign in to comment.