@go22/port0 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/index.d.ts +3 -0
- package/index.js +87 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 go22
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @go22/port0
|
|
2
|
+
|
|
3
|
+
Check whether a TCP port is in use and find a random available port. Requires Node.js 18 or newer.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @go22/port0
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { genPort, isPortInUse } from '@go22/port0';
|
|
15
|
+
|
|
16
|
+
await isPortInUse(3000); // true or false
|
|
17
|
+
await isPortInUse('3000'); // true or false
|
|
18
|
+
|
|
19
|
+
await genPort(); // random available port from 1024 through 65535
|
|
20
|
+
await genPort(3000); // random available port from 3000 through 65535
|
|
21
|
+
await genPort(3000, 4000); // random available port from 3000 through 4000
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Both range boundaries are inclusive. `genPort` checks each candidate no more than once and rejects with a `RangeError` if every port in the range is in use.
|
|
25
|
+
|
|
26
|
+
Availability is inherently a point-in-time check. Another process can claim a returned port before your application binds to it.
|
|
27
|
+
|
|
28
|
+
Because of this race condition, the intended use is to bind or connect with the randomly generated port immediately, minimizing the risk of a collision.
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
MIT
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createServer } from 'node:net';
|
|
2
|
+
|
|
3
|
+
const PORT_LOWER = 1024;
|
|
4
|
+
const PORT_UPPER = 65535;
|
|
5
|
+
|
|
6
|
+
function parsePort(value, name) {
|
|
7
|
+
const port = typeof value === 'string' && /^(0|[1-9]\d*)$/.test(value)
|
|
8
|
+
? Number(value)
|
|
9
|
+
: value;
|
|
10
|
+
|
|
11
|
+
if (!Number.isInteger(port) || port < 0 || port > PORT_UPPER) {
|
|
12
|
+
throw new RangeError(`${name} must be an integer between 0 and ${PORT_UPPER}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return port;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Determine whether a TCP port cannot be bound because it is already in use.
|
|
20
|
+
*
|
|
21
|
+
* @param {number|string} port
|
|
22
|
+
* @returns {Promise<boolean>}
|
|
23
|
+
*/
|
|
24
|
+
export async function isPortInUse(port) {
|
|
25
|
+
const parsedPort = parsePort(port, 'port');
|
|
26
|
+
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const server = createServer();
|
|
29
|
+
server.unref();
|
|
30
|
+
|
|
31
|
+
server.once('error', (error) => {
|
|
32
|
+
if (error.code === 'EADDRINUSE') {
|
|
33
|
+
resolve(true);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
reject(error);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
server.listen({ port: parsedPort, exclusive: true }, () => {
|
|
41
|
+
server.close((error) => {
|
|
42
|
+
if (error) {
|
|
43
|
+
reject(error);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
resolve(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Find a random available TCP port in an inclusive range.
|
|
55
|
+
*
|
|
56
|
+
* @param {number} [pLower=1024]
|
|
57
|
+
* @param {number} [pUpper=65535]
|
|
58
|
+
* @returns {Promise<number>}
|
|
59
|
+
*/
|
|
60
|
+
export async function genPort(pLower = PORT_LOWER, pUpper = PORT_UPPER) {
|
|
61
|
+
const lower = parsePort(pLower, 'pLower');
|
|
62
|
+
const upper = parsePort(pUpper, 'pUpper');
|
|
63
|
+
|
|
64
|
+
if (lower > upper) {
|
|
65
|
+
throw new RangeError('pLower must be less than or equal to pUpper');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const size = upper - lower + 1;
|
|
69
|
+
const swaps = new Map();
|
|
70
|
+
|
|
71
|
+
// Lazily shuffle the range so each port is tested once without allocating it.
|
|
72
|
+
for (let index = 0; index < size; index += 1) {
|
|
73
|
+
const randomIndex = index + Math.floor(Math.random() * (size - index));
|
|
74
|
+
const candidateOffset = swaps.get(randomIndex) ?? randomIndex;
|
|
75
|
+
const currentOffset = swaps.get(index) ?? index;
|
|
76
|
+
|
|
77
|
+
swaps.set(randomIndex, currentOffset);
|
|
78
|
+
swaps.delete(index);
|
|
79
|
+
|
|
80
|
+
const candidate = lower + candidateOffset;
|
|
81
|
+
if (!(await isPortInUse(candidate))) {
|
|
82
|
+
return candidate;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
throw new RangeError(`No available port found between ${lower} and ${upper}`);
|
|
87
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@go22/port0",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Check whether a TCP port is in use and find a random available port.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"import": "./index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"index.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"port",
|
|
24
|
+
"available",
|
|
25
|
+
"network",
|
|
26
|
+
"tcp"
|
|
27
|
+
],
|
|
28
|
+
"license": "MIT"
|
|
29
|
+
}
|