mybase 1.2.2 → 2.0.0

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