ip-utilties 3.1.3 → 3.2.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 +33 -14
- package/types/ip.d.mts +1 -0
package/package.json
CHANGED
package/src/ip.mjs
CHANGED
|
@@ -17,7 +17,9 @@ const ipv4 = {
|
|
|
17
17
|
segments: 4,
|
|
18
18
|
segmentLength: 8,
|
|
19
19
|
segmentMask: 0xffn,
|
|
20
|
-
base: 10
|
|
20
|
+
base: 10,
|
|
21
|
+
linkLocalPrefix: new Uint8Array([169, 254]),
|
|
22
|
+
linkLocalPrefixLength: 16
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
const ipv6 = {
|
|
@@ -37,7 +39,9 @@ const ipv6 = {
|
|
|
37
39
|
segments: 8,
|
|
38
40
|
segmentLength: 16,
|
|
39
41
|
segmentMask: 0xffffn,
|
|
40
|
-
base: 16
|
|
42
|
+
base: 16,
|
|
43
|
+
linkLocalPrefix: new Uint16Array([0xfe80]),
|
|
44
|
+
linkLocalPrefixLength: 64
|
|
41
45
|
};
|
|
42
46
|
|
|
43
47
|
/**
|
|
@@ -249,19 +253,23 @@ export function normalizeCIDR(address) {
|
|
|
249
253
|
let [prefix, prefixLength] = address.split(/\//);
|
|
250
254
|
let longPrefix;
|
|
251
255
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
256
|
+
const family = isIPv6(prefix) ? ipv6 : ipv4;
|
|
257
|
+
|
|
258
|
+
if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
|
|
259
|
+
prefixLength = family.linkLocalPrefixLength;
|
|
260
|
+
const n = _prefix(family, address, prefixLength);
|
|
261
|
+
prefix = _decode(family, n, prefixLength);
|
|
262
|
+
|
|
263
|
+
if (family == ipv6) {
|
|
264
|
+
if (!prefix.endsWith("::")) {
|
|
265
|
+
// TODO
|
|
266
|
+
prefix += prefix.endsWith(":") ? ":" : "::";
|
|
267
|
+
}
|
|
259
268
|
}
|
|
260
269
|
longPrefix = prefix;
|
|
261
270
|
} else {
|
|
262
271
|
prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
|
|
263
272
|
|
|
264
|
-
const family = /*_family(prefix); */ isIPv6(prefix) ? ipv6 : ipv4;
|
|
265
273
|
let n;
|
|
266
274
|
|
|
267
275
|
if (prefixLength) {
|
|
@@ -330,12 +338,22 @@ export function isLocalhost(address) {
|
|
|
330
338
|
}
|
|
331
339
|
|
|
332
340
|
export function isLinkLocal(address) {
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
335
|
-
return
|
|
341
|
+
const family = _family(address);
|
|
342
|
+
if (!family) {
|
|
343
|
+
return false;
|
|
336
344
|
}
|
|
345
|
+
return _isLinkLocal(family, address);
|
|
346
|
+
}
|
|
337
347
|
|
|
338
|
-
|
|
348
|
+
export function _isLinkLocal(family, address) {
|
|
349
|
+
const eaddr = _encode(family, address);
|
|
350
|
+
let i = 0;
|
|
351
|
+
for (const slot of family.linkLocalPrefix) {
|
|
352
|
+
if (slot !== eaddr[i++]) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return true;
|
|
339
357
|
}
|
|
340
358
|
|
|
341
359
|
export function isUniqueLocal(address) {
|
|
@@ -364,6 +382,7 @@ export function hasWellKnownSubnet(address) {
|
|
|
364
382
|
::1 128. local host
|
|
365
383
|
127 8 local host
|
|
366
384
|
172
|
|
385
|
+
|
|
367
386
|
*/
|
|
368
387
|
|
|
369
388
|
/*
|
package/types/ip.d.mts
CHANGED
|
@@ -42,6 +42,7 @@ export function normalizeIP(address: any): string | undefined;
|
|
|
42
42
|
export function reverseArpa(address: any): string;
|
|
43
43
|
export function isLocalhost(address: any): boolean;
|
|
44
44
|
export function isLinkLocal(address: any): boolean;
|
|
45
|
+
export function _isLinkLocal(family: any, address: any): boolean;
|
|
45
46
|
export function isUniqueLocal(address: any): boolean;
|
|
46
47
|
export function hasWellKnownSubnet(address: any): boolean;
|
|
47
48
|
/**
|