Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add makeRange() helper method #28

Merged
merged 27 commits into from Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 18 additions & 6 deletions index.d.ts
Expand Up @@ -10,9 +10,21 @@ export interface Options {
readonly host?: string;
}

/**
* Get an available TCP port number.
*
* @returns Port number.
*/
export default function getPort(options?: Options): Promise<number>;
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 range(inclusive)
* @param to - Last port of range(exclusive)
stroncium marked this conversation as resolved.
Show resolved Hide resolved
* @returns Array with ports in range.
*/
makeRange(from: number, to: number): number[];
}

export default getPort;
25 changes: 25 additions & 0 deletions index.js
Expand Up @@ -32,4 +32,29 @@ module.exports = options => options ?
getPort(options).catch(() => getPort(Object.assign(options, {port: 0}))) :
stroncium marked this conversation as resolved.
Show resolved Hide resolved
getPort({port: 0});

module.exports.makeRange = (from, to) => {
stroncium marked this conversation as resolved.
Show resolved Hide resolved
if (!Number.isInteger(from) || !Number.isInteger(to)) {
throw new TypeError('`from` and `to` must be integer numbers');
}

if (from < 1024 || from > 65535) {
throw new RangeError('`from` must be between 1024 and 65535');
}

if (to < 1024 || to > 65536) {
throw new RangeError('`to` must be between 1024 and 65536');
}

if (from >= to) {
throw new RangeError('`to` must be greater than `from`');
}
stroncium marked this conversation as resolved.
Show resolved Hide resolved

const ports = [];
for (let port = from; port < to; port++) {
ports.push(port);
}

return ports;
};

module.exports.default = module.exports;
2 changes: 2 additions & 0 deletions index.test-d.ts
Expand Up @@ -5,3 +5,5 @@ 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'}));

expectType<number[]>(getPort.makeRange(1024, 1025));
23 changes: 23 additions & 0 deletions readme.md
Expand Up @@ -39,6 +39,15 @@ Pass in an array of preferred ports:
})();
```

Use `makeRange` helper in case you need to select port from a range to generate an array of ports:

```js
(async () => {
console.log(await getPort({port: getPort.makeRange(3000, 3003)}));
stroncium marked this conversation as resolved.
Show resolved Hide resolved
// Will use any port >= 3000, port < 3003, otherwise fall back to a random port
})();
```

## API

### getPort([options])
Expand All @@ -61,6 +70,20 @@ Type: `string`

The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.

### getPort.makeRange(from, to)

#### from

Type: `number`

Port to start range from, inclusive.

#### to

Type: `number`

Port to end range at, exclusive.


## Beware

Expand Down
12 changes: 12 additions & 0 deletions test.js
Expand Up @@ -93,3 +93,15 @@ test('all preferred ports in array are unavailable', async t => {
t.not(port, desiredPorts[0]);
t.not(port, desiredPorts[1]);
});

test('makeRange throws on invalid ranges', t => {
t.throws(() => getPort.makeRange(0, 0));
t.throws(() => getPort.makeRange(1023, 1023));
t.throws(() => getPort.makeRange(65536, 65536));
t.throws(() => getPort.makeRange(1025, 1024));
});

test('makeRange produces valid ranges', t => {
t.deepEqual(getPort.makeRange(1024, 1025), [1024]);
t.deepEqual(getPort.makeRange(1024, 1027), [1024, 1025, 1026]);
});