mybase 1.2.2 → 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 (109) hide show
  1. package/mybase.js.map +1 -0
  2. package/package.json +16 -1
  3. package/ts/funcs/Geoip2Paths.js.map +1 -0
  4. package/ts/funcs/MaxRuntimeHours.js.map +1 -0
  5. package/ts/funcs/asJSON.js.map +1 -0
  6. package/ts/funcs/deepCopy.js.map +1 -0
  7. package/ts/funcs/ensureFolder.js.map +1 -0
  8. package/ts/funcs/fileCacheIsValid.js.map +1 -0
  9. package/ts/funcs/getMysql1.js.map +1 -0
  10. package/ts/funcs/getMysql2.js.map +1 -0
  11. package/ts/funcs/getWeekNumber.js.map +1 -0
  12. package/ts/funcs/hash_sha512.js.map +1 -0
  13. package/ts/funcs/initMysql2Pool.js.map +1 -0
  14. package/ts/funcs/int2ip.js.map +1 -0
  15. package/ts/funcs/ip2int.js.map +1 -0
  16. package/ts/funcs/isLANIp.js.map +1 -0
  17. package/ts/funcs/isLocal.js.map +1 -0
  18. package/ts/funcs/isLoopbackIP.js.map +1 -0
  19. package/ts/funcs/knexConnection.js.map +1 -0
  20. package/ts/funcs/promiseTimeout.js.map +1 -0
  21. package/ts/funcs/randomIP.js.map +1 -0
  22. package/ts/funcs/randomIP6.js.map +1 -0
  23. package/ts/funcs/randomString.js.map +1 -0
  24. package/ts/funcs/randomTCPPort.js.map +1 -0
  25. package/ts/funcs/randomUTFString.js.map +1 -0
  26. package/ts/funcs/utcnow.js.map +1 -0
  27. package/ts/funcs/validEmail.js.map +1 -0
  28. package/ts/funcs/validIp.js.map +1 -0
  29. package/ts/funcs/vaultFill.js.map +1 -0
  30. package/ts/funcs/vaultRead.js.map +1 -0
  31. package/ts/funcs/wait.js.map +1 -0
  32. package/ts/global.js.map +1 -0
  33. package/ts/index.js.map +1 -0
  34. package/ts/models/DateIterator.js.map +1 -0
  35. package/ts/models/IPAddress.js.map +1 -0
  36. package/ts/models/Interfaces.js.map +1 -0
  37. package/ts/models/OTPGenerator.js.map +1 -0
  38. package/ts/models/Timespan.js.map +1 -0
  39. package/ts/models/Unixtime.js.map +1 -0
  40. package/ts/types.js.map +1 -0
  41. package/jest/isLANIp.test.js +0 -36
  42. package/jest/isLoopbackIP.test.js +0 -31
  43. package/jest.config.js +0 -13
  44. package/mybase.test.ts +0 -647
  45. package/mybase.ts +0 -397
  46. package/ts/funcs/Geoip2Paths.test.ts +0 -11
  47. package/ts/funcs/Geoip2Paths.ts +0 -28
  48. package/ts/funcs/MaxRuntimeHours.ts +0 -7
  49. package/ts/funcs/asJSON.test.js +0 -72
  50. package/ts/funcs/asJSON.ts +0 -9
  51. package/ts/funcs/deepCopy.test.ts +0 -108
  52. package/ts/funcs/deepCopy.ts +0 -34
  53. package/ts/funcs/ensureFolder.test.ts +0 -79
  54. package/ts/funcs/ensureFolder.ts +0 -21
  55. package/ts/funcs/fileCacheIsValid.ts +0 -13
  56. package/ts/funcs/getMysql1.ts +0 -41
  57. package/ts/funcs/getMysql2.ts +0 -42
  58. package/ts/funcs/getWeekNumber.test.ts +0 -9
  59. package/ts/funcs/getWeekNumber.ts +0 -31
  60. package/ts/funcs/hash_sha512.ts +0 -6
  61. package/ts/funcs/initMysql2Pool.ts +0 -11
  62. package/ts/funcs/int2ip.test.js +0 -33
  63. package/ts/funcs/int2ip.ts +0 -9
  64. package/ts/funcs/ip2int.test.js +0 -30
  65. package/ts/funcs/ip2int.ts +0 -10
  66. package/ts/funcs/isLANIp.test.ts +0 -58
  67. package/ts/funcs/isLANIp.ts +0 -34
  68. package/ts/funcs/isLocal.test.ts +0 -16
  69. package/ts/funcs/isLocal.ts +0 -5
  70. package/ts/funcs/isLoopbackIP.test.ts +0 -72
  71. package/ts/funcs/isLoopbackIP.ts +0 -30
  72. package/ts/funcs/knexConnection.ts +0 -42
  73. package/ts/funcs/promiseTimeout.test.js +0 -104
  74. package/ts/funcs/promiseTimeout.ts +0 -19
  75. package/ts/funcs/randomIP.test.ts +0 -16
  76. package/ts/funcs/randomIP.ts +0 -10
  77. package/ts/funcs/randomIP6.test.js +0 -18
  78. package/ts/funcs/randomIP6.ts +0 -12
  79. package/ts/funcs/randomString.test.ts +0 -15
  80. package/ts/funcs/randomString.ts +0 -5
  81. package/ts/funcs/randomTCPPort.test.ts +0 -62
  82. package/ts/funcs/randomTCPPort.ts +0 -58
  83. package/ts/funcs/randomUTFString.test.ts +0 -44
  84. package/ts/funcs/randomUTFString.ts +0 -33
  85. package/ts/funcs/utcnow.ts +0 -5
  86. package/ts/funcs/validEmail.test.js +0 -31
  87. package/ts/funcs/validEmail.ts +0 -11
  88. package/ts/funcs/validIp.test.ts +0 -33
  89. package/ts/funcs/validIp.ts +0 -5
  90. package/ts/funcs/vaultFill.ts +0 -89
  91. package/ts/funcs/vaultRead.ts +0 -50
  92. package/ts/funcs/wait.ts +0 -7
  93. package/ts/global.ts +0 -10
  94. package/ts/index.test.ts +0 -7
  95. package/ts/index.ts +0 -39
  96. package/ts/models/DateIterator.test.ts +0 -149
  97. package/ts/models/DateIterator.ts +0 -80
  98. package/ts/models/IPAddress.test.ts +0 -393
  99. package/ts/models/IPAddress.ts +0 -272
  100. package/ts/models/Interfaces.ts +0 -58
  101. package/ts/models/OTPGenerator.test.ts +0 -42
  102. package/ts/models/OTPGenerator.ts +0 -43
  103. package/ts/models/Timespan.ts +0 -80
  104. package/ts/models/Unixtime.test.ts +0 -168
  105. package/ts/models/Unixtime.ts +0 -208
  106. package/ts/types.ts +0 -42
  107. package/tsconfig.jest.json +0 -11
  108. package/tsconfig.json +0 -37
  109. package/types/third-party.d.ts +0 -21
