mybase 1.1.11 → 1.1.13
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/package.json +1 -1
- package/ts/funcs/randomTCPPort.d.ts +2 -2
- package/ts/funcs/randomTCPPort.js +50 -34
- package/ts/funcs/randomTCPPort.ts +45 -30
package/package.json
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* This function should be used for testing purposes only
|
|
5
5
|
* This function is not suitable for production use
|
|
6
6
|
* @param timeout the timeout for each port check
|
|
7
|
-
* @param fromPort the first port number to check defaults to
|
|
8
|
-
* @param untilPort the last port number to check defaults to
|
|
7
|
+
* @param fromPort the first port number to check defaults to 1025
|
|
8
|
+
* @param untilPort the last port number to check defaults to 65534
|
|
9
9
|
* @param tries the number of tries to find a port number
|
|
10
10
|
* @param host the host to bind to
|
|
11
11
|
*/
|
|
@@ -40,45 +40,61 @@ const net = __importStar(require("net"));
|
|
|
40
40
|
* This function should be used for testing purposes only
|
|
41
41
|
* This function is not suitable for production use
|
|
42
42
|
* @param timeout the timeout for each port check
|
|
43
|
-
* @param fromPort the first port number to check defaults to
|
|
44
|
-
* @param untilPort the last port number to check defaults to
|
|
43
|
+
* @param fromPort the first port number to check defaults to 1025
|
|
44
|
+
* @param untilPort the last port number to check defaults to 65534
|
|
45
45
|
* @param tries the number of tries to find a port number
|
|
46
46
|
* @param host the host to bind to
|
|
47
47
|
*/
|
|
48
|
-
function randomTCPPort(timeout = 1000, fromPort =
|
|
48
|
+
function randomTCPPort(timeout = 1000, fromPort = 1025, untilPort = 65534, tries = 1000, host = '127.0.0.1') {
|
|
49
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
50
|
+
return new Promise(function (resolve, reject) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
// check if the given port range is valid
|
|
53
|
+
if (fromPort > untilPort)
|
|
54
|
+
return resolve(false);
|
|
55
|
+
// check if the given port range is valid
|
|
56
|
+
if (fromPort < 0 || untilPort < 0)
|
|
57
|
+
return resolve(false);
|
|
58
|
+
// check if the given port range is valid
|
|
59
|
+
if (fromPort > 65535 || untilPort > 65535)
|
|
60
|
+
return resolve(false);
|
|
61
|
+
// this function shall return a random port number that is not in use and we should validate this port number by trying to listen to it and releasing
|
|
62
|
+
// this port number immediately
|
|
63
|
+
// this function should be used for testing purposes only
|
|
64
|
+
// this function is not suitable for production use
|
|
65
|
+
while (true && tries > 0) {
|
|
66
|
+
tries--;
|
|
67
|
+
let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort);
|
|
68
|
+
let server = net.createServer();
|
|
69
|
+
let checkPort = new Promise((resolveInner, reject) => {
|
|
70
|
+
server.on('error', (err) => {
|
|
71
|
+
// any error should raise problem
|
|
72
|
+
resolveInner(false);
|
|
73
|
+
});
|
|
74
|
+
server.on('listening', () => {
|
|
75
|
+
resolveInner(true);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
server.listen(port, host);
|
|
79
|
+
let result = yield checkPort;
|
|
80
|
+
if (result) {
|
|
81
|
+
let to = setTimeout(() => {
|
|
82
|
+
return resolve(port);
|
|
83
|
+
}, 1000);
|
|
84
|
+
server.on('close', () => {
|
|
85
|
+
clearTimeout(to);
|
|
86
|
+
return resolve(port);
|
|
87
|
+
});
|
|
88
|
+
server.close();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
server.close();
|
|
93
|
+
}
|
|
94
|
+
if (tries === 0)
|
|
95
|
+
return resolve(false);
|
|
75
96
|
});
|
|
76
|
-
|
|
77
|
-
let result = yield checkPort;
|
|
78
|
-
if (result)
|
|
79
|
-
return port;
|
|
80
|
-
}
|
|
81
|
-
return false;
|
|
97
|
+
});
|
|
82
98
|
});
|
|
83
99
|
}
|
|
84
100
|
exports.randomTCPPort = randomTCPPort;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { wait } from './wait'
|
|
1
2
|
import * as net from 'net'
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -6,39 +7,53 @@ import * as net from 'net'
|
|
|
6
7
|
* This function should be used for testing purposes only
|
|
7
8
|
* This function is not suitable for production use
|
|
8
9
|
* @param timeout the timeout for each port check
|
|
9
|
-
* @param fromPort the first port number to check defaults to
|
|
10
|
-
* @param untilPort the last port number to check defaults to
|
|
10
|
+
* @param fromPort the first port number to check defaults to 1025
|
|
11
|
+
* @param untilPort the last port number to check defaults to 65534
|
|
11
12
|
* @param tries the number of tries to find a port number
|
|
12
13
|
* @param host the host to bind to
|
|
13
14
|
*/
|
|
14
|
-
export async function randomTCPPort(timeout:number=1000,fromPort:number=
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
export async function randomTCPPort(timeout: number = 1000, fromPort: number = 1025, untilPort: number = 65534, tries = 1000, host = '127.0.0.1'): Promise<number | false> {
|
|
16
|
+
return new Promise(async function (resolve, reject) {
|
|
17
|
+
// check if the given port range is valid
|
|
18
|
+
if (fromPort > untilPort) return resolve(false)
|
|
19
|
+
// check if the given port range is valid
|
|
20
|
+
if (fromPort < 0 || untilPort < 0) return resolve(false)
|
|
21
|
+
// check if the given port range is valid
|
|
22
|
+
if (fromPort > 65535 || untilPort > 65535) return resolve(false)
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
// this function shall return a random port number that is not in use and we should validate this port number by trying to listen to it and releasing
|
|
25
|
+
// this port number immediately
|
|
26
|
+
// this function should be used for testing purposes only
|
|
27
|
+
// this function is not suitable for production use
|
|
28
|
+
while(true && tries > 0) {
|
|
29
|
+
tries--
|
|
30
|
+
let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort)
|
|
31
|
+
let server = net.createServer()
|
|
32
|
+
let checkPort = new Promise((resolveInner, reject) => {
|
|
33
|
+
server.on('error', (err) => {
|
|
34
|
+
// any error should raise problem
|
|
35
|
+
resolveInner(false)
|
|
36
|
+
})
|
|
37
|
+
server.on('listening', () => {
|
|
38
|
+
resolveInner(true)
|
|
39
|
+
})
|
|
33
40
|
})
|
|
34
|
-
server.
|
|
41
|
+
server.listen(port, host)
|
|
42
|
+
let result = await checkPort
|
|
43
|
+
if (result) {
|
|
44
|
+
let to = setTimeout(() => {
|
|
45
|
+
return resolve(port)
|
|
46
|
+
},1000)
|
|
47
|
+
server.on('close', () => {
|
|
48
|
+
clearTimeout(to)
|
|
49
|
+
return resolve(port)
|
|
50
|
+
})
|
|
35
51
|
server.close()
|
|
36
|
-
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
52
|
+
return
|
|
53
|
+
} else server.close()
|
|
54
|
+
}
|
|
55
|
+
if (tries===0)
|
|
56
|
+
return resolve(false)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
}
|