mybase 1.1.33 → 1.1.34

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/package.json +5 -1
  2. package/jest/isLANIp.test.js +0 -36
  3. package/jest/isLoopbackIP.test.js +0 -31
  4. package/jest.config.js +0 -6
  5. package/ts/funcs/Geoip2Paths.d.ts +0 -7
  6. package/ts/funcs/Geoip2Paths.js +0 -26
  7. package/ts/funcs/Geoip2Paths.test.ts +0 -11
  8. package/ts/funcs/Geoip2Paths.ts +0 -26
  9. package/ts/funcs/MaxRuntimeHours.d.ts +0 -1
  10. package/ts/funcs/MaxRuntimeHours.js +0 -11
  11. package/ts/funcs/MaxRuntimeHours.ts +0 -7
  12. package/ts/funcs/asJSON.d.ts +0 -1
  13. package/ts/funcs/asJSON.js +0 -14
  14. package/ts/funcs/asJSON.test.js +0 -72
  15. package/ts/funcs/asJSON.ts +0 -6
  16. package/ts/funcs/ensureFolder.d.ts +0 -3
  17. package/ts/funcs/ensureFolder.js +0 -50
  18. package/ts/funcs/ensureFolder.test.ts +0 -79
  19. package/ts/funcs/ensureFolder.ts +0 -21
  20. package/ts/funcs/fileCacheIsValid.d.ts +0 -1
  21. package/ts/funcs/fileCacheIsValid.js +0 -40
  22. package/ts/funcs/fileCacheIsValid.ts +0 -13
  23. package/ts/funcs/getMysql1.d.ts +0 -8
  24. package/ts/funcs/getMysql1.js +0 -62
  25. package/ts/funcs/getMysql1.ts +0 -41
  26. package/ts/funcs/getMysql2.d.ts +0 -9
  27. package/ts/funcs/getMysql2.js +0 -62
  28. package/ts/funcs/getMysql2.ts +0 -42
  29. package/ts/funcs/getWeekNumber.d.ts +0 -1
  30. package/ts/funcs/getWeekNumber.js +0 -36
  31. package/ts/funcs/getWeekNumber.test.ts +0 -9
  32. package/ts/funcs/getWeekNumber.ts +0 -31
  33. package/ts/funcs/hash_sha512.d.ts +0 -1
  34. package/ts/funcs/hash_sha512.js +0 -11
  35. package/ts/funcs/hash_sha512.ts +0 -7
  36. package/ts/funcs/initMysql2Pool.d.ts +0 -3
  37. package/ts/funcs/initMysql2Pool.js +0 -39
  38. package/ts/funcs/initMysql2Pool.ts +0 -11
  39. package/ts/funcs/int2ip.d.ts +0 -1
  40. package/ts/funcs/int2ip.js +0 -18
  41. package/ts/funcs/int2ip.test.js +0 -33
  42. package/ts/funcs/int2ip.ts +0 -9
  43. package/ts/funcs/ip2int.d.ts +0 -1
  44. package/ts/funcs/ip2int.js +0 -18
  45. package/ts/funcs/ip2int.test.js +0 -30
  46. package/ts/funcs/ip2int.ts +0 -10
  47. package/ts/funcs/isLANIp.d.ts +0 -4
  48. package/ts/funcs/isLANIp.js +0 -47
  49. package/ts/funcs/isLANIp.test.ts +0 -59
  50. package/ts/funcs/isLANIp.ts +0 -37
  51. package/ts/funcs/isLocal.d.ts +0 -1
  52. package/ts/funcs/isLocal.js +0 -12
  53. package/ts/funcs/isLocal.test.ts +0 -16
  54. package/ts/funcs/isLocal.ts +0 -5
  55. package/ts/funcs/isLoopbackIP.d.ts +0 -4
  56. package/ts/funcs/isLoopbackIP.js +0 -42
  57. package/ts/funcs/isLoopbackIP.test.ts +0 -72
  58. package/ts/funcs/isLoopbackIP.ts +0 -32
  59. package/ts/funcs/promiseTimeout.d.ts +0 -1
  60. package/ts/funcs/promiseTimeout.js +0 -19
  61. package/ts/funcs/promiseTimeout.test.js +0 -104
  62. package/ts/funcs/promiseTimeout.ts +0 -19
  63. package/ts/funcs/randomIP.d.ts +0 -1
  64. package/ts/funcs/randomIP.js +0 -11
  65. package/ts/funcs/randomIP.test.ts +0 -16
  66. package/ts/funcs/randomIP.ts +0 -10
  67. package/ts/funcs/randomIP6.d.ts +0 -1
  68. package/ts/funcs/randomIP6.js +0 -17
  69. package/ts/funcs/randomIP6.test.js +0 -18
  70. package/ts/funcs/randomIP6.ts +0 -12
  71. package/ts/funcs/randomString.d.ts +0 -1
  72. package/ts/funcs/randomString.js +0 -8
  73. package/ts/funcs/randomString.test.ts +0 -15
  74. package/ts/funcs/randomString.ts +0 -5
  75. package/ts/funcs/randomTCPPort.d.ts +0 -12
  76. package/ts/funcs/randomTCPPort.js +0 -88
  77. package/ts/funcs/randomTCPPort.test.ts +0 -62
  78. package/ts/funcs/randomTCPPort.ts +0 -58
  79. package/ts/funcs/randomUTFString.d.ts +0 -1
  80. package/ts/funcs/randomUTFString.js +0 -31
  81. package/ts/funcs/randomUTFString.test.ts +0 -44
  82. package/ts/funcs/randomUTFString.ts +0 -33
  83. package/ts/funcs/utcnow.d.ts +0 -2
  84. package/ts/funcs/utcnow.js +0 -8
  85. package/ts/funcs/utcnow.ts +0 -5
  86. package/ts/funcs/validEmail.d.ts +0 -1
  87. package/ts/funcs/validEmail.js +0 -21
  88. package/ts/funcs/validEmail.test.js +0 -31
  89. package/ts/funcs/validEmail.ts +0 -13
  90. package/ts/funcs/validIp.d.ts +0 -1
  91. package/ts/funcs/validIp.js +0 -32
  92. package/ts/funcs/validIp.test.ts +0 -33
  93. package/ts/funcs/validIp.ts +0 -5
  94. package/ts/funcs/vaultFill.d.ts +0 -2
  95. package/ts/funcs/vaultFill.js +0 -86
  96. package/ts/funcs/vaultFill.ts +0 -89
  97. package/ts/funcs/vaultRead.d.ts +0 -2
  98. package/ts/funcs/vaultRead.js +0 -52
  99. package/ts/funcs/vaultRead.ts +0 -45
  100. package/ts/funcs/wait.d.ts +0 -1
  101. package/ts/funcs/wait.js +0 -10
  102. package/ts/funcs/wait.ts +0 -7
  103. package/ts/global.d.ts +0 -1
  104. package/ts/global.js +0 -16
  105. package/ts/global.ts +0 -10
  106. package/ts/index.d.ts +0 -30
  107. package/ts/index.js +0 -48
  108. package/ts/index.test.ts +0 -7
  109. package/ts/index.ts +0 -33
  110. package/ts/models/IPAddress.d.ts +0 -40
  111. package/ts/models/IPAddress.js +0 -254
  112. package/ts/models/IPAddress.test.ts +0 -393
  113. package/ts/models/IPAddress.ts +0 -271
  114. package/ts/models/Timespan.d.ts +0 -18
  115. package/ts/models/Timespan.js +0 -52
  116. package/ts/models/Timespan.ts +0 -63
  117. package/ts/models/Unixtime.d.ts +0 -32
  118. package/ts/models/Unixtime.js +0 -137
  119. package/ts/models/Unixtime.test.ts +0 -102
  120. package/ts/models/Unixtime.ts +0 -164
  121. package/ts/types.d.ts +0 -1
  122. package/ts/types.js +0 -3
  123. package/ts/types.ts +0 -1
  124. package/tsconfig.json +0 -36
