ip-utilties 3.2.0 → 3.3.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.
- package/package.json +1 -1
- package/src/ip.mjs +12 -8
- package/types/ip.d.mts +1 -1
package/package.json
CHANGED
package/src/ip.mjs
CHANGED
|
@@ -19,7 +19,8 @@ const ipv4 = {
|
|
|
19
19
|
segmentMask: 0xffn,
|
|
20
20
|
base: 10,
|
|
21
21
|
linkLocalPrefix: new Uint8Array([169, 254]),
|
|
22
|
-
linkLocalPrefixLength: 16
|
|
22
|
+
linkLocalPrefixLength: 16,
|
|
23
|
+
localHostPrefixLenth: 8
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
const ipv6 = {
|
|
@@ -41,9 +42,12 @@ const ipv6 = {
|
|
|
41
42
|
segmentMask: 0xffffn,
|
|
42
43
|
base: 16,
|
|
43
44
|
linkLocalPrefix: new Uint16Array([0xfe80]),
|
|
44
|
-
linkLocalPrefixLength: 64
|
|
45
|
+
linkLocalPrefixLength: 64,
|
|
46
|
+
localHostPrefixLenth: 128
|
|
45
47
|
};
|
|
46
48
|
|
|
49
|
+
const families = [ipv4, ipv6];
|
|
50
|
+
|
|
47
51
|
/**
|
|
48
52
|
* Encode ipv4 or ipv6 address into number array.
|
|
49
53
|
* @param {string|number[]} address
|
|
@@ -64,7 +68,7 @@ export function encodeIPv6(address) {
|
|
|
64
68
|
}
|
|
65
69
|
|
|
66
70
|
/**
|
|
67
|
-
* Encode
|
|
71
|
+
* Encode ipv4 address into number array.
|
|
68
72
|
* @param {string|number[]|bigint} address
|
|
69
73
|
* @returns number[]
|
|
70
74
|
*/
|
|
@@ -177,7 +181,7 @@ export function familyIP(address) {
|
|
|
177
181
|
}
|
|
178
182
|
|
|
179
183
|
function _family(address) {
|
|
180
|
-
return
|
|
184
|
+
return families.find(d => _is(d, address));
|
|
181
185
|
}
|
|
182
186
|
|
|
183
187
|
function _is(family, address) {
|
|
@@ -278,7 +282,7 @@ export function normalizeCIDR(address) {
|
|
|
278
282
|
n = _encode(family, prefix);
|
|
279
283
|
|
|
280
284
|
if (isLocalhost(n)) {
|
|
281
|
-
prefixLength = family
|
|
285
|
+
prefixLength = family.localHostPrefixLenth;
|
|
282
286
|
}
|
|
283
287
|
}
|
|
284
288
|
prefix = _decode(family, n, prefixLength);
|
|
@@ -339,10 +343,10 @@ export function isLocalhost(address) {
|
|
|
339
343
|
|
|
340
344
|
export function isLinkLocal(address) {
|
|
341
345
|
const family = _family(address);
|
|
342
|
-
if (
|
|
343
|
-
return
|
|
346
|
+
if (family) {
|
|
347
|
+
return _isLinkLocal(family, address);
|
|
344
348
|
}
|
|
345
|
-
return
|
|
349
|
+
return false;
|
|
346
350
|
}
|
|
347
351
|
|
|
348
352
|
export function _isLinkLocal(family, address) {
|
package/types/ip.d.mts
CHANGED
|
@@ -11,7 +11,7 @@ export function encodeIP(address: string | number[]): any;
|
|
|
11
11
|
*/
|
|
12
12
|
export function encodeIPv6(address: string | number[] | bigint): any;
|
|
13
13
|
/**
|
|
14
|
-
* Encode
|
|
14
|
+
* Encode ipv4 address into number array.
|
|
15
15
|
* @param {string|number[]|bigint} address
|
|
16
16
|
* @returns number[]
|
|
17
17
|
*/
|