mybase 1.1.34 → 1.1.36
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/jest/isLANIp.test.js +36 -0
- package/jest/isLoopbackIP.test.js +31 -0
- package/jest.config.js +6 -0
- package/package.json +1 -5
- package/ts/funcs/Geoip2Paths.d.ts +7 -0
- package/ts/funcs/Geoip2Paths.js +26 -0
- package/ts/funcs/Geoip2Paths.test.ts +11 -0
- package/ts/funcs/Geoip2Paths.ts +26 -0
- package/ts/funcs/MaxRuntimeHours.d.ts +1 -0
- package/ts/funcs/MaxRuntimeHours.js +11 -0
- package/ts/funcs/MaxRuntimeHours.ts +7 -0
- package/ts/funcs/asJSON.d.ts +1 -0
- package/ts/funcs/asJSON.js +14 -0
- package/ts/funcs/asJSON.test.js +72 -0
- package/ts/funcs/asJSON.ts +6 -0
- package/ts/funcs/ensureFolder.d.ts +3 -0
- package/ts/funcs/ensureFolder.js +50 -0
- package/ts/funcs/ensureFolder.test.ts +79 -0
- package/ts/funcs/ensureFolder.ts +21 -0
- package/ts/funcs/fileCacheIsValid.d.ts +1 -0
- package/ts/funcs/fileCacheIsValid.js +40 -0
- package/ts/funcs/fileCacheIsValid.ts +13 -0
- package/ts/funcs/getMysql1.d.ts +8 -0
- package/ts/funcs/getMysql1.js +62 -0
- package/ts/funcs/getMysql1.ts +41 -0
- package/ts/funcs/getMysql2.d.ts +9 -0
- package/ts/funcs/getMysql2.js +62 -0
- package/ts/funcs/getMysql2.ts +42 -0
- package/ts/funcs/getWeekNumber.d.ts +1 -0
- package/ts/funcs/getWeekNumber.js +36 -0
- package/ts/funcs/getWeekNumber.test.ts +9 -0
- package/ts/funcs/getWeekNumber.ts +31 -0
- package/ts/funcs/hash_sha512.d.ts +1 -0
- package/ts/funcs/hash_sha512.js +11 -0
- package/ts/funcs/hash_sha512.ts +7 -0
- package/ts/funcs/initMysql2Pool.d.ts +3 -0
- package/ts/funcs/initMysql2Pool.js +39 -0
- package/ts/funcs/initMysql2Pool.ts +11 -0
- package/ts/funcs/int2ip.d.ts +1 -0
- package/ts/funcs/int2ip.js +18 -0
- package/ts/funcs/int2ip.test.js +33 -0
- package/ts/funcs/int2ip.ts +9 -0
- package/ts/funcs/ip2int.d.ts +1 -0
- package/ts/funcs/ip2int.js +18 -0
- package/ts/funcs/ip2int.test.js +30 -0
- package/ts/funcs/ip2int.ts +10 -0
- package/ts/funcs/isLANIp.d.ts +4 -0
- package/ts/funcs/isLANIp.js +47 -0
- package/ts/funcs/isLANIp.test.ts +59 -0
- package/ts/funcs/isLANIp.ts +37 -0
- package/ts/funcs/isLocal.d.ts +1 -0
- package/ts/funcs/isLocal.js +12 -0
- package/ts/funcs/isLocal.test.ts +16 -0
- package/ts/funcs/isLocal.ts +5 -0
- package/ts/funcs/isLoopbackIP.d.ts +4 -0
- package/ts/funcs/isLoopbackIP.js +42 -0
- package/ts/funcs/isLoopbackIP.test.ts +72 -0
- package/ts/funcs/isLoopbackIP.ts +32 -0
- package/ts/funcs/promiseTimeout.d.ts +1 -0
- package/ts/funcs/promiseTimeout.js +19 -0
- package/ts/funcs/promiseTimeout.test.js +104 -0
- package/ts/funcs/promiseTimeout.ts +19 -0
- package/ts/funcs/randomIP.d.ts +1 -0
- package/ts/funcs/randomIP.js +11 -0
- package/ts/funcs/randomIP.test.ts +16 -0
- package/ts/funcs/randomIP.ts +10 -0
- package/ts/funcs/randomIP6.d.ts +1 -0
- package/ts/funcs/randomIP6.js +17 -0
- package/ts/funcs/randomIP6.test.js +18 -0
- package/ts/funcs/randomIP6.ts +12 -0
- package/ts/funcs/randomString.d.ts +1 -0
- package/ts/funcs/randomString.js +8 -0
- package/ts/funcs/randomString.test.ts +15 -0
- package/ts/funcs/randomString.ts +5 -0
- package/ts/funcs/randomTCPPort.d.ts +12 -0
- package/ts/funcs/randomTCPPort.js +88 -0
- package/ts/funcs/randomTCPPort.test.ts +62 -0
- package/ts/funcs/randomTCPPort.ts +58 -0
- package/ts/funcs/randomUTFString.d.ts +1 -0
- package/ts/funcs/randomUTFString.js +31 -0
- package/ts/funcs/randomUTFString.test.ts +44 -0
- package/ts/funcs/randomUTFString.ts +33 -0
- package/ts/funcs/utcnow.d.ts +2 -0
- package/ts/funcs/utcnow.js +8 -0
- package/ts/funcs/utcnow.ts +5 -0
- package/ts/funcs/validEmail.d.ts +1 -0
- package/ts/funcs/validEmail.js +21 -0
- package/ts/funcs/validEmail.test.js +31 -0
- package/ts/funcs/validEmail.ts +13 -0
- package/ts/funcs/validIp.d.ts +1 -0
- package/ts/funcs/validIp.js +32 -0
- package/ts/funcs/validIp.test.ts +33 -0
- package/ts/funcs/validIp.ts +5 -0
- package/ts/funcs/vaultFill.d.ts +2 -0
- package/ts/funcs/vaultFill.js +86 -0
- package/ts/funcs/vaultFill.ts +89 -0
- package/ts/funcs/vaultRead.d.ts +2 -0
- package/ts/funcs/vaultRead.js +52 -0
- package/ts/funcs/vaultRead.ts +45 -0
- package/ts/funcs/wait.d.ts +1 -0
- package/ts/funcs/wait.js +10 -0
- package/ts/funcs/wait.ts +7 -0
- package/ts/global.d.ts +1 -0
- package/ts/global.js +16 -0
- package/ts/global.ts +10 -0
- package/ts/index.d.ts +30 -0
- package/ts/index.js +48 -0
- package/ts/index.test.ts +7 -0
- package/ts/index.ts +33 -0
- package/ts/models/IPAddress.d.ts +40 -0
- package/ts/models/IPAddress.js +255 -0
- package/ts/models/IPAddress.test.ts +393 -0
- package/ts/models/IPAddress.ts +272 -0
- package/ts/models/Timespan.d.ts +18 -0
- package/ts/models/Timespan.js +52 -0
- package/ts/models/Timespan.ts +63 -0
- package/ts/models/Unixtime.d.ts +32 -0
- package/ts/models/Unixtime.js +137 -0
- package/ts/models/Unixtime.test.ts +102 -0
- package/ts/models/Unixtime.ts +164 -0
- package/ts/types.d.ts +1 -0
- package/ts/types.js +3 -0
- package/ts/types.ts +1 -0
- package/tsconfig.json +36 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { randomIP } from './randomIP';
|
|
2
|
+
import { isLoopbackIP } from './isLoopbackIP';
|
|
3
|
+
|
|
4
|
+
describe('isLoopbackIP', () => {
|
|
5
|
+
test('should return true for loopback IPv4 address', () => {
|
|
6
|
+
expect(isLoopbackIP('127.0.0.1')).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test('should return true for loopback IPv6 address', () => {
|
|
10
|
+
expect(isLoopbackIP('::1')).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('should return false for non-loopback IPv4 address', () => {
|
|
14
|
+
expect(isLoopbackIP('192.168.0.1')).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('should return false for non-loopback IPv6 address', () => {
|
|
18
|
+
expect(isLoopbackIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should return false for invalid IP address', () => {
|
|
22
|
+
expect(isLoopbackIP('invalid')).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should return true for local IPv4', () => {
|
|
26
|
+
const ip = '127.0.0.1'
|
|
27
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('should return true for local IPv4', () => {
|
|
31
|
+
const ip = '127.2.0.1'
|
|
32
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('should return true for local IPv4', () => {
|
|
36
|
+
const ip = '127.255.0.1'
|
|
37
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('should return true for local IPv4', () => {
|
|
41
|
+
const ip = '127.127.127.127'
|
|
42
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('should return true for local IPv6', () => {
|
|
46
|
+
const ip = '::1'
|
|
47
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('should return null for non-local IPv4', () => {
|
|
51
|
+
const ip = '8.8.8.8'
|
|
52
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('should return null for non-local IPv6', () => {
|
|
56
|
+
const ip = '2001:4860:4860::8888'
|
|
57
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('should return false for invalid IP', () => {
|
|
61
|
+
const ip = 'invalid'
|
|
62
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('random ips should not be Loopback', () => {
|
|
66
|
+
for(let i=0;i<10;i++){
|
|
67
|
+
let ip = randomIP()
|
|
68
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
})
|
|
72
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import ip6addr from 'ip6addr'
|
|
2
|
+
import net from 'net'
|
|
3
|
+
|
|
4
|
+
export const local_network_cidrs = [
|
|
5
|
+
ip6addr.createCIDR('127.0.0.0/8'),
|
|
6
|
+
ip6addr.createCIDR('::1/128')
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
export function isLoopbackIP(ip:string) : boolean {
|
|
10
|
+
ip=normalizeIp(ip)
|
|
11
|
+
if (net.isIP(ip)===0) return false
|
|
12
|
+
try {
|
|
13
|
+
// speed optimized
|
|
14
|
+
let ipVersion = net.isIPv4(ip) ? 'ipv4' : 'ipv6'
|
|
15
|
+
for (let cidr of local_network_cidrs) {
|
|
16
|
+
let first = cidr.first()
|
|
17
|
+
if (first.kind() !== ipVersion) continue
|
|
18
|
+
if (cidr.contains(ip))
|
|
19
|
+
return true
|
|
20
|
+
}
|
|
21
|
+
} catch (err) {
|
|
22
|
+
console.log(err)
|
|
23
|
+
}
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function normalizeIp(ip:string):string {
|
|
28
|
+
// also support ipv6
|
|
29
|
+
if (net.isIP(ip) === 0) return ''
|
|
30
|
+
if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
|
|
31
|
+
return ip
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function promiseTimeout(miliseconds: number, inputPromise: Promise<any>, onTimeout?: any): Promise<any>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseTimeout = void 0;
|
|
4
|
+
function promiseTimeout(miliseconds, inputPromise, onTimeout = 'TIMEDOUT') {
|
|
5
|
+
// Create a promise that rejects in <miliseconds> milliseconds
|
|
6
|
+
let timeout = new Promise((resolve, reject) => {
|
|
7
|
+
let stID = setTimeout(() => {
|
|
8
|
+
clearTimeout(stID);
|
|
9
|
+
reject(onTimeout);
|
|
10
|
+
}, miliseconds);
|
|
11
|
+
});
|
|
12
|
+
// Returns a race between our timeout and the passed in promise
|
|
13
|
+
return Promise.race([
|
|
14
|
+
inputPromise,
|
|
15
|
+
timeout
|
|
16
|
+
]);
|
|
17
|
+
}
|
|
18
|
+
exports.promiseTimeout = promiseTimeout;
|
|
19
|
+
//# sourceMappingURL=promiseTimeout.js.map
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const { promiseTimeout } = require('./promiseTimeout');
|
|
2
|
+
|
|
3
|
+
describe('promiseTimeout', () => {
|
|
4
|
+
|
|
5
|
+
// Returns a promise that resolves when inputPromise resolves before the timeout.
|
|
6
|
+
it('should resolve when inputPromise resolves before the timeout', () => {
|
|
7
|
+
// Arrange
|
|
8
|
+
const inputPromise = new Promise((resolve) => {
|
|
9
|
+
setTimeout(() => {
|
|
10
|
+
resolve('Resolved');
|
|
11
|
+
}, 100);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
// Act
|
|
15
|
+
const resultPromise = promiseTimeout(200, inputPromise);
|
|
16
|
+
|
|
17
|
+
// Assert
|
|
18
|
+
return expect(resultPromise).resolves.toBe('Resolved');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Returns a promise that rejects with onTimeout when inputPromise rejects after the timeout.
|
|
22
|
+
it('should reject with onTimeout when inputPromise rejects after the timeout', () => {
|
|
23
|
+
// Arrange
|
|
24
|
+
const inputPromise = new Promise((resolve, reject) => {
|
|
25
|
+
setTimeout(() => {
|
|
26
|
+
reject('Rejected');
|
|
27
|
+
}, 200);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Act
|
|
31
|
+
const resultPromise = promiseTimeout(100, inputPromise);
|
|
32
|
+
|
|
33
|
+
// Assert
|
|
34
|
+
return expect(resultPromise).rejects.toBe('TIMEDOUT');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Returns a promise that rejects with onTimeout when inputPromise is pending after the timeout.
|
|
38
|
+
it('should reject with onTimeout when inputPromise is pending after the timeout', () => {
|
|
39
|
+
// Arrange
|
|
40
|
+
const inputPromise = new Promise(() => { });
|
|
41
|
+
|
|
42
|
+
// Act
|
|
43
|
+
const resultPromise = promiseTimeout(100, inputPromise);
|
|
44
|
+
|
|
45
|
+
// Assert
|
|
46
|
+
return expect(resultPromise).rejects.toBe('TIMEDOUT');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Returns a promise that resolves with inputPromise value when inputPromise resolves before the timeout.
|
|
50
|
+
it('should resolve with inputPromise value when inputPromise resolves before the timeout', () => {
|
|
51
|
+
// Arrange
|
|
52
|
+
const inputPromise = new Promise((resolve) => {
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
resolve('Resolved');
|
|
55
|
+
}, 100);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Act
|
|
59
|
+
const resultPromise = promiseTimeout(200, inputPromise);
|
|
60
|
+
|
|
61
|
+
// Assert
|
|
62
|
+
return expect(resultPromise).resolves.toBe('Resolved');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Returns a promise that rejects with inputPromise value when inputPromise rejects before the timeout.
|
|
66
|
+
it('should reject with inputPromise value when inputPromise rejects before the timeout', () => {
|
|
67
|
+
// Arrange
|
|
68
|
+
const inputPromise = new Promise((resolve, reject) => {
|
|
69
|
+
setTimeout(() => {
|
|
70
|
+
reject('Rejected');
|
|
71
|
+
}, 100);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Act
|
|
75
|
+
const resultPromise = promiseTimeout(200, inputPromise);
|
|
76
|
+
|
|
77
|
+
// Assert
|
|
78
|
+
return expect(resultPromise).rejects.toBe('Rejected');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Returns a promise that rejects with inputPromise value when inputPromise is pending before the timeout.
|
|
82
|
+
it('should reject with inputPromise value when inputPromise is pending before the timeout', () => {
|
|
83
|
+
// Arrange
|
|
84
|
+
const inputPromise = new Promise(() => { });
|
|
85
|
+
|
|
86
|
+
// Act
|
|
87
|
+
const resultPromise = promiseTimeout(200, inputPromise);
|
|
88
|
+
|
|
89
|
+
// Assert
|
|
90
|
+
return expect(resultPromise).rejects.toBe('TIMEDOUT');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Returns a promise that resolves with undefined when inputPromise is undefined.
|
|
94
|
+
it('should resolve with undefined when inputPromise is undefined', () => {
|
|
95
|
+
// Arrange
|
|
96
|
+
const inputPromise = undefined;
|
|
97
|
+
|
|
98
|
+
// Act
|
|
99
|
+
const resultPromise = promiseTimeout(200, inputPromise);
|
|
100
|
+
|
|
101
|
+
// Assert
|
|
102
|
+
return expect(resultPromise).resolves.toBe(undefined);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export function promiseTimeout(miliseconds:number, inputPromise:Promise<any>, onTimeout:any='TIMEDOUT') { // ported
|
|
6
|
+
// Create a promise that rejects in <miliseconds> milliseconds
|
|
7
|
+
let timeout = new Promise((resolve, reject) => {
|
|
8
|
+
let stID = setTimeout(() => {
|
|
9
|
+
clearTimeout(stID);
|
|
10
|
+
reject(onTimeout)
|
|
11
|
+
}, miliseconds)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
// Returns a race between our timeout and the passed in promise
|
|
15
|
+
return Promise.race([
|
|
16
|
+
inputPromise,
|
|
17
|
+
timeout
|
|
18
|
+
])
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function randomIP(): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.randomIP = void 0;
|
|
4
|
+
function randomIP() {
|
|
5
|
+
function int2ip(ipInt) {
|
|
6
|
+
return ((ipInt >>> 24) + '.' + (ipInt >> 16 & 255) + '.' + (ipInt >> 8 & 255) + '.' + (ipInt & 255));
|
|
7
|
+
}
|
|
8
|
+
return int2ip(Math.random() * Math.pow(2, 32));
|
|
9
|
+
}
|
|
10
|
+
exports.randomIP = randomIP;
|
|
11
|
+
//# sourceMappingURL=randomIP.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { randomIP } from './randomIP';
|
|
2
|
+
|
|
3
|
+
describe('randomIP', () => {
|
|
4
|
+
it('should return a valid IP address', () => {
|
|
5
|
+
for(let i=0; i<1000; i++) {
|
|
6
|
+
const ipAddress = randomIP();
|
|
7
|
+
const ipParts = ipAddress.split('.');
|
|
8
|
+
expect(ipParts.length).toBe(4);
|
|
9
|
+
ipParts.forEach((part) => {
|
|
10
|
+
const partInt = parseInt(part);
|
|
11
|
+
expect(partInt).toBeGreaterThanOrEqual(0);
|
|
12
|
+
expect(partInt).toBeLessThanOrEqual(255);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function randomIP6(): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.randomIP6 = void 0;
|
|
7
|
+
const crypto_1 = require("crypto");
|
|
8
|
+
// const ip6 = require('ip6')
|
|
9
|
+
//@ts-ignore
|
|
10
|
+
const ip6_1 = __importDefault(require("ip6"));
|
|
11
|
+
function randomIP6() {
|
|
12
|
+
var _a;
|
|
13
|
+
const bytes = (0, crypto_1.randomBytes)(16);
|
|
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
|
+
}
|
|
16
|
+
exports.randomIP6 = randomIP6;
|
|
17
|
+
//# sourceMappingURL=randomIP6.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const ip6 = require('ip6');
|
|
2
|
+
const { randomIP6 } = require('./randomIP6');
|
|
3
|
+
|
|
4
|
+
describe('randomIP6', () => {
|
|
5
|
+
it('should generate a random IPv6 address', () => {
|
|
6
|
+
const result = randomIP6();
|
|
7
|
+
expect(()=>ip6.normalize(result)).not.toThrow()
|
|
8
|
+
expect(()=>ip6.abbreviate(result)).not.toThrow()
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('generate 100 ipv6', () => {
|
|
12
|
+
for (let i = 0; i < 100; i++) {
|
|
13
|
+
const result = randomIP6();
|
|
14
|
+
expect(()=>ip6.normalize(result)).not.toThrow()
|
|
15
|
+
expect(()=>ip6.abbreviate(result)).not.toThrow()
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { randomBytes } from "crypto"
|
|
2
|
+
// const ip6 = require('ip6')
|
|
3
|
+
//@ts-ignore
|
|
4
|
+
import ip6 from 'ip6'
|
|
5
|
+
|
|
6
|
+
export function randomIP6() : string {
|
|
7
|
+
const bytes = randomBytes(16)
|
|
8
|
+
return ip6.abbreviate(ip6.normalize(bytes.toString('hex').match(/.{1,4}/g)?.join(':') || '0000:0000:0000:0000:0000:0000:0000:0000'))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function randomString(length?: number, charSet?: string): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.randomString = void 0;
|
|
4
|
+
function randomString(length = 10, charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
|
|
5
|
+
return Array.from({ length }, () => charSet.charAt(Math.floor(Math.random() * charSet.length))).join('');
|
|
6
|
+
}
|
|
7
|
+
exports.randomString = randomString;
|
|
8
|
+
//# sourceMappingURL=randomString.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { randomString } from "./randomString";
|
|
2
|
+
|
|
3
|
+
test('randomString', () => {
|
|
4
|
+
// Test default length and character set
|
|
5
|
+
expect(randomString()).toHaveLength(10);
|
|
6
|
+
expect(randomString()).toMatch(/^[A-Za-z0-9]{10}$/);
|
|
7
|
+
|
|
8
|
+
// Test custom length and character set
|
|
9
|
+
expect(randomString(5, "abc")).toHaveLength(5);
|
|
10
|
+
expect(randomString(5, "abc")).toMatch(/^[abc]{5}$/);
|
|
11
|
+
|
|
12
|
+
// Test empty character set
|
|
13
|
+
expect(randomString(10, "")).toHaveLength(0);
|
|
14
|
+
expect(randomString(10, "")).toMatch(/^$/);
|
|
15
|
+
});
|
|
@@ -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 1025
|
|
8
|
+
* @param untilPort the last port number to check defaults to 65534
|
|
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,88 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.randomTCPPort = void 0;
|
|
27
|
+
const net = __importStar(require("net"));
|
|
28
|
+
/**
|
|
29
|
+
* 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
|
|
30
|
+
* this port number immediately
|
|
31
|
+
* This function should be used for testing purposes only
|
|
32
|
+
* This function is not suitable for production use
|
|
33
|
+
* @param timeout the timeout for each port check
|
|
34
|
+
* @param fromPort the first port number to check defaults to 1025
|
|
35
|
+
* @param untilPort the last port number to check defaults to 65534
|
|
36
|
+
* @param tries the number of tries to find a port number
|
|
37
|
+
* @param host the host to bind to
|
|
38
|
+
*/
|
|
39
|
+
async function randomTCPPort(timeout = 1000, fromPort = 1025, untilPort = 65534, tries = 1000, host = '127.0.0.1') {
|
|
40
|
+
return new Promise(async function (resolve, reject) {
|
|
41
|
+
// check if the given port range is valid
|
|
42
|
+
if (fromPort > untilPort)
|
|
43
|
+
return resolve(false);
|
|
44
|
+
// check if the given port range is valid
|
|
45
|
+
if (fromPort < 0 || untilPort < 0)
|
|
46
|
+
return resolve(false);
|
|
47
|
+
// check if the given port range is valid
|
|
48
|
+
if (fromPort > 65535 || untilPort > 65535)
|
|
49
|
+
return resolve(false);
|
|
50
|
+
// 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
|
|
51
|
+
// this port number immediately
|
|
52
|
+
// this function should be used for testing purposes only
|
|
53
|
+
// this function is not suitable for production use
|
|
54
|
+
while (true && tries > 0) {
|
|
55
|
+
tries--;
|
|
56
|
+
let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort);
|
|
57
|
+
let server = net.createServer();
|
|
58
|
+
let checkPort = new Promise((resolveInner, reject) => {
|
|
59
|
+
server.on('error', (err) => {
|
|
60
|
+
// any error should raise problem
|
|
61
|
+
resolveInner(false);
|
|
62
|
+
});
|
|
63
|
+
server.on('listening', () => {
|
|
64
|
+
resolveInner(true);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
server.listen(port, host);
|
|
68
|
+
let result = await checkPort;
|
|
69
|
+
if (result) {
|
|
70
|
+
let to = setTimeout(() => {
|
|
71
|
+
return resolve(port);
|
|
72
|
+
}, 1000);
|
|
73
|
+
server.on('close', () => {
|
|
74
|
+
clearTimeout(to);
|
|
75
|
+
return resolve(port);
|
|
76
|
+
});
|
|
77
|
+
server.close();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
else
|
|
81
|
+
server.close();
|
|
82
|
+
}
|
|
83
|
+
if (tries === 0)
|
|
84
|
+
return resolve(false);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
exports.randomTCPPort = randomTCPPort;
|
|
88
|
+
//# sourceMappingURL=randomTCPPort.js.map
|
|
@@ -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,58 @@
|
|
|
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 1025
|
|
10
|
+
* @param untilPort the last port number to check defaults to 65534
|
|
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 = 1025, untilPort: number = 65534, tries = 1000, host = '127.0.0.1'): Promise<number | false> {
|
|
15
|
+
return new Promise(async function (resolve, reject) {
|
|
16
|
+
// check if the given port range is valid
|
|
17
|
+
if (fromPort > untilPort) return resolve(false)
|
|
18
|
+
// check if the given port range is valid
|
|
19
|
+
if (fromPort < 0 || untilPort < 0) return resolve(false)
|
|
20
|
+
// check if the given port range is valid
|
|
21
|
+
if (fromPort > 65535 || untilPort > 65535) return resolve(false)
|
|
22
|
+
|
|
23
|
+
// 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
|
|
24
|
+
// this port number immediately
|
|
25
|
+
// this function should be used for testing purposes only
|
|
26
|
+
// this function is not suitable for production use
|
|
27
|
+
while(true && tries > 0) {
|
|
28
|
+
tries--
|
|
29
|
+
let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort)
|
|
30
|
+
let server = net.createServer()
|
|
31
|
+
let checkPort = new Promise((resolveInner, reject) => {
|
|
32
|
+
server.on('error', (err) => {
|
|
33
|
+
// any error should raise problem
|
|
34
|
+
resolveInner(false)
|
|
35
|
+
})
|
|
36
|
+
server.on('listening', () => {
|
|
37
|
+
resolveInner(true)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
server.listen(port, host)
|
|
41
|
+
let result = await checkPort
|
|
42
|
+
if (result) {
|
|
43
|
+
let to = setTimeout(() => {
|
|
44
|
+
return resolve(port)
|
|
45
|
+
},1000)
|
|
46
|
+
server.on('close', () => {
|
|
47
|
+
clearTimeout(to)
|
|
48
|
+
return resolve(port)
|
|
49
|
+
})
|
|
50
|
+
server.close()
|
|
51
|
+
return
|
|
52
|
+
} else server.close()
|
|
53
|
+
}
|
|
54
|
+
if (tries===0)
|
|
55
|
+
return resolve(false)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function randomUTFString(minLength: number, maxLength: number, includeAscii?: boolean): string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.randomUTFString = void 0;
|
|
4
|
+
function randomUTFString(minLength, maxLength, includeAscii = true) {
|
|
5
|
+
// Extended characters set including various languages and symbols
|
|
6
|
+
let characters = 'àáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ' + // Accented Latin characters
|
|
7
|
+
'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρσςτυφχψω' + // Greek
|
|
8
|
+
'אבגדהוזחטיךכלםמןנסעפצקרשת' + // Hebrew
|
|
9
|
+
//Arabic
|
|
10
|
+
'ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ف ق ك ل م ن ه و ي' +
|
|
11
|
+
// Persian
|
|
12
|
+
'آ ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی' +
|
|
13
|
+
// Turkish
|
|
14
|
+
'ğıöşü' +
|
|
15
|
+
'ĞİÖŞÜ ' +
|
|
16
|
+
// More Emojis
|
|
17
|
+
'🙂🤔😍😂👍🚀❤️🎉' +
|
|
18
|
+
'бвгдежзийклмнопрстуфхцчшщъыьэюя' + // Cyrillic (Russian, etc.)
|
|
19
|
+
'日本語中文한국어' + // Japanese, Chinese, Korean characters
|
|
20
|
+
'🙂🤔😍😂👍🚀❤️🎉'; // Emojis
|
|
21
|
+
if (includeAscii)
|
|
22
|
+
characters += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
23
|
+
const charactersLength = Array.from(characters).length;
|
|
24
|
+
const length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength;
|
|
25
|
+
let result = '';
|
|
26
|
+
for (let i = 0; i < length; i++)
|
|
27
|
+
result += Array.from(characters)[Math.floor(Math.random() * charactersLength)];
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
exports.randomUTFString = randomUTFString;
|
|
31
|
+
//# sourceMappingURL=randomUTFString.js.map
|