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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -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 25400
8
- * @param untilPort the last port number to check defaults to 26400
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 25400
44
- * @param untilPort the last port number to check defaults to 26400
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 = 25400, untilPort = 26400, tries = 1000, host = '127.0.0.1') {
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
- // check if the given port range is valid
51
- if (fromPort > untilPort)
52
- return false;
53
- // check if the given port range is valid
54
- if (fromPort < 0 || untilPort < 0)
55
- return false;
56
- // check if the given port range is valid
57
- if (fromPort > 65535 || untilPort > 65535)
58
- return false;
59
- // 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
60
- // this port number immediately
61
- // this function should be used for testing purposes only
62
- // this function is not suitable for production use
63
- for (let i = 0; i < tries; i++) {
64
- let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort);
65
- let server = net.createServer();
66
- let checkPort = new Promise((resolve, reject) => {
67
- server.on('error', (err) => {
68
- // any error should raise problem
69
- resolve(false);
70
- });
71
- server.on('listening', () => {
72
- server.close();
73
- resolve(true);
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
- server.listen(port, host);
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 25400
10
- * @param untilPort the last port number to check defaults to 26400
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=25400,untilPort:number=26400,tries=1000,host='127.0.0.1') : Promise<number|false> {
15
- // check if the given port range is valid
16
- if (fromPort>untilPort) return false
17
- // check if the given port range is valid
18
- if (fromPort<0 || untilPort<0) return false
19
- // check if the given port range is valid
20
- if (fromPort>65535 || untilPort>65535) return false
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
- // 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
23
- // this port number immediately
24
- // this function should be used for testing purposes only
25
- // this function is not suitable for production use
26
- for(let i=0; i<tries; i++) {
27
- let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort)
28
- let server = net.createServer()
29
- let checkPort = new Promise((resolve,reject) => {
30
- server.on('error', (err) => {
31
- // any error should raise problem
32
- resolve(false)
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.on('listening', () => {
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
- resolve(true)
37
- })
38
- })
39
- server.listen(port,host)
40
- let result = await checkPort
41
- if (result) return port
42
- }
43
- return false
44
- }
52
+ return
53
+ } else server.close()
54
+ }
55
+ if (tries===0)
56
+ return resolve(false)
57
+ })
58
+
59
+ }