@cjser/get-port 7.2.0-cjser.2
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/dist-cjser/index.cjs +186 -0
- package/index.d.ts +111 -0
- package/index.js +199 -0
- package/license +9 -0
- package/package.json +79 -0
- package/readme.md +158 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// packages/@cjser/get-port.tmp-26-1778150857313/index.js
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
clearLockedPorts: () => clearLockedPorts,
|
|
33
|
+
default: () => getPorts,
|
|
34
|
+
portNumbers: () => portNumbers
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_node_net = __toESM(require("node:net"), 1);
|
|
38
|
+
var import_node_os = __toESM(require("node:os"), 1);
|
|
39
|
+
var Locked = class extends Error {
|
|
40
|
+
constructor(port) {
|
|
41
|
+
super(`${port} is locked`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var lockedPorts = {
|
|
45
|
+
old: /* @__PURE__ */ new Set(),
|
|
46
|
+
young: /* @__PURE__ */ new Set()
|
|
47
|
+
};
|
|
48
|
+
var releaseOldLockedPortsIntervalMs = 1e3 * 15;
|
|
49
|
+
var reservedPorts = /* @__PURE__ */ new Set();
|
|
50
|
+
var minPort = 1024;
|
|
51
|
+
var maxPort = 65535;
|
|
52
|
+
var timeout;
|
|
53
|
+
var getLocalHosts = () => {
|
|
54
|
+
const interfaces = import_node_os.default.networkInterfaces();
|
|
55
|
+
const results = /* @__PURE__ */ new Set([void 0, "0.0.0.0"]);
|
|
56
|
+
for (const _interface of Object.values(interfaces)) {
|
|
57
|
+
for (const config of _interface) {
|
|
58
|
+
results.add(config.address);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return results;
|
|
62
|
+
};
|
|
63
|
+
var checkAvailablePort = (options) => new Promise((resolve, reject) => {
|
|
64
|
+
const server = import_node_net.default.createServer();
|
|
65
|
+
server.unref();
|
|
66
|
+
server.on("error", reject);
|
|
67
|
+
server.listen(options, () => {
|
|
68
|
+
const { port } = server.address();
|
|
69
|
+
server.close(() => {
|
|
70
|
+
resolve(port);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
var getAvailablePort = async (options, hosts) => {
|
|
75
|
+
if (options.host || options.port === 0) {
|
|
76
|
+
return checkAvailablePort(options);
|
|
77
|
+
}
|
|
78
|
+
for (const host of hosts) {
|
|
79
|
+
try {
|
|
80
|
+
await checkAvailablePort({ port: options.port, host });
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (!["EADDRNOTAVAIL", "EINVAL"].includes(error.code)) {
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return options.port;
|
|
88
|
+
};
|
|
89
|
+
var isLockedPort = (port) => lockedPorts.old.has(port) || lockedPorts.young.has(port) || reservedPorts.has(port);
|
|
90
|
+
var portCheckSequence = function* (ports) {
|
|
91
|
+
if (ports) {
|
|
92
|
+
yield* ports;
|
|
93
|
+
}
|
|
94
|
+
yield 0;
|
|
95
|
+
};
|
|
96
|
+
async function getPorts(options) {
|
|
97
|
+
let ports;
|
|
98
|
+
let exclude = /* @__PURE__ */ new Set();
|
|
99
|
+
if (options) {
|
|
100
|
+
if (options.port) {
|
|
101
|
+
ports = typeof options.port === "number" ? [options.port] : options.port;
|
|
102
|
+
}
|
|
103
|
+
if (options.exclude) {
|
|
104
|
+
const excludeIterable = options.exclude;
|
|
105
|
+
if (typeof excludeIterable[Symbol.iterator] !== "function") {
|
|
106
|
+
throw new TypeError("The `exclude` option must be an iterable.");
|
|
107
|
+
}
|
|
108
|
+
for (const element of excludeIterable) {
|
|
109
|
+
if (typeof element !== "number") {
|
|
110
|
+
throw new TypeError("Each item in the `exclude` option must be a number corresponding to the port you want excluded.");
|
|
111
|
+
}
|
|
112
|
+
if (!Number.isSafeInteger(element)) {
|
|
113
|
+
throw new TypeError(`Number ${element} in the exclude option is not a safe integer and can't be used`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exclude = new Set(excludeIterable);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const { reserve, ...netOptions } = options ?? {};
|
|
120
|
+
if (timeout === void 0) {
|
|
121
|
+
timeout = setTimeout(() => {
|
|
122
|
+
timeout = void 0;
|
|
123
|
+
lockedPorts.old = lockedPorts.young;
|
|
124
|
+
lockedPorts.young = /* @__PURE__ */ new Set();
|
|
125
|
+
}, releaseOldLockedPortsIntervalMs);
|
|
126
|
+
if (timeout.unref) {
|
|
127
|
+
timeout.unref();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const hosts = getLocalHosts();
|
|
131
|
+
for (const port of portCheckSequence(ports)) {
|
|
132
|
+
try {
|
|
133
|
+
if (exclude.has(port)) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
let availablePort = await getAvailablePort({ ...netOptions, port }, hosts);
|
|
137
|
+
while (isLockedPort(availablePort)) {
|
|
138
|
+
if (port !== 0) {
|
|
139
|
+
throw new Locked(port);
|
|
140
|
+
}
|
|
141
|
+
availablePort = await getAvailablePort({ ...netOptions, port }, hosts);
|
|
142
|
+
}
|
|
143
|
+
if (reserve) {
|
|
144
|
+
reservedPorts.add(availablePort);
|
|
145
|
+
} else {
|
|
146
|
+
lockedPorts.young.add(availablePort);
|
|
147
|
+
}
|
|
148
|
+
return availablePort;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (!["EADDRINUSE", "EACCES"].includes(error.code) && !(error instanceof Locked)) {
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
throw new Error("No available ports found");
|
|
156
|
+
}
|
|
157
|
+
function portNumbers(from, to) {
|
|
158
|
+
if (!Number.isInteger(from) || !Number.isInteger(to)) {
|
|
159
|
+
throw new TypeError("`from` and `to` must be integer numbers");
|
|
160
|
+
}
|
|
161
|
+
if (from < minPort || from > maxPort) {
|
|
162
|
+
throw new RangeError(`'from' must be between ${minPort} and ${maxPort}`);
|
|
163
|
+
}
|
|
164
|
+
if (to < minPort || to > maxPort) {
|
|
165
|
+
throw new RangeError(`'to' must be between ${minPort} and ${maxPort}`);
|
|
166
|
+
}
|
|
167
|
+
if (from > to) {
|
|
168
|
+
throw new RangeError("`to` must be greater than or equal to `from`");
|
|
169
|
+
}
|
|
170
|
+
const generator = function* (from2, to2) {
|
|
171
|
+
for (let port = from2; port <= to2; port++) {
|
|
172
|
+
yield port;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
return generator(from, to);
|
|
176
|
+
}
|
|
177
|
+
function clearLockedPorts() {
|
|
178
|
+
lockedPorts.old.clear();
|
|
179
|
+
lockedPorts.young.clear();
|
|
180
|
+
reservedPorts.clear();
|
|
181
|
+
}
|
|
182
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
183
|
+
0 && (module.exports = {
|
|
184
|
+
clearLockedPorts,
|
|
185
|
+
portNumbers
|
|
186
|
+
});
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {type ListenOptions} from 'node:net';
|
|
2
|
+
|
|
3
|
+
export type Options = {
|
|
4
|
+
/**
|
|
5
|
+
A preferred port or an iterable of preferred ports to use.
|
|
6
|
+
*/
|
|
7
|
+
readonly port?: number | Iterable<number>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
Ports that should not be returned.
|
|
11
|
+
|
|
12
|
+
You could, for example, pass it the return value of the `portNumbers()` function.
|
|
13
|
+
*/
|
|
14
|
+
readonly exclude?: Iterable<number>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
Reserve the port so that it's locked for the lifetime of the process instead of the default 15-30 seconds.
|
|
18
|
+
|
|
19
|
+
This is useful when there is a long delay between getting the port and actually binding to it, such as in long-running test suites.
|
|
20
|
+
|
|
21
|
+
Reserved ports are locked globally by port number for the current process, even if you looked them up with a specific `host` or `ipv6Only` option.
|
|
22
|
+
|
|
23
|
+
Use {@link clearLockedPorts} to release reserved ports.
|
|
24
|
+
|
|
25
|
+
@default false
|
|
26
|
+
|
|
27
|
+
@example
|
|
28
|
+
```
|
|
29
|
+
import getPort from '@cjser/get-port';
|
|
30
|
+
|
|
31
|
+
const port = await getPort({reserve: true});
|
|
32
|
+
// `port` will not be returned again by get-port for the lifetime of the process
|
|
33
|
+
```
|
|
34
|
+
*/
|
|
35
|
+
readonly reserve?: boolean;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
|
|
39
|
+
|
|
40
|
+
By default, it checks availability on all local addresses defined in [OS network interfaces](https://nodejs.org/api/os.html#os_os_networkinterfaces). If this option is set, it will only check the given host.
|
|
41
|
+
*/
|
|
42
|
+
readonly host?: string;
|
|
43
|
+
} & Omit<ListenOptions, 'port'>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
Get an available TCP port number.
|
|
47
|
+
|
|
48
|
+
@returns Port number.
|
|
49
|
+
|
|
50
|
+
@example
|
|
51
|
+
```
|
|
52
|
+
import getPort from '@cjser/get-port';
|
|
53
|
+
|
|
54
|
+
console.log(await getPort());
|
|
55
|
+
//=> 51402
|
|
56
|
+
|
|
57
|
+
// Pass in a preferred port
|
|
58
|
+
console.log(await getPort({port: 3000}));
|
|
59
|
+
// Will use 3000 if available, otherwise fall back to a random port
|
|
60
|
+
|
|
61
|
+
// Pass in an array of preferred ports
|
|
62
|
+
console.log(await getPort({port: [3000, 3001, 3002]}));
|
|
63
|
+
// Will use any element in the preferred ports array if available, otherwise fall back to a random port
|
|
64
|
+
```
|
|
65
|
+
*/
|
|
66
|
+
export default function getPort(options?: Options): Promise<number>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
Generate port numbers in the given range `from`...`to`.
|
|
70
|
+
|
|
71
|
+
@param from - The first port of the range. Must be in the range `1024`...`65535`.
|
|
72
|
+
@param to - The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.
|
|
73
|
+
@returns The port numbers in the range.
|
|
74
|
+
|
|
75
|
+
@example
|
|
76
|
+
```
|
|
77
|
+
import getPort, {portNumbers} from '@cjser/get-port';
|
|
78
|
+
|
|
79
|
+
console.log(await getPort({port: portNumbers(3000, 3100)}));
|
|
80
|
+
// Will use any port from 3000 to 3100, otherwise fall back to a random port
|
|
81
|
+
```
|
|
82
|
+
*/
|
|
83
|
+
export function portNumbers(from: number, to: number): Iterable<number>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
Clear the internal cache of locked ports, including any ports locked with the {@link Options.reserve reserve} option.
|
|
87
|
+
|
|
88
|
+
This can be useful when you want the results to be unaffected by previous calls.
|
|
89
|
+
|
|
90
|
+
Please note that clearing the cache removes protection against [in-process race conditions](https://github.com/sindresorhus/get-port#beware).
|
|
91
|
+
|
|
92
|
+
@example
|
|
93
|
+
```
|
|
94
|
+
import getPort, {clearLockedPorts} from '@cjser/get-port';
|
|
95
|
+
|
|
96
|
+
const port = [3000, 3001, 3002];
|
|
97
|
+
|
|
98
|
+
console.log(await getPort({port}));
|
|
99
|
+
//=> 3000
|
|
100
|
+
|
|
101
|
+
console.log(await getPort({port}));
|
|
102
|
+
//=> 3001
|
|
103
|
+
|
|
104
|
+
// If you want the results to be unaffected by previous calls, clear the cache.
|
|
105
|
+
clearLockedPorts();
|
|
106
|
+
|
|
107
|
+
console.log(await getPort({port}));
|
|
108
|
+
//=> 3000
|
|
109
|
+
```
|
|
110
|
+
*/
|
|
111
|
+
export function clearLockedPorts(): void;
|
package/index.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
|
|
4
|
+
class Locked extends Error {
|
|
5
|
+
constructor(port) {
|
|
6
|
+
super(`${port} is locked`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const lockedPorts = {
|
|
11
|
+
old: new Set(),
|
|
12
|
+
young: new Set(),
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// On this interval, the old locked ports are discarded,
|
|
16
|
+
// the young locked ports are moved to old locked ports,
|
|
17
|
+
// and a new young set for locked ports are created.
|
|
18
|
+
const releaseOldLockedPortsIntervalMs = 1000 * 15;
|
|
19
|
+
|
|
20
|
+
// Keep `reserve` deliberately process-wide by port number.
|
|
21
|
+
// It is meant to avoid in-process races, not to model every possible
|
|
22
|
+
// IPv4/IPv6 or host-specific bind combination.
|
|
23
|
+
const reservedPorts = new Set();
|
|
24
|
+
|
|
25
|
+
const minPort = 1024;
|
|
26
|
+
const maxPort = 65_535;
|
|
27
|
+
|
|
28
|
+
// Lazily create timeout on first use
|
|
29
|
+
let timeout;
|
|
30
|
+
|
|
31
|
+
const getLocalHosts = () => {
|
|
32
|
+
const interfaces = os.networkInterfaces();
|
|
33
|
+
|
|
34
|
+
// Add undefined value for createServer function to use default host,
|
|
35
|
+
// and default IPv4 host in case createServer defaults to IPv6.
|
|
36
|
+
const results = new Set([undefined, '0.0.0.0']);
|
|
37
|
+
|
|
38
|
+
for (const _interface of Object.values(interfaces)) {
|
|
39
|
+
for (const config of _interface) {
|
|
40
|
+
results.add(config.address);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return results;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const checkAvailablePort = options =>
|
|
48
|
+
new Promise((resolve, reject) => {
|
|
49
|
+
const server = net.createServer();
|
|
50
|
+
server.unref();
|
|
51
|
+
server.on('error', reject);
|
|
52
|
+
|
|
53
|
+
server.listen(options, () => {
|
|
54
|
+
const {port} = server.address();
|
|
55
|
+
server.close(() => {
|
|
56
|
+
resolve(port);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const getAvailablePort = async (options, hosts) => {
|
|
62
|
+
if (options.host || options.port === 0) {
|
|
63
|
+
return checkAvailablePort(options);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (const host of hosts) {
|
|
67
|
+
try {
|
|
68
|
+
await checkAvailablePort({port: options.port, host}); // eslint-disable-line no-await-in-loop
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (!['EADDRNOTAVAIL', 'EINVAL'].includes(error.code)) {
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return options.port;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const isLockedPort = port => lockedPorts.old.has(port) || lockedPorts.young.has(port) || reservedPorts.has(port);
|
|
80
|
+
|
|
81
|
+
const portCheckSequence = function * (ports) {
|
|
82
|
+
if (ports) {
|
|
83
|
+
yield * ports;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
yield 0; // Fall back to 0 if anything else failed
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export default async function getPorts(options) {
|
|
90
|
+
let ports;
|
|
91
|
+
let exclude = new Set();
|
|
92
|
+
|
|
93
|
+
if (options) {
|
|
94
|
+
if (options.port) {
|
|
95
|
+
ports = typeof options.port === 'number' ? [options.port] : options.port;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (options.exclude) {
|
|
99
|
+
const excludeIterable = options.exclude;
|
|
100
|
+
|
|
101
|
+
if (typeof excludeIterable[Symbol.iterator] !== 'function') {
|
|
102
|
+
throw new TypeError('The `exclude` option must be an iterable.');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const element of excludeIterable) {
|
|
106
|
+
if (typeof element !== 'number') {
|
|
107
|
+
throw new TypeError('Each item in the `exclude` option must be a number corresponding to the port you want excluded.');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!Number.isSafeInteger(element)) {
|
|
111
|
+
throw new TypeError(`Number ${element} in the exclude option is not a safe integer and can't be used`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
exclude = new Set(excludeIterable);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const {reserve, ...netOptions} = options ?? {};
|
|
120
|
+
|
|
121
|
+
if (timeout === undefined) {
|
|
122
|
+
timeout = setTimeout(() => {
|
|
123
|
+
timeout = undefined;
|
|
124
|
+
|
|
125
|
+
lockedPorts.old = lockedPorts.young;
|
|
126
|
+
lockedPorts.young = new Set();
|
|
127
|
+
}, releaseOldLockedPortsIntervalMs);
|
|
128
|
+
|
|
129
|
+
// Does not exist in some environments (Electron, Jest jsdom env, browser, etc).
|
|
130
|
+
if (timeout.unref) {
|
|
131
|
+
timeout.unref();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const hosts = getLocalHosts();
|
|
136
|
+
|
|
137
|
+
for (const port of portCheckSequence(ports)) {
|
|
138
|
+
try {
|
|
139
|
+
if (exclude.has(port)) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let availablePort = await getAvailablePort({...netOptions, port}, hosts); // eslint-disable-line no-await-in-loop
|
|
144
|
+
while (isLockedPort(availablePort)) {
|
|
145
|
+
if (port !== 0) {
|
|
146
|
+
throw new Locked(port);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
availablePort = await getAvailablePort({...netOptions, port}, hosts); // eslint-disable-line no-await-in-loop
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (reserve) {
|
|
153
|
+
reservedPorts.add(availablePort);
|
|
154
|
+
} else {
|
|
155
|
+
lockedPorts.young.add(availablePort);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return availablePort;
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (!['EADDRINUSE', 'EACCES'].includes(error.code) && !(error instanceof Locked)) {
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
throw new Error('No available ports found');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function portNumbers(from, to) {
|
|
170
|
+
if (!Number.isInteger(from) || !Number.isInteger(to)) {
|
|
171
|
+
throw new TypeError('`from` and `to` must be integer numbers');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (from < minPort || from > maxPort) {
|
|
175
|
+
throw new RangeError(`'from' must be between ${minPort} and ${maxPort}`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (to < minPort || to > maxPort) {
|
|
179
|
+
throw new RangeError(`'to' must be between ${minPort} and ${maxPort}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (from > to) {
|
|
183
|
+
throw new RangeError('`to` must be greater than or equal to `from`');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const generator = function * (from, to) {
|
|
187
|
+
for (let port = from; port <= to; port++) {
|
|
188
|
+
yield port;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
return generator(from, to);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function clearLockedPorts() {
|
|
196
|
+
lockedPorts.old.clear();
|
|
197
|
+
lockedPorts.young.clear();
|
|
198
|
+
reservedPorts.clear();
|
|
199
|
+
}
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cjser/get-port",
|
|
3
|
+
"version": "7.2.0-cjser.2",
|
|
4
|
+
"description": "Get an available port",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://code.moenext.com/3rdeye/cjser.git"
|
|
9
|
+
},
|
|
10
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Sindre Sorhus",
|
|
13
|
+
"email": "sindresorhus@gmail.com",
|
|
14
|
+
"url": "https://sindresorhus.com"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"exports": {
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"require": "./dist-cjser/index.cjs",
|
|
20
|
+
"default": "./index.js"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=16"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "xo && ava && tsd"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"index.js",
|
|
31
|
+
"index.d.ts",
|
|
32
|
+
"dist-cjser"
|
|
33
|
+
],
|
|
34
|
+
"keywords": [
|
|
35
|
+
"port",
|
|
36
|
+
"find",
|
|
37
|
+
"finder",
|
|
38
|
+
"portfinder",
|
|
39
|
+
"free",
|
|
40
|
+
"available",
|
|
41
|
+
"connection",
|
|
42
|
+
"connect",
|
|
43
|
+
"open",
|
|
44
|
+
"net",
|
|
45
|
+
"tcp",
|
|
46
|
+
"scan",
|
|
47
|
+
"random",
|
|
48
|
+
"preferred",
|
|
49
|
+
"chosen"
|
|
50
|
+
],
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^20.2.5",
|
|
53
|
+
"ava": "^5.3.0",
|
|
54
|
+
"tsd": "^0.28.1",
|
|
55
|
+
"xo": "^0.54.2"
|
|
56
|
+
},
|
|
57
|
+
"types": "./index.d.ts",
|
|
58
|
+
"main": "./dist-cjser/index.cjs",
|
|
59
|
+
"cjser": {
|
|
60
|
+
"sourceVersion": "7.2.0",
|
|
61
|
+
"cjserVersion": 2,
|
|
62
|
+
"original": {
|
|
63
|
+
"name": "get-port",
|
|
64
|
+
"version": "7.2.0",
|
|
65
|
+
"exports": {
|
|
66
|
+
"types": "./index.d.ts",
|
|
67
|
+
"default": "./index.js"
|
|
68
|
+
},
|
|
69
|
+
"repository": "sindresorhus/get-port",
|
|
70
|
+
"files": [
|
|
71
|
+
"index.js",
|
|
72
|
+
"index.d.ts"
|
|
73
|
+
],
|
|
74
|
+
"scripts": {
|
|
75
|
+
"test": "xo && ava && tsd"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# get-port
|
|
2
|
+
|
|
3
|
+
> Get an available [TCP port](https://en.wikipedia.org/wiki/Port_(computer_networking)).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install get-port
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import getPort from 'get-port';
|
|
15
|
+
|
|
16
|
+
console.log(await getPort());
|
|
17
|
+
//=> 51402
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Pass in a preferred port:
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import getPort from 'get-port';
|
|
24
|
+
|
|
25
|
+
console.log(await getPort({port: 3000}));
|
|
26
|
+
// Will use 3000 if available, otherwise fall back to a random port
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Pass in an array of preferred ports:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import getPort from 'get-port';
|
|
33
|
+
|
|
34
|
+
console.log(await getPort({port: [3000, 3001, 3002]}));
|
|
35
|
+
// Will use any element in the preferred ports array if available, otherwise fall back to a random port
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Use the `portNumbers()` helper in case you need a port in a certain range:
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
import getPort, {portNumbers} from 'get-port';
|
|
42
|
+
|
|
43
|
+
console.log(await getPort({port: portNumbers(3000, 3100)}));
|
|
44
|
+
// Will use any port from 3000 to 3100, otherwise fall back to a random port
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
### getPort(options?)
|
|
50
|
+
|
|
51
|
+
Returns a `Promise` for a port number.
|
|
52
|
+
|
|
53
|
+
#### options
|
|
54
|
+
|
|
55
|
+
Type: `object`
|
|
56
|
+
|
|
57
|
+
##### port
|
|
58
|
+
|
|
59
|
+
Type: `number | Iterable<number>`
|
|
60
|
+
|
|
61
|
+
A preferred port or an iterable of preferred ports to use.
|
|
62
|
+
|
|
63
|
+
##### exclude
|
|
64
|
+
|
|
65
|
+
Type: `Iterable<number>`
|
|
66
|
+
|
|
67
|
+
Ports that should not be returned.
|
|
68
|
+
|
|
69
|
+
You could, for example, pass it the return value of the `portNumbers()` function.
|
|
70
|
+
|
|
71
|
+
##### reserve
|
|
72
|
+
|
|
73
|
+
Type: `boolean`\
|
|
74
|
+
Default: `false`
|
|
75
|
+
|
|
76
|
+
Reserve the port so that it's locked for the lifetime of the process instead of the default 15-30 seconds.
|
|
77
|
+
|
|
78
|
+
This is useful when there is a long delay between getting the port and actually binding to it, such as in long-running test suites.
|
|
79
|
+
|
|
80
|
+
Reserved ports are locked globally by port number for the current process, even if you looked them up with a specific `host` or `ipv6Only` option.
|
|
81
|
+
|
|
82
|
+
Use [`clearLockedPorts()`](#clearlockedports) to release reserved ports.
|
|
83
|
+
|
|
84
|
+
##### host
|
|
85
|
+
|
|
86
|
+
Type: `string`
|
|
87
|
+
|
|
88
|
+
The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
|
|
89
|
+
|
|
90
|
+
By default, it checks availability on all local addresses defined in [OS network interfaces](https://nodejs.org/api/os.html#os_os_networkinterfaces). If this option is set, it will only check the given host.
|
|
91
|
+
|
|
92
|
+
### portNumbers(from, to)
|
|
93
|
+
|
|
94
|
+
Generate port numbers in the given range `from`...`to`.
|
|
95
|
+
|
|
96
|
+
Returns an `Iterable` for port numbers in the given range.
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
import getPort, {portNumbers} from 'get-port';
|
|
100
|
+
|
|
101
|
+
console.log(await getPort({port: portNumbers(3000, 3100)}));
|
|
102
|
+
// Will use any port from 3000 to 3100, otherwise fall back to a random port
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### from
|
|
106
|
+
|
|
107
|
+
Type: `number`
|
|
108
|
+
|
|
109
|
+
The first port of the range. Must be in the range `1024`...`65535`.
|
|
110
|
+
|
|
111
|
+
#### to
|
|
112
|
+
|
|
113
|
+
Type: `number`
|
|
114
|
+
|
|
115
|
+
The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.
|
|
116
|
+
|
|
117
|
+
### clearLockedPorts()
|
|
118
|
+
|
|
119
|
+
Clear the internal cache of locked ports, including any ports locked with the [`reserve`](#reserve) option.
|
|
120
|
+
|
|
121
|
+
This can be useful when you want the results to be unaffected by previous calls.
|
|
122
|
+
|
|
123
|
+
Please note that clearing the cache removes protection against [in-process race conditions](#beware).
|
|
124
|
+
|
|
125
|
+
```js
|
|
126
|
+
import getPort, {clearLockedPorts} from 'get-port';
|
|
127
|
+
|
|
128
|
+
const port = [3000, 3001, 3002];
|
|
129
|
+
|
|
130
|
+
console.log(await getPort({port}));
|
|
131
|
+
//=> 3000
|
|
132
|
+
|
|
133
|
+
console.log(await getPort({port}));
|
|
134
|
+
//=> 3001
|
|
135
|
+
|
|
136
|
+
// If you want the results to be unaffected by previous calls, clear the cache.
|
|
137
|
+
clearLockedPorts();
|
|
138
|
+
|
|
139
|
+
console.log(await getPort({port}));
|
|
140
|
+
//=> 3000
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Beware
|
|
144
|
+
|
|
145
|
+
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.
|
|
146
|
+
|
|
147
|
+
**In-process race conditions** (such as when running parallel Jest tests) are completely eliminated by a lightweight locking mechanism where returned ports are held for 15-30 seconds before being eligible for reuse. If the delay between getting a port and binding to it may exceed this window (for example, in long-running test suites), use the [`reserve`](#reserve) option to lock the port for the lifetime of the process.
|
|
148
|
+
|
|
149
|
+
**Multi-process race conditions** are extremely rare and will result in an immediate `EADDRINUSE` error when attempting to bind to the port, allowing your application to retry.
|
|
150
|
+
|
|
151
|
+
## Related
|
|
152
|
+
|
|
153
|
+
- [get-port-cli](https://github.com/sindresorhus/get-port-cli) - CLI for this module
|
|
154
|
+
|
|
155
|
+
## cjser
|
|
156
|
+
|
|
157
|
+
This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
|
|
158
|
+
Original repository: https://github.com/sindresorhus/get-port
|