mybase 1.1.30 → 1.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/ts/models/IPAddress.js +8 -6
- package/ts/models/IPAddress.test.ts +86 -0
- package/ts/models/IPAddress.ts +11 -7
- package/types/ip6addr.d.ts +6 -0
package/package.json
CHANGED
package/ts/models/IPAddress.js
CHANGED
|
@@ -15,9 +15,13 @@ dbg.enabled = true;
|
|
|
15
15
|
class IPAddress {
|
|
16
16
|
constructor(ip) {
|
|
17
17
|
if (ip === '-')
|
|
18
|
-
ip = '0.0.0.0';
|
|
18
|
+
ip = '0.0.0.0'; // is this really required?
|
|
19
19
|
try {
|
|
20
20
|
this._ip = ip6addr_1.default.parse(ip);
|
|
21
|
+
if (this._ip._attrs.ipv4Mapped) {
|
|
22
|
+
// we wany to convert it to ipv4
|
|
23
|
+
this._ip = ip6addr_1.default.parse(this._ip.toString({ format: 'v4' }));
|
|
24
|
+
}
|
|
21
25
|
}
|
|
22
26
|
catch (err) {
|
|
23
27
|
if (err instanceof Error)
|
|
@@ -25,6 +29,9 @@ class IPAddress {
|
|
|
25
29
|
throw err;
|
|
26
30
|
}
|
|
27
31
|
}
|
|
32
|
+
abbreviated() {
|
|
33
|
+
return this.toString(); // default behaviour of ip6addr
|
|
34
|
+
}
|
|
28
35
|
toString(opts = { format: 'auto', zeroElide: false, zeroPad: false }) {
|
|
29
36
|
return this._ip.toString();
|
|
30
37
|
}
|
|
@@ -181,11 +188,6 @@ class IPAddress {
|
|
|
181
188
|
return this._ip.toString();
|
|
182
189
|
return ip6.normalize(this._ip.toString());
|
|
183
190
|
}
|
|
184
|
-
abbreviated() {
|
|
185
|
-
if (this.kind() === 4)
|
|
186
|
-
return this._ip.toString();
|
|
187
|
-
return ip6.abbreviate(this._ip.toString());
|
|
188
|
-
}
|
|
189
191
|
padIp6() {
|
|
190
192
|
const hextets = this.normalized().split(':'); // we need to have the full form
|
|
191
193
|
const paddedHextets = hextets.map(hextet => {
|
|
@@ -110,6 +110,92 @@ describe('IPAddress', () => {
|
|
|
110
110
|
}
|
|
111
111
|
})
|
|
112
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
|
+
|
|
113
199
|
});
|
|
114
200
|
|
|
115
201
|
describe('IPAddress IPv6', () => {
|
package/ts/models/IPAddress.ts
CHANGED
|
@@ -39,10 +39,13 @@ export class IPAddress {
|
|
|
39
39
|
|
|
40
40
|
|
|
41
41
|
constructor(ip: string) { // may throw
|
|
42
|
-
if (ip === '-') ip = '0.0.0.0'
|
|
43
|
-
|
|
42
|
+
if (ip === '-') ip = '0.0.0.0' // is this really required?
|
|
44
43
|
try {
|
|
45
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
|
+
}
|
|
46
49
|
} catch (err) {
|
|
47
50
|
if (err instanceof Error)
|
|
48
51
|
throw new Error(`IPAddress: '${ip}' Error: ${err.message}`)
|
|
@@ -50,7 +53,12 @@ export class IPAddress {
|
|
|
50
53
|
}
|
|
51
54
|
}
|
|
52
55
|
|
|
53
|
-
|
|
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 {
|
|
54
62
|
return this._ip.toString()
|
|
55
63
|
}
|
|
56
64
|
|
|
@@ -213,10 +221,6 @@ export class IPAddress {
|
|
|
213
221
|
return ip6.normalize(this._ip.toString())
|
|
214
222
|
}
|
|
215
223
|
|
|
216
|
-
public abbreviated(): string {
|
|
217
|
-
if (this.kind() === 4) return this._ip.toString()
|
|
218
|
-
return ip6.abbreviate(this._ip.toString())
|
|
219
|
-
}
|
|
220
224
|
|
|
221
225
|
private padIp6(): string {
|
|
222
226
|
const hextets = this.normalized().split(':') // we need to have the full form
|
package/types/ip6addr.d.ts
CHANGED
|
@@ -4,8 +4,14 @@ declare module 'ip6addr' {
|
|
|
4
4
|
zeroElide?: boolean | undefined;
|
|
5
5
|
zeroPad?: boolean | undefined;
|
|
6
6
|
}
|
|
7
|
+
|
|
8
|
+
export interface AddrAttributes {
|
|
9
|
+
ipv4Bare:boolean;
|
|
10
|
+
ipv4Mapped: boolean;
|
|
11
|
+
}
|
|
7
12
|
|
|
8
13
|
export interface Addr {
|
|
14
|
+
_attrs: AddrAttributes;
|
|
9
15
|
kind: () => "ipv4" | "ipv6";
|
|
10
16
|
toString: (opts?: ToStringOpts) => string;
|
|
11
17
|
toBuffer: (buff?: Uint8Array) => Uint8Array;
|