mybase 1.1.9 → 1.1.11

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.9",
3
+ "version": "1.1.11",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -11,6 +11,6 @@ const ip6_1 = __importDefault(require("ip6"));
11
11
  function randomIP6() {
12
12
  var _a;
13
13
  const bytes = (0, crypto_1.randomBytes)(16);
14
- return ip6_1.default.normalize(((_a = bytes.toString('hex').match(/.{1,4}/g)) === null || _a === void 0 ? void 0 : _a.join(':')) || '0000:0000:0000:0000:0000:0000:0000:0000');
14
+ return ip6_1.default.abbreviate(ip6_1.default.normalize(((_a = bytes.toString('hex').match(/.{1,4}/g)) === null || _a === void 0 ? void 0 : _a.join(':')) || '0000:0000:0000:0000:0000:0000:0000:0000'));
15
15
  }
16
16
  exports.randomIP6 = randomIP6;
@@ -5,7 +5,7 @@ import ip6 from 'ip6'
5
5
 
6
6
  export function randomIP6() : string {
7
7
  const bytes = randomBytes(16)
8
- return ip6.normalize(bytes.toString('hex').match(/.{1,4}/g)?.join(':') || '0000:0000:0000:0000:0000:0000:0000:0000')
8
+ return ip6.abbreviate(ip6.normalize(bytes.toString('hex').match(/.{1,4}/g)?.join(':') || '0000:0000:0000:0000:0000:0000:0000:0000'))
9
9
  }
10
10
 
11
11
 
@@ -0,0 +1,12 @@
1
+ /**
2
+ * This function returns a random port number that is not in use and we should validate this port number by trying to listen to it and releasing
3
+ * this port number immediately
4
+ * This function should be used for testing purposes only
5
+ * This function is not suitable for production use
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
9
+ * @param tries the number of tries to find a port number
10
+ * @param host the host to bind to
11
+ */
12
+ export declare function randomTCPPort(timeout?: number, fromPort?: number, untilPort?: number, tries?: number, host?: string): Promise<number | false>;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.randomTCPPort = void 0;
36
+ const net = __importStar(require("net"));
37
+ /**
38
+ * This function returns a random port number that is not in use and we should validate this port number by trying to listen to it and releasing
39
+ * this port number immediately
40
+ * This function should be used for testing purposes only
41
+ * This function is not suitable for production use
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
45
+ * @param tries the number of tries to find a port number
46
+ * @param host the host to bind to
47
+ */
48
+ function randomTCPPort(timeout = 1000, fromPort = 25400, untilPort = 26400, tries = 1000, host = '127.0.0.1') {
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
+ });
75
+ });
76
+ server.listen(port, host);
77
+ let result = yield checkPort;
78
+ if (result)
79
+ return port;
80
+ }
81
+ return false;
82
+ });
83
+ }
84
+ exports.randomTCPPort = randomTCPPort;
@@ -0,0 +1,62 @@
1
+ import { wait } from "./wait";
2
+ import { randomTCPPort } from "./randomTCPPort";
3
+
4
+
5
+ describe("randomTCPPort", () => {
6
+ let fromPort = 15000;
7
+ let untilPort = fromPort+1000;
8
+ let hostToBind="127.0.0.1"
9
+
10
+ beforeEach(() => {
11
+ untilPort = fromPort+1000;
12
+ })
13
+
14
+ test("should return a random port number within the given range", async () => {
15
+ const port = await randomTCPPort(1000, fromPort, untilPort, 1000, hostToBind);
16
+ expect(port).toBeGreaterThanOrEqual(fromPort);
17
+ expect(port).toBeLessThanOrEqual(untilPort);
18
+ });
19
+
20
+ test("should return false if no available port is found", async () => {
21
+ untilPort = fromPort+1;
22
+ // listen to the port 15000 and 15001 to simulate that these ports are in use
23
+ const server1 = require("net").createServer();
24
+ const server2 = require("net").createServer();
25
+ server1.listen(fromPort, hostToBind);
26
+ server2.listen(untilPort, hostToBind);
27
+ // try to find a port within the range
28
+ const port = await randomTCPPort(1000, fromPort, untilPort, 1000, hostToBind);
29
+ expect(port).toBe(false);
30
+ server1.close();
31
+ server2.close();
32
+ await wait(1/100)
33
+ const port2 = await randomTCPPort(1000, fromPort, untilPort, 1000, hostToBind);
34
+ expect(port2===fromPort || port2===untilPort).toBe(true);
35
+ });
36
+
37
+ test("should handle errors when trying to listen to a port", async () => {
38
+ const port = await randomTCPPort(1000, fromPort, untilPort, 1000, "invalid-host");
39
+ expect(port).toBe(false);
40
+ })
41
+
42
+ test("should return false if the given port range is invalid", async () => {
43
+ const port = await randomTCPPort(1000, untilPort, fromPort, 1000, hostToBind);
44
+ expect(port).toBe(false);
45
+ });
46
+
47
+ test("should return false if the given port range is invalid", async () => {
48
+ const port = await randomTCPPort(1000, -1, untilPort, 1000, hostToBind);
49
+ expect(port).toBe(false);
50
+ });
51
+
52
+ test("should return false if the given port range is invalid", async () => {
53
+ const port = await randomTCPPort(1000, fromPort, -1, 1000, hostToBind);
54
+ expect(port).toBe(false);
55
+ });
56
+
57
+ test("should return false if the given port range is invalid", async () => {
58
+ const port = await randomTCPPort(1000, fromPort, 65536, 1000, hostToBind);
59
+ expect(port).toBe(false);
60
+ });
61
+
62
+ });
@@ -0,0 +1,44 @@
1
+ import * as net from 'net'
2
+
3
+ /**
4
+ * This function returns a random port number that is not in use and we should validate this port number by trying to listen to it and releasing
5
+ * this port number immediately
6
+ * This function should be used for testing purposes only
7
+ * This function is not suitable for production use
8
+ * @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
11
+ * @param tries the number of tries to find a port number
12
+ * @param host the host to bind to
13
+ */
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
21
+
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)
33
+ })
34
+ server.on('listening', () => {
35
+ 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
+ }
package/ts/index.d.ts CHANGED
@@ -14,3 +14,4 @@ export * from "./funcs/randomIP";
14
14
  export * from "./funcs/ip2int";
15
15
  export * from "./funcs/int2ip";
16
16
  export * from "./funcs/randomIP6";
17
+ export * from "./funcs/randomTCPPort";
package/ts/index.js CHANGED
@@ -30,3 +30,4 @@ __exportStar(require("./funcs/randomIP"), exports);
30
30
  __exportStar(require("./funcs/ip2int"), exports);
31
31
  __exportStar(require("./funcs/int2ip"), exports);
32
32
  __exportStar(require("./funcs/randomIP6"), exports);
33
+ __exportStar(require("./funcs/randomTCPPort"), exports);
package/ts/index.ts CHANGED
@@ -14,4 +14,5 @@ export * from "./funcs/randomIP"
14
14
  export * from "./funcs/ip2int"
15
15
  export * from "./funcs/int2ip"
16
16
  export * from "./funcs/randomIP6"
17
+ export * from "./funcs/randomTCPPort"
17
18