mybase 1.1.53 → 1.2.3

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.
Files changed (124) hide show
  1. package/ip6addr.d.ts +10 -1
  2. package/mybase.d.ts +57 -0
  3. package/mybase.js +401 -684
  4. package/mybase.js.map +1 -0
  5. package/package.json +18 -2
  6. package/ts/funcs/Geoip2Paths.js +7 -5
  7. package/ts/funcs/Geoip2Paths.js.map +1 -0
  8. package/ts/funcs/MaxRuntimeHours.js.map +1 -0
  9. package/ts/funcs/asJSON.d.ts +1 -1
  10. package/ts/funcs/asJSON.js +6 -4
  11. package/ts/funcs/asJSON.js.map +1 -0
  12. package/ts/funcs/deepCopy.js.map +1 -0
  13. package/ts/funcs/ensureFolder.js.map +1 -0
  14. package/ts/funcs/fileCacheIsValid.js.map +1 -0
  15. package/ts/funcs/getMysql1.js.map +1 -0
  16. package/ts/funcs/getMysql2.js.map +1 -0
  17. package/ts/funcs/getWeekNumber.js.map +1 -0
  18. package/ts/funcs/hash_sha512.d.ts +1 -1
  19. package/ts/funcs/hash_sha512.js +4 -4
  20. package/ts/funcs/hash_sha512.js.map +1 -0
  21. package/ts/funcs/initMysql2Pool.js.map +1 -0
  22. package/ts/funcs/int2ip.js.map +1 -0
  23. package/ts/funcs/ip2int.js.map +1 -0
  24. package/ts/funcs/isLANIp.d.ts +2 -3
  25. package/ts/funcs/isLANIp.js +14 -15
  26. package/ts/funcs/isLANIp.js.map +1 -0
  27. package/ts/funcs/isLocal.js.map +1 -0
  28. package/ts/funcs/isLoopbackIP.d.ts +2 -3
  29. package/ts/funcs/isLoopbackIP.js +15 -16
  30. package/ts/funcs/isLoopbackIP.js.map +1 -0
  31. package/ts/funcs/knexConnection.js.map +1 -0
  32. package/ts/funcs/promiseTimeout.js.map +1 -0
  33. package/ts/funcs/randomIP.js.map +1 -0
  34. package/ts/funcs/randomIP6.js.map +1 -0
  35. package/ts/funcs/randomString.js.map +1 -0
  36. package/ts/funcs/randomTCPPort.js.map +1 -0
  37. package/ts/funcs/randomUTFString.js.map +1 -0
  38. package/ts/funcs/utcnow.js.map +1 -0
  39. package/ts/funcs/validEmail.d.ts +1 -1
  40. package/ts/funcs/validEmail.js +0 -3
  41. package/ts/funcs/validEmail.js.map +1 -0
  42. package/ts/funcs/validIp.js.map +1 -0
  43. package/ts/funcs/vaultFill.js +1 -1
  44. package/ts/funcs/vaultFill.js.map +1 -0
  45. package/ts/funcs/vaultRead.js +9 -3
  46. package/ts/funcs/vaultRead.js.map +1 -0
  47. package/ts/funcs/wait.js.map +1 -0
  48. package/ts/global.js.map +1 -0
  49. package/ts/index.js.map +1 -0
  50. package/ts/models/DateIterator.js.map +1 -0
  51. package/ts/models/IPAddress.d.ts +13 -13
  52. package/ts/models/IPAddress.js.map +1 -0
  53. package/ts/models/Interfaces.js.map +1 -0
  54. package/ts/models/OTPGenerator.js.map +1 -0
  55. package/ts/models/Timespan.js.map +1 -0
  56. package/ts/models/Unixtime.js.map +1 -0
  57. package/ts/types.d.ts +35 -0
  58. package/ts/types.js +1 -0
  59. package/ts/types.js.map +1 -0
  60. package/jest/isLANIp.test.js +0 -36
  61. package/jest/isLoopbackIP.test.js +0 -31
  62. package/jest.config.js +0 -7
  63. package/ts/funcs/Geoip2Paths.test.ts +0 -11
  64. package/ts/funcs/Geoip2Paths.ts +0 -26
  65. package/ts/funcs/MaxRuntimeHours.ts +0 -7
  66. package/ts/funcs/asJSON.test.js +0 -72
  67. package/ts/funcs/asJSON.ts +0 -6
  68. package/ts/funcs/deepCopy.test.ts +0 -108
  69. package/ts/funcs/deepCopy.ts +0 -34
  70. package/ts/funcs/ensureFolder.test.ts +0 -79
  71. package/ts/funcs/ensureFolder.ts +0 -21
  72. package/ts/funcs/fileCacheIsValid.ts +0 -13
  73. package/ts/funcs/getMysql1.ts +0 -41
  74. package/ts/funcs/getMysql2.ts +0 -42
  75. package/ts/funcs/getWeekNumber.test.ts +0 -9
  76. package/ts/funcs/getWeekNumber.ts +0 -31
  77. package/ts/funcs/hash_sha512.ts +0 -7
  78. package/ts/funcs/initMysql2Pool.ts +0 -11
  79. package/ts/funcs/int2ip.test.js +0 -33
  80. package/ts/funcs/int2ip.ts +0 -9
  81. package/ts/funcs/ip2int.test.js +0 -30
  82. package/ts/funcs/ip2int.ts +0 -10
  83. package/ts/funcs/isLANIp.test.ts +0 -59
  84. package/ts/funcs/isLANIp.ts +0 -37
  85. package/ts/funcs/isLocal.test.ts +0 -16
  86. package/ts/funcs/isLocal.ts +0 -5
  87. package/ts/funcs/isLoopbackIP.test.ts +0 -72
  88. package/ts/funcs/isLoopbackIP.ts +0 -32
  89. package/ts/funcs/knexConnection.ts +0 -42
  90. package/ts/funcs/promiseTimeout.test.js +0 -104
  91. package/ts/funcs/promiseTimeout.ts +0 -19
  92. package/ts/funcs/randomIP.test.ts +0 -16
  93. package/ts/funcs/randomIP.ts +0 -10
  94. package/ts/funcs/randomIP6.test.js +0 -18
  95. package/ts/funcs/randomIP6.ts +0 -12
  96. package/ts/funcs/randomString.test.ts +0 -15
  97. package/ts/funcs/randomString.ts +0 -5
  98. package/ts/funcs/randomTCPPort.test.ts +0 -62
  99. package/ts/funcs/randomTCPPort.ts +0 -58
  100. package/ts/funcs/randomUTFString.test.ts +0 -44
  101. package/ts/funcs/randomUTFString.ts +0 -33
  102. package/ts/funcs/utcnow.ts +0 -5
  103. package/ts/funcs/validEmail.test.js +0 -31
  104. package/ts/funcs/validEmail.ts +0 -13
  105. package/ts/funcs/validIp.test.ts +0 -33
  106. package/ts/funcs/validIp.ts +0 -5
  107. package/ts/funcs/vaultFill.ts +0 -89
  108. package/ts/funcs/vaultRead.ts +0 -45
  109. package/ts/funcs/wait.ts +0 -7
  110. package/ts/global.ts +0 -10
  111. package/ts/index.test.ts +0 -7
  112. package/ts/index.ts +0 -39
  113. package/ts/models/DateIterator.test.ts +0 -149
  114. package/ts/models/DateIterator.ts +0 -80
  115. package/ts/models/IPAddress.test.ts +0 -393
  116. package/ts/models/IPAddress.ts +0 -272
  117. package/ts/models/Interfaces.ts +0 -58
  118. package/ts/models/OTPGenerator.test.ts +0 -42
  119. package/ts/models/OTPGenerator.ts +0 -43
  120. package/ts/models/Timespan.ts +0 -80
  121. package/ts/models/Unixtime.test.ts +0 -168
  122. package/ts/models/Unixtime.ts +0 -208
  123. package/ts/types.ts +0 -1
  124. package/tsconfig.json +0 -36