@@ -1,393 +0,0 @@
1
- import ip6addr from '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
- })
@@ -1,271 +0,0 @@
1
- import debug from 'debug'
2
- import { randomIP, randomIP6, } from "./../"
3
- import ip6addr, { Addr, ToStringOpts } from 'ip6addr'
4
- import net from 'net'
5
- const ip6 = require('ip6') // "url": "https://github.com/elgs/ip6"
6
- const dbg = debug('_IPAddress')
7
- dbg.enabled = true
8
- // need version "ip6": "0.2.7",!!
9
-
10
- export class IPAddress {
11
- private _ip: Addr
12
-
13
- public static loopback_cidrs4 = [
14
- ip6addr.createCIDR('127.0.0.0/8'),
15
- ]
16
-
17
- public static loopback_cidrs6 = [
18
- ip6addr.createCIDR('::1/128') // yes ipv6 has single address
19
- ]
20
-
21
- public static loopback_cidrs = IPAddress.loopback_cidrs4.concat(IPAddress.loopback_cidrs6)
22
-
23
- public static lan_cidrs4 = [
24
- ip6addr.createCIDR('10.0.0.0/8'),
25
- ip6addr.createCIDR('172.16.0.0/12'),
26
- ip6addr.createCIDR('192.168.0.0/16'),
27
- ]
28
-
29
- public static lan_cidrs6 = [
30
- ip6addr.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
31
- ip6addr.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
32
- ]
33
-
34
- public static lan_cidrs = IPAddress.lan_cidrs4.concat(IPAddress.lan_cidrs6)
35
-
36
- public static local_cidrs = IPAddress.loopback_cidrs.concat(IPAddress.lan_cidrs)
37
- public static local_cidrs4 = IPAddress.loopback_cidrs4.concat(IPAddress.lan_cidrs4)
38
- public static local_cidrs6 = IPAddress.loopback_cidrs6.concat(IPAddress.lan_cidrs6)
39
-
40
-
41
- constructor(ip: string) { // may throw
42
- if (ip === '-') ip = '0.0.0.0' // is this really required?
43
- try {
44
- this._ip = ip6addr.parse(ip)
45
- if (this._ip._attrs.ipv4Mapped) {
46
- // we wany to convert it to ipv4
47
- this._ip = ip6addr.parse(this._ip.toString({ format: 'v4' }))
48
- }
49
- } catch (err) {
50
- if (err instanceof Error)
51
- throw new Error(`IPAddress: '${ip}' Error: ${err.message}`)
52
- throw err
53
- }
54
- }
55
-
56
-
57
- public abbreviated(): string {
58
- return this.toString() // default behaviour of ip6addr
59
- }
60
-
61
- public toString(opts: ToStringOpts = { format: 'auto', zeroElide: false, zeroPad: false }): string {
62
- return this._ip.toString()
63
- }
64
-
65
- public static validate(ip: string): boolean {
66
- try {
67
- new IPAddress(ip)
68
- return true
69
- } catch (_) {
70
- return false
71
- }
72
- }
73
-
74
- public static try(ip: string): IPAddress | undefined {
75
- try {
76
- return new IPAddress(ip)
77
- } catch (_) {
78
- return undefined
79
- }
80
- }
81
-
82
- public static fromString(ip: string): IPAddress {
83
- return new IPAddress(ip)
84
- }
85
-
86
- public static get Empty(): IPAddress {
87
- //The IP address 0.0.0.0 is a non-routable meta-address used to denote an invalid, unknown,
88
- // or non-applicable target. It's not assigned to any individual device.
89
- // It is used in different contexts to mean "any IPv4-enabled network interface" or as a default route
90
- return new IPAddress('0.0.0.0')
91
- }
92
-
93
- public isLoopbackIP(): boolean {
94
- return this.partOfCIDR(IPAddress.loopback_cidrs)
95
- }
96
-
97
- public isLANIP(): boolean {
98
- return this.partOfCIDR(IPAddress.lan_cidrs)
99
- }
100
-
101
- public isLocalIP(): boolean {
102
- return this.partOfCIDR(IPAddress.local_cidrs)
103
- }
104
-
105
- partOfCIDR(cidr_networks: ip6addr.CIDR[]): boolean {
106
- const ipNormalized = this.toString()
107
- let ipVersion = net.isIPv4(ipNormalized) ? 'ipv4' : 'ipv6'
108
- for (let cidr of cidr_networks) {
109
- if (!cidr) continue
110
- let first = cidr.first()
111
- if (first.kind() !== ipVersion) continue
112
- if (cidr.contains(ipNormalized))
113
- return true
114
- }
115
- return false
116
- }
117
-
118
- static partOfCIDR(_ip: string, cidr_networks: ip6addr.CIDR[]): boolean {
119
- const ip = IPAddress.try(_ip)
120
- if (ip === undefined) return false
121
- return ip.partOfCIDR(cidr_networks)
122
- }
123
-
124
- public isPublicIP(): boolean {
125
- return this.toString() !== '0.0.0.0' && !this.isLoopbackIP() && !this.isLANIP()
126
- }
127
-
128
- public static unserialize(serializedIpString: string): IPAddress {
129
- if (serializedIpString.length === 32) {
130
- // we have possible ipv6
131
- let ip = serializedIpString.match(/.{1,4}/g)?.join(':')
132
- if (ip) return new IPAddress(ip)
133
- }
134
- // we have possible ipv4
135
- let ip = serializedIpString.match(/.{1,3}/g)?.join('.')
136
- if (ip)
137
- try { return new IPAddress(ip) } catch (_) { }
138
-
139
- throw new Error(`Invalid SerializationString: ${serializedIpString}`)
140
-
141
- }
142
-
143
- public static randomIP4(): IPAddress {
144
- return new IPAddress(randomIP())
145
- }
146
-
147
-
148
- public static randomIP6(): IPAddress {
149
- return new IPAddress(randomIP6())
150
- }
151
-
152
-
153
- public static randomCIDRIp(cidr: ip6addr.CIDR[] | 'loopback' | 'lan' | 'local' | 'loopback4' | 'loopback6' | 'lan4' | 'lan6' | 'local4' | 'local6'): IPAddress {
154
- if (cidr === 'loopback') cidr = IPAddress.loopback_cidrs
155
- if (cidr === 'loopback4') cidr = IPAddress.loopback_cidrs4
156
- if (cidr === 'loopback6') cidr = IPAddress.loopback_cidrs6
157
- if (cidr === 'lan') cidr = IPAddress.lan_cidrs
158
- if (cidr === 'lan4') cidr = IPAddress.lan_cidrs4
159
- if (cidr === 'lan6') cidr = IPAddress.lan_cidrs6
160
- if (cidr === 'local') cidr = IPAddress.local_cidrs
161
- if (cidr === 'local4') cidr = IPAddress.local_cidrs4
162
- if (cidr === 'local6') cidr = IPAddress.local_cidrs6
163
- // dbg('randomCIDRIp', cidr)
164
- if (cidr.length === 0) throw new Error('CIDR array is empty')
165
- // Pick a random CIDR network
166
- const network = cidr[Math.floor(Math.random() * cidr.length)];
167
- // dbg('randomCIDRIp:network', network)
168
- // Get the range of IPs in this CIDR
169
- const start = network.first().toBigInt()
170
- // dbg('randomCIDRIp:first', network.first().toString(),start)
171
- const end = network.last().toBigInt();
172
- // dbg('randomCIDRIp:last', network.last().toString(),end)
173
- let range : any = end-start
174
- // dbg('randomCIDRIp:first:last', start, end)
175
-
176
-
177
- // Generate a random Long between the start and end
178
- // if start-end > Maximum integer number
179
- // we need to use BigInt
180
- if (range > BigInt(Number.MAX_SAFE_INTEGER)) range=(Number.MAX_SAFE_INTEGER)
181
-
182
- const randomBigInt = BigInt(Math.floor(Math.random() * (Number(range) + 1))) + start;
183
- // dbg('randomBigInt', randomBigInt)
184
-
185
- // Convert the BigInt back to an IP address
186
- const randomIp = ip6addr.parse(randomBigInt)
187
- // dbg('randomCIDRIp', randomIp.toString())
188
-
189
- return new IPAddress(randomIp.toString())
190
- }
191
-
192
- public serialize(): string {
193
- if (this.kind() === 4) {
194
- // zero pad ipv4
195
- return this._ip.toString().split('.').map((octet) => {
196
- return octet.padStart(3, '0')
197
- }).join('.').replace(/\./g, '')
198
- }
199
-
200
- // zero pad ipv6
201
- return ip6.normalize(this._ip.toString()).split(':').map((hextet: string) => {
202
- return hextet.padStart(4, '0')
203
- }).join(':').replace(/:/g, '')
204
- }
205
-
206
- public kind(): number {
207
- return this._ip.kind() === 'ipv6' ? 6 : 4
208
- }
209
-
210
- public equals(secondIp: IPAddress): boolean {
211
- return this._ip.compare(secondIp._ip) === 0
212
- }
213
-
214
- public randomPad(): string {
215
- if (this.kind() === 4) return this.padIp4()
216
- return this.padIp6()
217
- }
218
-
219
- public normalized(): string {
220
- if (this.kind() === 4) return this._ip.toString()
221
- return ip6.normalize(this._ip.toString())
222
- }
223
-
224
-
225
- private padIp6(): string {
226
- const hextets = this.normalized().split(':') // we need to have the full form
227
- const paddedHextets = hextets.map(hextet => {
228
- const cleanHextet = Number(`0x${hextet}`).toString(16)
229
- const padLength = Math.floor(Math.random() * (4 - cleanHextet.length))
230
- return cleanHextet.padStart(padLength + cleanHextet.length, '0')
231
- })
232
-
233
- return paddedHextets.join(':')
234
- }
235
-
236
- private padIp4(): string {
237
- const octets = this._ip.toString().split('.')
238
- const paddedOctets = octets.map(octet => {
239
- const cleanOctet = String(Number(octet))
240
- const padLength = Math.floor(Math.random() * (3 - cleanOctet.length))
241
- return cleanOctet.padStart(padLength + cleanOctet.length, '0')
242
- });
243
- return paddedOctets.join('.')
244
- }
245
-
246
- asciiChecksum(): string {
247
- // support ipv4 and ipv6
248
- // FNV-1a hash parameters
249
- const FNV_PRIME = 0x01000193;
250
- const FNV_OFFSET_BASIS = 0x811c9dc5;
251
- const CHAR_SET = 'abcdefghijklmnopqrstuvwxyz0123456789';
252
-
253
- let hash = FNV_OFFSET_BASIS;
254
- const ip = this.normalized()
255
- for (let i = 0; i < ip.length; i++) {
256
- hash ^= ip.charCodeAt(i);
257
- hash *= FNV_PRIME;
258
- hash >>>= 0; // Ensure hash is always a positive 32-bit integer
259
- }
260
-
261
- // Map the hash to a character in the CHAR_SET
262
- const index = hash % CHAR_SET.length;
263
- return CHAR_SET.charAt(index);
264
- }
265
-
266
- checksum1(): string {
267
- // this shall check that the ip is ipv4 and return the last octet as checksum
268
- if (this.kind() !== 4) return 'x'
269
- return this._ip.toString().split('.').pop() || '-'
270
- }
271
- }
@@ -1,18 +0,0 @@
1
- export declare class Timespan {
2
- private readonly ms;
3
- constructor(ms: number);
4
- get miliseconds(): number;
5
- get seconds(): number;
6
- get minutes(): number;
7
- get hours(): number;
8
- get days(): number;
9
- get weeks(): number;
10
- get months(): number;
11
- static miliseconds(amount: number): Timespan;
12
- static seconds(amount: number): Timespan;
13
- static minutes(mins: number): Timespan;
14
- static hours(hours: number): Timespan;
15
- static days(days: number): Timespan;
16
- static weeks(weeks: number): Timespan;
17
- static months(months: number): Timespan;
18
- }