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
21 changes: 21 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,24 @@ console.log(await getPort({port: portNumbers(3000, 3100)}));
```
*/
export function portNumbers(from: number, to: number): Iterable<number>;

/**
Clear the internal cache of locked ports when you want your results to be unaffected by previous calls.
nanianlisao marked this conversation as resolved.
Show resolved Hide resolved

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

console.log(await getPort({prot: [3000, 3001, 3002]}));
nanianlisao marked this conversation as resolved.
Show resolved Hide resolved
//=> 3000

console.log(await getPort({prot: [3000, 3001, 3002]}));
//=> 3001

// If you want your results to be unaffected by previous calls, clear the cache.
clearLockedPorts();
console.log(await getPort({prot: [3000, 3001, 3002]}));
//=> 3000
```
*/
nanianlisao marked this conversation as resolved.
Show resolved Hide resolved
nanianlisao marked this conversation as resolved.
Show resolved Hide resolved
nanianlisao marked this conversation as resolved.
Show resolved Hide resolved
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();
}
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ console.log(await getPort({port: portNumbers(3000, 3100)}));
// Will use any port from 3000 to 3100, otherwise fall back to a random port
```

Use the `clearLockedPorts()` to clear the internal cache of locked ports.
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

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

console.log(await getPort({prot: [3000, 3001, 3002]}));
//=> 3000

console.log(await getPort({prot: [3000, 3001, 3002]}));
//=> 3001

// If you want the results to not be affected by previous calls, clear the cache.
clearLockedPorts();
console.log(await getPort({prot: [3000, 3001, 3002]}));
//=> 3000
```

## API

### getPort(options?)
Expand Down Expand Up @@ -94,6 +111,10 @@ Type: `number`

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

### clearLockedPorts
nanianlisao marked this conversation as resolved.
Show resolved Hide resolved

Clear the internal cache of locked ports when you want your results to be unaffected by previous calls.

## 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('clear locked ports', 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);
});