@@ -1,11 +0,0 @@
1
- import * as mysql2 from "mysql2"
2
- import { Pool } from "mysql2"
3
- import debug from "debug"
4
- import { Mysql2ServerConfiguration } from "./getMysql2"
5
- const dbg = debug("initMysql2Pool")
6
-
7
- export function initMysql2Pool(sqlServer: Mysql2ServerConfiguration): Pool {
8
- const connectionUri = `mysql://${sqlServer.login}:${sqlServer.password}@${sqlServer.host}`
9
- dbg(`creating mysql pool to ${sqlServer.host}@${sqlServer.db} as connectionUri=${connectionUri}`)
10
- return mysql2.createPool(connectionUri)
11
- }
@@ -1,33 +0,0 @@
1
- const { int2ip } =require('./int2ip')
2
-
3
- describe('int2ip', () => {
4
- it('should convert integer to valid IPv4 address', () => {
5
- const ipInt = 3232235521; // equivalent to 192.168.0.1
6
- const result = int2ip(ipInt);
7
- expect(result).toBe('192.168.0.1');
8
- });
9
-
10
- it('should throw an error for non-numeric input', () => {
11
- const ipInt = '3232235521'; // string input instead of number
12
- expect(() => int2ip(ipInt)).toThrow('require number as input');
13
- expect(() => int2ip([])).toThrow('require number as input');
14
- expect(() => int2ip(null)).toThrow('require number as input');
15
- expect(() => int2ip({})).toThrow('require number as input');
16
- });
17
-
18
- it('overflowing is allowed', () => {
19
- const ipInt = 4294967297;
20
- let ip = int2ip(ipInt)
21
- expect(ip).toBe('0.0.0.1')
22
- });
23
-
24
- it('should support 0', () => {
25
- const ipInt = 0;
26
- expect(int2ip(ipInt)).toBe('0.0.0.0')
27
- });
28
-
29
- it('negative numbers should throw', () => {
30
- expect(() => int2ip(-1)).toThrow('require number as input');
31
- })
32
-
33
- });
@@ -1,9 +0,0 @@
1
- import net from 'net'
2
-
3
- //we only support ipv4
4
- export function int2ip (ipInt: number) : string {
5
- if (typeof ipInt !== 'number' || ipInt<0) throw new Error(`require number as input`)
6
- const ip4 = ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) )
7
- if (net.isIP(ip4) !== 4) throw new Error(`Invalid IP: ${ip4}`)
8
- return ip4
9
- }
@@ -1,30 +0,0 @@
1
- const { ip2int } = require('./ip2int');
2
- const { randomIP } = require('./randomIP');
3
-
4
- describe('ip2int', () => {
5
- it('should convert IP address to integer', () => {
6
- const ipAddress = '192.168.0.1';
7
- const result = ip2int(ipAddress);
8
- expect(result).toBe(3232235521);
9
- })
10
-
11
- it('invalid inputs', () => {
12
- expect(() => ip2int('')).toThrow()
13
- expect(() => ip2int('192.168.0')).toThrow()
14
- expect(() => ip2int(null)).toThrow()
15
- expect(() => ip2int(undefined)).toThrow()
16
- })
17
-
18
- it('should not support ipv6', () => {
19
- const ipAddress = '::';
20
- expect(() => ip2int(ipAddress)).toThrow()
21
- })
22
-
23
- it('test 100 random ips', () => {
24
- for (let i = 0; i < 100; i++) {
25
- const ipAddress = randomIP();
26
- const result = ip2int(ipAddress);
27
- expect(result).toBe(ip2int(ipAddress));
28
- }
29
- })
30
- });
@@ -1,10 +0,0 @@
1
- import net from 'net'
2
- // function int2ip (ipInt) {
3
- // return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
4
- // }
5
-
6
- // only supports ipv4
7
- export function ip2int(ip:string) {
8
- if (net.isIP(ip) !== 4) throw new Error(`Invalid IP: ${ip}`)
9
- return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
10
- }
@@ -1,59 +0,0 @@
1
- import { isLANIp } from './isLANIp';
2
-
3
- describe('isLANIp', () => {
4
- it('should return true for LAN IP addresses', () => {
5
- expect(isLANIp('192.168.0.1')).toBe(true);
6
- expect(isLANIp('10.0.0.1')).toBe(true);
7
- expect(isLANIp('172.16.0.1')).toBe(true);
8
-
9
- // Add more test cases for LAN IP addresses
10
- });
11
-
12
- it('should return false for non-LAN IP addresses', () => {
13
- expect(isLANIp('8.8.8.8')).toBe(false);
14
- expect(isLANIp('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(false);
15
- // Add more test cases for non-LAN IP addresses
16
- });
17
-
18
- it('should return false for invalid IP addresses', () => {
19
- expect(isLANIp('')).toBe(false);
20
- expect(isLANIp('192.168.0.')).toBe(false);
21
- expect(isLANIp('256.0.0.1')).toBe(false);
22
- // Add more test cases for invalid IP addresses
23
- });
24
-
25
- test('should return true for private IPv4', () => {
26
- const ip = '192.168.1.1';
27
- expect(isLANIp(ip)).toBe(true);
28
- });
29
-
30
- test('should return true for private IPv6', () => {
31
- const ip = 'fd00::';
32
- expect(isLANIp(ip)).toBe(true);
33
- });
34
-
35
- test('should return null for public IPv4', () => {
36
- const ip = '8.8.8.8';
37
- expect(isLANIp(ip)).toBe(false);
38
- });
39
-
40
- test('should return false for public IPv6', () => {
41
- const ip = '2001:4860:4860::8888';
42
- expect(isLANIp(ip)).toBe(false);
43
- });
44
-
45
- test('should return false for invalid IP', () => {
46
- const ip = 'invalid';
47
- expect(isLANIp(ip)).toBe(false);
48
- });
49
-
50
- test('should return false for invalid IP', () => {
51
- const ip = '1.2.3';
52
- expect(isLANIp(ip)).toBe(false);
53
- });
54
-
55
- test('should return null for non private IPv6', () => {
56
- const ip = '::2';
57
- expect(isLANIp(ip)).toBe(false);
58
- });
59
- });
@@ -1,37 +0,0 @@
1
- import ip6addr from '@7c/node-ip6addr'
2
- import net from 'net'
3
-
4
- export const private_network_cidrs = [
5
- ip6addr.createCIDR('10.0.0.0/8'),
6
- ip6addr.createCIDR('172.16.0.0/12'),
7
- ip6addr.createCIDR('192.168.0.0/16'),
8
- ip6addr.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
9
- ip6addr.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
10
- ]
11
-
12
- export function isLANIp(ip:string) : boolean {
13
- ip=normalizeIp(ip)
14
- if (net.isIP(ip)===0) return false
15
- try {
16
- // speed optimized
17
- let ipVersion = net.isIPv4(ip) ? 'ipv4' : 'ipv6'
18
- for (let cidr of private_network_cidrs) {
19
- if (!cidr) continue
20
-
21
- let first = cidr.first()
22
- if (first.kind() !== ipVersion) continue
23
- if (cidr.contains(ip))
24
- return true
25
- }
26
- } catch (err) {
27
- console.log(err)
28
- }
29
- return false
30
- }
31
-
32
- function normalizeIp(ip:string):string {
33
- // also support ipv6
34
- if (net.isIP(ip) === 0) return ''
35
- if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
36
- return ip
37
- }
@@ -1,16 +0,0 @@
1
- import { isLocal } from './isLocal';
2
- import os from 'os';
3
-
4
- describe('isLocal', () => {
5
- it('should return true on Darwin OS', () => {
6
- jest.spyOn(os, 'type').mockReturnValue('Darwin');
7
- const result = isLocal();
8
- expect(result).toBe(true);
9
- });
10
-
11
- it('should return false on non-Darwin OS', () => {
12
- jest.spyOn(os, 'type').mockReturnValue('Linux');
13
- const result = isLocal();
14
- expect(result).toBe(false);
15
- });
16
- });
@@ -1,5 +0,0 @@
1
- import os from 'os'
2
-
3
- export function isLocal() : boolean {
4
- return (os.type() === 'Darwin')
5
- }
@@ -1,72 +0,0 @@
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
- });
@@ -1,32 +0,0 @@
1
- import ip6addr from '@7c/node-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
- }
@@ -1,42 +0,0 @@
1
- import debug from "debug"
2
- import knex from "knex"
3
- const dbg = debug('_knexConnection')
4
-
5
- export function knexConnection(connection:{
6
- host: string,
7
- port: number,
8
- password: string,
9
- db: string,
10
- login: string
11
- }, poolConfiguration:knex.Knex.PoolConfig= {
12
- min: 1,
13
- max: 10
14
- }, acquireConnectionTimeout:number=10000, debug: boolean = false) {
15
- return knex({
16
- client: 'mysql2',
17
- debug,
18
- connection:{
19
- host: connection.host,
20
- port: connection.port,
21
- password: connection.password,
22
- database: connection.db,
23
- user: connection.login
24
- },
25
- pool: poolConfiguration,
26
- acquireConnectionTimeout,
27
- log: {
28
- warn(message: string) {
29
- dbg('knex warn', message)
30
- },
31
- error(message: string) {
32
- dbg('knex error', message)
33
- },
34
- deprecate(message: string) {
35
- dbg('knex deprecate', message)
36
- },
37
- debug(message: string) {
38
- if (debug) console.log('knex debug', message)
39
- },
40
- }
41
- } as knex.Knex.Config)
42
- }
@@ -1,104 +0,0 @@
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
- });
@@ -1,19 +0,0 @@
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
- }
@@ -1,16 +0,0 @@
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
- });
@@ -1,10 +0,0 @@
1
-
2
- export function randomIP() : string {
3
- function int2ip (ipInt: number) {
4
- return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
5
- }
6
- return int2ip(Math.random()*Math.pow(2,32))
7
- }
8
-
9
-
10
-
@@ -1,18 +0,0 @@
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
- });
@@ -1,12 +0,0 @@
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
-
@@ -1,15 +0,0 @@
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
- });
@@ -1,5 +0,0 @@
1
-
2
-
3
- export function randomString(length = 10, charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"): string {
4
- return Array.from({ length }, () => charSet.charAt(Math.floor(Math.random() * charSet.length))).join('');
5
- }
@@ -1,62 +0,0 @@
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
- })