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

feat: support clearLockedPorts #70

Merged
merged 12 commits into from
Mar 20, 2024
28 changes: 28 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,31 @@ console.log(await getPort({port: portNumbers(3000, 3100)}));
```
*/
export function portNumbers(from: number, to: number): Iterable<number>;

/**
Clear the internal cache of locked ports.

This can be useful when you want the results to be unaffected by previous calls.

Please note that clearing the cache could cause [race conditions](https://github.com/sindresorhus/get-port#beware).

@example
```
import getPort, {clearLockedPorts} from 'get-port';

const port = [3000, 3001, 3002];

console.log(await getPort({port}));
//=> 3000

console.log(await getPort({port}));
//=> 3001

// If you want the results to be unaffected by previous calls, clear the cache.
clearLockedPorts();

console.log(await getPort({port}));
//=> 3000
```
*/
export function clearLockedPorts(): void;
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,8 @@ export function portNumbers(from, to) {

return generator(from, to);
}

export function clearLockedPorts() {
lockedPorts.old.clear();
lockedPorts.young.clear();
}
26 changes: 26 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ Type: `number`

The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.

### clearLockedPorts()

Clear the internal cache of locked ports.
nanianlisao marked this conversation as resolved.
Show resolved Hide resolved

This can be useful when you want the results to be unaffected by previous calls.

Please note that clearing the cache could cause [race conditions](#beware).

```js
import getPort, {clearLockedPorts} from 'get-port';

const port = [3000, 3001, 3002];

console.log(await getPort({port}));
//=> 3000

console.log(await getPort({port}));
//=> 3001

// If you want the results to be unaffected by previous calls, clear the cache.
clearLockedPorts();

console.log(await getPort({port}));
//=> 3000
```

## Beware

There is a very tiny chance of a race condition if another process starts using the same port number as you in between the time you get the port number and you actually start using it.
Expand Down
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {promisify} from 'node:util';
import net from 'node:net';
import test from 'ava';
import getPort, {portNumbers} from './index.js';
import getPort, {portNumbers, clearLockedPorts} from './index.js';

test('port can be bound when promise resolves', async t => {
const port = await getPort();
Expand Down Expand Up @@ -193,3 +193,18 @@ test('preferred ports is bound up with different hosts', async t => {

t.is(port, desiredPorts[3]);
});

test('clearLockedPorts()', async t => {
const desiredPort = 8088;
const port1 = await getPort({port: desiredPort});
t.is(port1, desiredPort);

const port2 = await getPort({port: desiredPort});
// Port1 is locked
t.not(port2, desiredPort);

// Clear locked ports
clearLockedPorts();
const port3 = await getPort({port: desiredPort});
t.is(port3, desiredPort);
});