package/ts/index.ts DELETED
@@ -1,39 +0,0 @@
1
- export * from "./funcs/getWeekNumber"
2
- export * from "./funcs/Geoip2Paths";
3
- export * from "./funcs/isLocal"
4
- export * from "./funcs/randomString"
5
- export * from "./funcs/utcnow"
6
- export * from "./funcs/validIp"
7
- export * from "./funcs/wait"
8
- export * from "./funcs/fileCacheIsValid"
9
- export * from "./funcs/hash_sha512"
10
- export * from "./funcs/vaultRead"
11
- export * from "./funcs/vaultFill"
12
- export * from "./funcs/asJSON"
13
- export * from "./funcs/promiseTimeout"
14
- export * from "./funcs/randomIP"
15
- export * from "./funcs/ip2int"
16
- export * from "./funcs/int2ip"
17
- export * from "./funcs/randomIP6"
18
- export * from "./funcs/randomTCPPort"
19
- export * from "./funcs/validEmail"
20
- export * from "./funcs/isLoopbackIP"
21
- export * from "./funcs/isLANIp"
22
- export * from "./funcs/getMysql1"
23
- export * from "./funcs/getMysql2"
24
- export * from "./funcs/initMysql2Pool"
25
- export * from "./funcs/randomUTFString"
26
- export * from "./funcs/MaxRuntimeHours"
27
- export * from "./funcs/ensureFolder"
28
- export * from "./funcs/knexConnection"
29
- export * from "./funcs/deepCopy"
30
-
31
-
32
-
33
- // models
34
- export * from "./models/Unixtime"
35
- export * from "./models/Timespan"
36
- export * from "./models/IPAddress"
37
- export * from "./models/OTPGenerator"
38
- export * from "./models/Interfaces"
39
- export * from "./models/DateIterator"
@@ -1,149 +0,0 @@
1
- import { DateIterator } from './DateIterator';
2
- import { Unixtime } from './Unixtime';
3
-
4
- describe('DateIterator', () => {
5
- const start = () => Unixtime.fromYYYYMMDD('20240110');
6
- const end = () => Unixtime.fromYYYYMMDD('20240105');
7
-
8
- it('should iterate backwards day-by-day (end-exclusive) with default separator', () => {
9
- const it = new DateIterator(start(), end());
10
- expect(it.toArray()).toEqual([
11
- '20240110',
12
- '20240109',
13
- '20240108',
14
- '20240107',
15
- '20240106',
16
- ]);
17
- });
18
-
19
- it('should honor a custom separator', () => {
20
- const it = new DateIterator(start(), end(), '-');
21
- expect(it.toArray()).toEqual([
22
- '2024-01-10',
23
- '2024-01-09',
24
- '2024-01-08',
25
- '2024-01-07',
26
- '2024-01-06',
27
- ]);
28
- });
29
-
30
- it('should honor a slash separator', () => {
31
- const it = new DateIterator(start(), end(), '/');
32
- expect(it.toArray()).toEqual([
33
- '2024/01/10',
34
- '2024/01/09',
35
- '2024/01/08',
36
- '2024/01/07',
37
- '2024/01/06',
38
- ]);
39
- });
40
-
41
- it('next() should return null when exhausted', () => {
42
- const s = Unixtime.fromYYYYMMDD('20240103');
43
- const e = Unixtime.fromYYYYMMDD('20240101');
44
- const it = new DateIterator(s, e);
45
- expect(it.next()).toEqual('20240103');
46
- expect(it.next()).toEqual('20240102');
47
- expect(it.next()).toBeNull();
48
- expect(it.next()).toBeNull();
49
- });
50
-
51
- it('should return empty array when start equals end (end-exclusive)', () => {
52
- const s = Unixtime.fromYYYYMMDD('20240105');
53
- const e = Unixtime.fromYYYYMMDD('20240105');
54
- const it = new DateIterator(s, e);
55
- expect(it.toArray()).toEqual([]);
56
- expect(it.next()).toBeNull();
57
- });
58
-
59
- it('should throw when start is older than end', () => {
60
- const s = Unixtime.fromYYYYMMDD('20240101');
61
- const e = Unixtime.fromYYYYMMDD('20240110');
62
- expect(() => new DateIterator(s, e)).toThrow(
63
- 'DateIterator: `start` must be newer than or equal to `end`'
64
- );
65
- });
66
-
67
- it('should not mutate the caller-supplied Unixtime instances', () => {
68
- const s = Unixtime.fromYYYYMMDD('20240110');
69
- const e = Unixtime.fromYYYYMMDD('20240105');
70
- const sBefore = s.toLongUnixtime();
71
- const eBefore = e.toLongUnixtime();
72
- const it = new DateIterator(s, e);
73
- it.toArray();
74
- expect(s.toLongUnixtime()).toEqual(sBefore);
75
- expect(e.toLongUnixtime()).toEqual(eBefore);
76
- });
77
-
78
- it('reset() should allow the iterator to be consumed again', () => {
79
- const it = new DateIterator(start(), end());
80
- const first = it.toArray();
81
- expect(it.next()).toBeNull();
82
- it.reset();
83
- const second = it.toArray();
84
- expect(second).toEqual(first);
85
- });
86
-
87
- it('reset() should return the iterator for chaining', () => {
88
- const it = new DateIterator(start(), end());
89
- expect(it.reset()).toBe(it);
90
- });
91
-
92
- it('should be iterable via for..of', () => {
93
- const it = new DateIterator(start(), end(), '-');
94
- const out: string[] = [];
95
- for (const d of it) out.push(d);
96
- expect(out).toEqual([
97
- '2024-01-10',
98
- '2024-01-09',
99
- '2024-01-08',
100
- '2024-01-07',
101
- '2024-01-06',
102
- ]);
103
- });
104
-
105
- it('for..of should be independent of next()/toArray() cursor', () => {
106
- const it = new DateIterator(start(), end());
107
- expect(it.next()).toEqual('20240110');
108
- const collected: string[] = [];
109
- for (const d of it) collected.push(d);
110
- expect(collected).toEqual([
111
- '20240110',
112
- '20240109',
113
- '20240108',
114
- '20240107',
115
- '20240106',
116
- ]);
117
- expect(it.next()).toEqual('20240109');
118
- });
119
-
120
- it('should support spread syntax', () => {
121
- const it = new DateIterator(
122
- Unixtime.fromYYYYMMDD('20240103'),
123
- Unixtime.fromYYYYMMDD('20240101')
124
- );
125
- expect([...it]).toEqual(['20240103', '20240102']);
126
- });
127
-
128
- it('should cross month boundaries correctly', () => {
129
- const s = Unixtime.fromYYYYMMDD('20240302');
130
- const e = Unixtime.fromYYYYMMDD('20240227');
131
- const it = new DateIterator(s, e, '-');
132
- expect(it.toArray()).toEqual([
133
- '2024-03-02',
134
- '2024-03-01',
135
- '2024-02-29',
136
- '2024-02-28',
137
- ]);
138
- });
139
-
140
- it('should produce N entries for a span of N+1 days (end-exclusive)', () => {
141
- const s = Unixtime.fromYYYYMMDD('20240620');
142
- const e = Unixtime.fromYYYYMMDD('20240610');
143
- const it = new DateIterator(s, e);
144
- const arr = it.toArray();
145
- expect(arr.length).toEqual(10);
146
- expect(arr[0]).toEqual('20240620');
147
- expect(arr[arr.length - 1]).toEqual('20240611');
148
- });
149
- });
@@ -1,80 +0,0 @@
1
- import { Unixtime } from "./Unixtime"
2
-
3
- /**
4
- * Iterates day-by-day backwards from `start` toward `end` (end-exclusive),
5
- * emitting each date as a `yyyy{sep}mm{sep}dd` string.
6
- *
7
- * @example
8
- * const it = new DateIterator(Unixtime.now(), Unixtime.now().addDays(-5))
9
- * it.next() // "20260417"
10
- * it.next() // "20260416"
11
- * // ...
12
- * it.next() // null
13
- *
14
- * @example separator
15
- * new DateIterator(start, end, "-").toArray()
16
- * // ["2026-04-17", "2026-04-16", ...]
17
- *
18
- * @example for..of
19
- * for (const d of new DateIterator(start, end, "/")) console.log(d)
20
- */
21
- export class DateIterator implements Iterable<string> {
22
- private readonly start: Unixtime
23
- private readonly end: Unixtime
24
- private readonly separator: string
25
- private cursor: Unixtime | null
26
-
27
- constructor(start: Unixtime, end: Unixtime, separator: string = "") {
28
- if (start.lessThan(end)) {
29
- throw new Error('DateIterator: `start` must be newer than or equal to `end`')
30
- }
31
- // Defensive clones: callers' instances must not be mutated, and
32
- // Unixtime.addDays() mutates the underlying Date.
33
- this.start = start.clone()
34
- this.end = end.clone()
35
- this.separator = separator
36
- this.cursor = this.start.clone()
37
- }
38
-
39
- /** Returns the next formatted date going backwards, or `null` when exhausted. */
40
- next(): string | null {
41
- if (this.cursor === null) return null
42
- if (!this.cursor.greaterThan(this.end)) {
43
- this.cursor = null
44
- return null
45
- }
46
- const value = this.cursor.yyyymmdd(this.separator)
47
- this.cursor.addDays(-1)
48
- return value
49
- }
50
-
51
- /** Reset the iterator so it can be consumed again from `start`. */
52
- reset(): this {
53
- this.cursor = this.start.clone()
54
- return this
55
- }
56
-
57
- /** Collect all remaining dates into an array. */
58
- toArray(): string[] {
59
- const out: string[] = []
60
- let d: string | null
61
- while ((d = this.next()) !== null) out.push(d)
62
- return out
63
- }
64
-
65
- [Symbol.iterator](): Iterator<string> {
66
- let cursor: Unixtime | null = this.start.clone()
67
- const end = this.end
68
- const sep = this.separator
69
- return {
70
- next(): IteratorResult<string> {
71
- if (cursor === null || !cursor.greaterThan(end)) {
72
- return { value: undefined as unknown as string, done: true }
73
- }
74
- const value = cursor.yyyymmdd(sep)
75
- cursor.addDays(-1)
76
- return { value, done: false }
77
- }
78
- }
79
- }
80
- }
@@ -1,393 +0,0 @@
1
- import ip6addr from '@7c/node-ip6addr'
2
- import { IPAddress } from './IPAddress'
3
- import { randomIP, randomIP6 } from '../'
4
-
5
- describe('IPAddress', () => {
6
- it('should create an instance of IPAddress', () => {
7
- const ipAddress = new IPAddress('192.168.0.1');
8
- expect(ipAddress).toBeInstanceOf(IPAddress);
9
- });
10
-
11
-
12
- it('should convert IPAddress to string', () => {
13
- const ipAddress = new IPAddress('192.168.0.1');
14
- const result = ipAddress.toString();
15
- expect(result).toBe('192.168.0.1');
16
- });
17
-
18
- it('should create an instance of IPAddress from string', () => {
19
- const ipAddress = IPAddress.fromString('192.168.0.1');
20
- expect(ipAddress).toBeInstanceOf(IPAddress);
21
- });
22
-
23
- it('should throw an error when creating an instance of IPAddress from invalid string', () => {
24
- expect(() => IPAddress.fromString('192.168.0')).toThrow()
25
- expect(() => IPAddress.fromString('0.0.0')).toThrow()
26
- expect(() => IPAddress.fromString('')).toThrow()
27
- //@ts-ignore
28
- expect(() => IPAddress.fromString(null)).toThrow()
29
- //@ts-ignore
30
- expect(() => IPAddress.fromString(undefined)).toThrow()
31
- })
32
-
33
- it('should serialize ipv4 to string', () => {
34
- expect(IPAddress.fromString('192.168.0.1').serialize()).toBe('192168000001')
35
- expect(IPAddress.fromString('0.0.0.0').serialize()).toBe('000000000000')
36
- expect(IPAddress.fromString('0.0.0.1').serialize()).toBe('000000000001')
37
- expect(IPAddress.fromString('0.00.000.01').serialize()).toBe('000000000001')
38
- expect(IPAddress.fromString('0.10.0.0').serialize()).toBe('000010000000')
39
- expect(IPAddress.fromString('255.255.255.255').serialize()).toBe('255255255255')
40
- });
41
-
42
- it('should serialize ipv6 to string', () => {
43
- expect(IPAddress.fromString('::').serialize()).toBe('00000000000000000000000000000000')
44
- expect(IPAddress.fromString('::1').serialize()).toBe('00000000000000000000000000000001')
45
- expect(IPAddress.fromString('::01').serialize()).toBe('00000000000000000000000000000001')
46
- expect(IPAddress.fromString('::001').serialize()).toBe('00000000000000000000000000000001')
47
- expect(IPAddress.fromString('::0001').serialize()).toBe('00000000000000000000000000000001')
48
- expect(IPAddress.fromString('::1:1').serialize()).toBe('00000000000000000000000000010001')
49
- expect(IPAddress.fromString('::2:1').serialize()).toBe('00000000000000000000000000020001')
50
- })
51
-
52
- // testing unserialize
53
- it('should unserialize ipv4 from string', () => {
54
- expect(IPAddress.unserialize('192168000001').toString()).toBe('192.168.0.1')
55
- expect(IPAddress.unserialize('192168000002').toString()).toBe('192.168.0.2')
56
- expect(IPAddress.unserialize('000000000000').toString()).toBe('0.0.0.0')
57
- })
58
-
59
- it('should throw invalid serialization string', () => {
60
- expect(() => IPAddress.unserialize('92168000001')).toThrow(Error)
61
- expect(() => IPAddress.unserialize('168000001')).toThrow(Error)
62
- expect(() => IPAddress.unserialize('292168000002')).toThrow(Error)
63
- })
64
-
65
- it('randomly generated ipv4 serialization', () => {
66
- for (let i = 0; i < 100; i++) {
67
- const ip = randomIP()
68
- expect(IPAddress.unserialize(IPAddress.fromString(ip).serialize()).toString()).toBe(ip)
69
- }
70
- })
71
-
72
- it('randomly generated ipv6 serialization and unserialization', () => {
73
- for (let i = 0; i < 100; i++) {
74
- const ip = randomIP6()
75
- let ip2 = IPAddress.unserialize(IPAddress.fromString(ip).serialize()).toString()
76
- expect(ip6addr.compare(ip, ip2)).toEqual(0)
77
- }
78
- })
79
-
80
- it('it should detect the kind of ip4', () => {
81
- for (let i = 0; i < 100; i++)
82
- expect(IPAddress.randomIP4().kind()).toBe(4)
83
- })
84
-
85
- it('should return shortened address by default toString', () => {
86
- expect(new IPAddress('001.02.3.004').toString()).toBe('1.2.3.4');
87
- });
88
-
89
- it('randomPad return valid ipv4 results', () => {
90
- let compared = 0
91
- while (compared < 100) {
92
- const ip1 = IPAddress.randomIP4()
93
- const ip2 = ip1.randomPad()
94
- if (ip1.toString() !== ip2) { // find ips they are padded from the original
95
- compared++
96
- }
97
- expect(IPAddress.validate(ip2)).toBe(true)
98
- }
99
- })
100
-
101
- it('compare ipv4 should detect equality even if they are padded', () => {
102
- let compared = 0
103
- while (compared < 100) {
104
- const ip1 = IPAddress.randomIP4()
105
- const ip2 = ip1.randomPad()
106
- if (ip1.toString() !== ip2) { // find ips they are padded from the original
107
- compared++
108
- }
109
- expect(ip1.equals(IPAddress.fromString(ip2))).toBe(true)
110
- }
111
- })
112
-
113
- it('normalized should normalize ipv4 ips properly', () => {
114
- expect(new IPAddress('127.0.0.1').normalized()).toBe('127.0.0.1')
115
- expect(new IPAddress('127.00.0.1').normalized()).toBe('127.0.0.1')
116
- expect(new IPAddress('127.000.00.1').normalized()).toBe('127.0.0.1')
117
- expect(new IPAddress('127.0.00.00001').normalized()).toBe('127.0.0.1')
118
- })
119
-
120
- it('normalized should normalize ipv6 ips properly', () => {
121
- expect(new IPAddress('::1').normalized()).toBe('0000:0000:0000:0000:0000:0000:0000:0001')
122
- expect(new IPAddress('::01').normalized()).toBe('0000:0000:0000:0000:0000:0000:0000:0001')
123
- expect(new IPAddress('::001').normalized()).toBe('0000:0000:0000:0000:0000:0000:0000:0001')
124
- expect(new IPAddress('::0001').normalized()).toBe('0000:0000:0000:0000:0000:0000:0000:0001')
125
- expect(new IPAddress('::000001').normalized()).toBe('0000:0000:0000:0000:0000:0000:0000:0001')
126
- expect(new IPAddress('::1:1').normalized()).toBe('0000:0000:0000:0000:0000:0000:0001:0001')
127
- expect(new IPAddress('::2:1').normalized()).toBe('0000:0000:0000:0000:0000:0000:0002:0001')
128
-
129
-
130
- expect(new IPAddress('1::1').normalized()).toBe('0001:0000:0000:0000:0000:0000:0000:0001')
131
- expect(new IPAddress('2::01').normalized()).toBe('0002:0000:0000:0000:0000:0000:0000:0001')
132
- expect(new IPAddress('3::001').normalized()).toBe('0003:0000:0000:0000:0000:0000:0000:0001')
133
- expect(new IPAddress('4::0001').normalized()).toBe('0004:0000:0000:0000:0000:0000:0000:0001')
134
- expect(new IPAddress('5::000001').normalized()).toBe('0005:0000:0000:0000:0000:0000:0000:0001')
135
- expect(new IPAddress('6::1:1').normalized()).toBe('0006:0000:0000:0000:0000:0000:0001:0001')
136
- expect(new IPAddress('7::2:1').normalized()).toBe('0007:0000:0000:0000:0000:0000:0002:0001')
137
- })
138
-
139
-
140
- it('abbreviated should abbreviate ipv6 ips properly', () => {
141
- expect(new IPAddress('::1').abbreviated()).toBe('::1')
142
- expect(new IPAddress('::01').abbreviated()).toBe('::1')
143
- expect(new IPAddress('::001').abbreviated()).toBe('::1')
144
- expect(new IPAddress('::0001').abbreviated()).toBe('::1')
145
- expect(new IPAddress('::000001').abbreviated()).toBe('::1')
146
- expect(new IPAddress('0000:0000:0000:0000:0000:0000:0001:0001').abbreviated()).toBe('::1:1')
147
- expect(new IPAddress('0000:0000:0000:0000:0000:0000:0002:0001').abbreviated()).toBe('::2:1')
148
-
149
- expect(new IPAddress('2001:0db8:0001:0002:0003:0004:0005:0006').abbreviated()).toBe('2001:db8:1:2:3:4:5:6')
150
- expect(new IPAddress('1:0000:0000::0000:0000:0000:0001').abbreviated()).toBe('1::1')
151
- expect(new IPAddress('0001:0000:0000:0000:0000:0000:0000:0001').abbreviated()).toBe('1::1')
152
- expect(new IPAddress('0002:0000:0000:0000:0000::0000:0001').abbreviated()).toBe('2::1')
153
- expect(new IPAddress('0003:0000:0000:0000:0000:0000:0000:0001').abbreviated()).toBe('3::1')
154
- expect(new IPAddress('0004::0000:0000:0000:0000:0000:0001').abbreviated()).toBe('4::1')
155
- expect(new IPAddress('0005:0000:0000:0000:0000:0000:0000:0001').abbreviated()).toBe('5::1')
156
- expect(new IPAddress('0006:0000:0000::0000:0000:0001:0001').abbreviated()).toBe('6::1:1')
157
- expect(new IPAddress('0007:0000:0000:0000:0000::0002:0001').abbreviated()).toBe('7::2:1')
158
- })
159
-
160
- it('abbreviated should abbreviate ipv4 ips properly', () => {
161
- expect(new IPAddress('127.00.000.1').abbreviated()).toBe('127.0.0.1')
162
- expect(new IPAddress('127.0.00.00001').abbreviated()).toBe('127.0.0.1')
163
- expect(new IPAddress('127.0000.0.00001').abbreviated()).toBe('127.0.0.1')
164
- expect(new IPAddress('0.0000.0.00000').abbreviated()).toBe('0.0.0.0')
165
- })
166
- it('abbreviated should abbreviate ipv4-mapped ips properly', () => {
167
- expect(new IPAddress('::ffff:127.00.000.1').abbreviated()).toBe('127.0.0.1')
168
- expect(new IPAddress('::ffff:127.0.00.00001').abbreviated()).toBe('127.0.0.1')
169
- expect(new IPAddress('::ffff:127.0000.0.00001').abbreviated()).toBe('127.0.0.1')
170
- expect(new IPAddress('0::ffff:127.0000.0.00001').abbreviated()).toBe('127.0.0.1')
171
- expect(new IPAddress('0:0:0::ffff:127.0000.0.00001').abbreviated()).toBe('127.0.0.1')
172
- expect(new IPAddress('::ffff:0.0000.0.00000').abbreviated()).toBe('0.0.0.0')
173
- })
174
-
175
- it('toString() IPv4-mapped IPv6 address should be returned as ipv4', () => {
176
- expect(new IPAddress('::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
177
- expect(new IPAddress('0::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
178
- expect(new IPAddress('00::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
179
- expect(new IPAddress('0:00::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
180
- expect(new IPAddress('0:0:0::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
181
- })
182
-
183
- it('kind() IPv4-mapped IPv6 address should be returned as ipv4', () => {
184
- expect(new IPAddress('::ffff:127.0.00.00001').kind()).toBe(4)
185
- expect(new IPAddress('0::ffff:127.0.00.00001').kind()).toBe(4)
186
- expect(new IPAddress('00::ffff:127.0.00.00001').kind()).toBe(4)
187
- expect(new IPAddress('0:00::ffff:127.0.00.00001').kind()).toBe(4)
188
- expect(new IPAddress('0:0:0::ffff:127.0.00.00001').kind()).toBe(4)
189
- })
190
-
191
- it('IPv4-mapps Addresses should be converted to Ipv4 at constructor', () => {
192
- expect(new IPAddress('::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
193
- expect(new IPAddress('0::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
194
- expect(new IPAddress('00::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
195
- expect(new IPAddress('0:00::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
196
- expect(new IPAddress('0:0:0::ffff:127.0.00.00001').toString()).toBe('127.0.0.1')
197
- })
198
-
199
- });
200
-
201
- describe('IPAddress IPv6', () => {
202
- it('should work with any form of valid ipv6, prefixed or not', () => {
203
- expect(new IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress);
204
- expect(new IPAddress('2605:2700:0000:003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress);
205
- expect(new IPAddress('2605:2700:000:003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress);
206
- expect(new IPAddress('2605:2700:0:003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress);
207
- expect(new IPAddress('2605:2700:0:003:a800:0ff:fe10:f0d3')).toBeInstanceOf(IPAddress);
208
- })
209
-
210
- it('should return shortened address by default toString', () => {
211
- expect(new IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
212
- expect(new IPAddress('2605:2700:0000:003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
213
- expect(new IPAddress('2605:2700:0000:03:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
214
- expect(new IPAddress('2605:2700:0000:03:a800:ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
215
- });
216
-
217
- it('ip6 addresses should be lowercased', () => {
218
- expect(new IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
219
- expect(new IPAddress('2605:2700:0000:003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
220
- expect(new IPAddress('2605:2700:0000:03:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
221
- expect(new IPAddress('2605:2700:0000:03:a800:ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
222
- });
223
-
224
- it('it should detect the kind of ip6', () => {
225
- expect(new IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3').kind()).toBe(6);
226
- for (let i = 0; i < 100; i++)
227
- expect(IPAddress.randomIP6().kind()).toBe(6)
228
- })
229
-
230
- it('randomPad return valid ipv6 results be compareable with equals', () => {
231
- let compared = 0
232
- while (compared < 500) {
233
- const ip1 = IPAddress.randomIP6()
234
- const ip2 = ip1.randomPad()
235
- expect(ip1.kind()).toBe(6)
236
- if (ip1.toString() !== ip2) { // find ips they are padded from the original
237
- compared++
238
- }
239
-
240
- expect(IPAddress.validate(ip2)).toBe(true)
241
- expect(ip1.equals(IPAddress.fromString(ip2))).toBe(true)
242
- }
243
- })
244
-
245
- })
246
-
247
-
248
-
249
- describe('IPAddress asciiChecksum', () => {
250
- it('should return the correct checksum for ipv4', () => {
251
- expect(new IPAddress('1.2.3.4').asciiChecksum()).toBe('e')
252
- expect(new IPAddress('1.2.3.6').asciiChecksum()).toBe('i')
253
- })
254
-
255
- it('should return the correct checksum for ipv6', () => {
256
- expect(new IPAddress('::').asciiChecksum()).toBe('a')
257
- expect(new IPAddress('::1').asciiChecksum()).toBe('a')
258
- expect(new IPAddress('::f').asciiChecksum()).toBe('s')
259
- })
260
-
261
- it('it should always return a single character ipv4/v6', () => {
262
- for (let i = 0; i < 10000; i++) {
263
- expect(new IPAddress(randomIP()).asciiChecksum().length).toBe(1)
264
- expect(new IPAddress(randomIP6()).asciiChecksum().length).toBe(1)
265
- }
266
- })
267
-
268
- it('it should return a-z0-9 ascii only', () => {
269
- for (let i = 0; i < 10000; i++) {
270
- expect('abcdefghijklmnopqrstuvwxyz0123456789').toContain(new IPAddress(randomIP()).asciiChecksum())
271
- expect('abcdefghijklmnopqrstuvwxyz0123456789').toContain(new IPAddress(randomIP6()).asciiChecksum())
272
- }
273
- })
274
- })
275
-
276
- describe('IPAddress partOfCIDR', () => {
277
- it('should detect cidrs in ipv4', () => {
278
- expect(IPAddress.partOfCIDR('127.0.0.1', [ip6addr.createCIDR('127.0.0.0/8')])).toBe(true)
279
- expect(IPAddress.partOfCIDR('127.0.0.1', [ip6addr.createCIDR('128.0.0.0/8')])).toBe(false)
280
- expect(IPAddress.partOfCIDR('127.0.0.1', [ip6addr.createCIDR('126.0.0.0/8')])).toBe(false)
281
- })
282
- it('should detect cidrs in ipv6', () => {
283
- expect(IPAddress.partOfCIDR('::1', [ip6addr.createCIDR('::/8')])).toBe(true)
284
- expect(IPAddress.partOfCIDR('::1', [ip6addr.createCIDR('2ff0::1:0/8')])).toBe(false)
285
- expect(IPAddress.partOfCIDR('::1', [ip6addr.createCIDR('3ff0::2:0/8')])).toBe(false)
286
- })
287
-
288
- it('should also work non-static', () => {
289
- expect(new IPAddress('::1').partOfCIDR([ip6addr.createCIDR('::/8')])).toBe(true)
290
- })
291
- })
292
-
293
- describe('IPAddress randomCIDRIp', () => {
294
- const testCount = 25000
295
-
296
- it('"loopback" should return random loopback ip4/6', () => {
297
- const stats:any={
298
- '4':0,
299
- '6':0
300
- }
301
- for(var i=0;i<testCount;i++) {
302
- const rip = IPAddress.randomCIDRIp('loopback')
303
- stats[rip.kind().toString()]++
304
- expect(rip.isLoopbackIP()).toBe(true)
305
- }
306
- // we need to have at least 30% of ipv4 and ipv6
307
- expect(stats['4']).toBeGreaterThan(testCount*0.3)
308
- expect(stats['6']).toBeGreaterThan(testCount*0.3)
309
- })
310
-
311
- it('"loopback4" should return random loopback ip4', () => {
312
- for(var i=0;i<testCount;i++) {
313
- const rip = IPAddress.randomCIDRIp('loopback4')
314
- expect(rip.kind()).toBe(4)
315
- expect(rip.isLoopbackIP()).toBe(true)
316
- }
317
- })
318
-
319
-
320
- it('"loopback6" should return random loopback ip6', () => {
321
- for(var i=0;i<testCount;i++) {
322
- const rip = IPAddress.randomCIDRIp('loopback6')
323
- expect(rip.kind()).toBe(6)
324
- expect(rip.isLoopbackIP()).toBe(true)
325
- // ipv6 has single loopback address
326
- expect(rip.toString()).toBe('::1')
327
- }
328
- })
329
-
330
- it('"lan" should return random LAN ip4/6', () => {
331
- const stats:any={
332
- '4':0,
333
- '6':0
334
- }
335
- for(var i=0;i<testCount;i++) {
336
- const rip = IPAddress.randomCIDRIp('lan')
337
- stats[rip.kind().toString()]++
338
- // if (!rip.isLANIP()) console.log(rip.toString(),`must be lan but it is not`)
339
- expect(rip.isLANIP()).toBe(true)
340
- }
341
- // we need to have at least 30% of ipv4 and ipv6
342
- expect(stats['4']).toBeGreaterThan(testCount*0.3)
343
- expect(stats['6']).toBeGreaterThan(testCount*0.3)
344
- })
345
-
346
-
347
- it('"lan6" should return random lan ip6', () => {
348
- for(var i=0;i<testCount;i++) {
349
- const rip = IPAddress.randomCIDRIp('lan6')
350
- expect(rip.kind()).toBe(6)
351
- expect(rip.isLANIP()).toBe(true)
352
- }
353
- })
354
-
355
- it('"lan4" should return random lan ip4', () => {
356
- for(var i=0;i<testCount;i++) {
357
- const rip = IPAddress.randomCIDRIp('lan4')
358
- expect(rip.kind()).toBe(4)
359
- expect(rip.isLANIP()).toBe(true)
360
- }
361
- })
362
-
363
- it('"local" should return random local ip4/6', () => {
364
- const stats:any={
365
- '4':0,
366
- '6':0
367
- }
368
- for(var i=0;i<testCount;i++) {
369
- const rip = IPAddress.randomCIDRIp('local')
370
- stats[rip.kind().toString()]++
371
- expect(rip.isLocalIP()).toBe(true)
372
- }
373
- // we need to have at least 30% of ipv4 and ipv6
374
- expect(stats['4']).toBeGreaterThan(testCount*0.3)
375
- expect(stats['6']).toBeGreaterThan(testCount*0.3)
376
- })
377
-
378
- it('"local4" should return random local ip4', () => {
379
- for(var i=0;i<testCount;i++) {
380
- const rip = IPAddress.randomCIDRIp('local4')
381
- expect(rip.kind()).toBe(4)
382
- expect(rip.isLocalIP()).toBe(true)
383
- }
384
- })
385
-
386
- it('"local6" should return random local ip6', () => {
387
- for(var i=0;i<testCount;i++) {
388
- const rip = IPAddress.randomCIDRIp('local6')
389
- expect(rip.kind()).toBe(6)
390
- expect(rip.isLocalIP()).toBe(true)
391
- }
392
- })
393
- })