Skip to content

Commit

Permalink
chore: export lockedPorts
Browse files Browse the repository at this point in the history
  • Loading branch information
nanianlisao committed Mar 12, 2024
1 parent 85c1867 commit 5f4465c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
26 changes: 26 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,29 @@ console.log(await getPort({port: portNumbers(3000, 3100)}));
```
*/
export function portNumbers(from: number, to: number): Iterable<number>;

/**
Exporting `lockedPorts` allows for explicit management of port state, enabling developers to maintain application stability and avoid potential conflicts due to untrackable port allocations.
*
@example
```
// Assuming `get-port` has been updated to export `lockedPorts`
const getPort = require('get-port');
const { lockedPorts } = require('get-port');
async function getUniquePort() {
const port = await getPort();
console.log(port); // Outputs a free port
// do something
lockedPorts.old.clear(); // Reset/Clear the lockedPorts before use
lockedPorts.young.clear(); // Reset/Clear the lockedPorts before use
// Now, when getPort is called again, it considers all ports (excluding the system reserved)
// which means there's no inadvertent port "leakage" or unintended persisting of locked ports across calls.
}
```
**/
export declare const lockedPorts: {
old: Set<number>;
young: Set<number>;
};
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Locked extends Error {
}
}

const lockedPorts = {
// Export variables to make them modifiable and clearable
export const lockedPorts = {
old: new Set(),
young: new Set(),
};
Expand Down
18 changes: 17 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, lockedPorts} from './index.js';

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

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

test('clear locked ports by lockedPorts', async t => {
const desiredPort = 8088;
const port1 = await getPort({port: desiredPort});
t.is(port1, desiredPort);

const port2 = await getPort({port: desiredPort});
// getPort is locked

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 20 on ubuntu-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 20 on windows-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 20 on macos-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Comments should not begin with a lowercase character.

Check failure on line 203 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Comments should not begin with a lowercase character.
t.not(port2, desiredPort);

// clear locked ports

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 20 on ubuntu-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 20 on windows-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 20 on macos-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Comments should not begin with a lowercase character.

Check failure on line 206 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Comments should not begin with a lowercase character.
lockedPorts.young.clear();
lockedPorts.old.clear();
const port3 = await getPort({port: desiredPort});
t.is(port3, desiredPort);
});

0 comments on commit 5f4465c

Please sign in to comment.