ip-utilties 3.5.3 → 3.6.1
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 +2 -2
- package/src/ip.mjs +49 -42
- package/types/ip.d.mts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ip-utilties",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"provenance": true
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"browser-ava": "^2.3.66",
|
|
45
45
|
"c8": "^11.0.0",
|
|
46
46
|
"documentation": "^14.0.3",
|
|
47
|
-
"semantic-release": "^25.0.
|
|
47
|
+
"semantic-release": "^25.0.6",
|
|
48
48
|
"typescript": "^7.0.2"
|
|
49
49
|
},
|
|
50
50
|
"overrides": {
|
package/src/ip.mjs
CHANGED
|
@@ -18,19 +18,18 @@ const ipv4 = {
|
|
|
18
18
|
segmentLength: 8,
|
|
19
19
|
segmentMask: 0xffn,
|
|
20
20
|
base: 10,
|
|
21
|
-
linkLocalPrefix: new Uint8Array([169, 254]),
|
|
22
|
-
linkLocalPrefixLength: 16,
|
|
23
21
|
localHost: new Uint8Array([127, 0, 0, 1]),
|
|
24
22
|
localHostPrefixLenth: 8,
|
|
25
23
|
|
|
26
24
|
// https://www.geeksforgeeks.org/computer-networks/subnet-cheat-sheet/
|
|
27
25
|
wellKnownAddresses: [
|
|
26
|
+
[new Uint8Array([169, 254, 0, 0]), 16], // Link-local address (Autoconfiguration)
|
|
27
|
+
|
|
28
28
|
[new Uint8Array([0, 0, 0, 0]), 8], // This network
|
|
29
29
|
[new Uint8Array([10, 0, 0, 0]), 8], // Private network (RFC 1918)
|
|
30
30
|
[new Uint8Array([100, 64, 0, 0]), 10], // Carrier-grade NAT / Shared address space (CGN)
|
|
31
31
|
[new Uint8Array([127, 0, 0, 0]), 8], // Loopback
|
|
32
32
|
[new Uint8Array([127, 0, 53, 53]), 0], // Name collision occurrence
|
|
33
|
-
[new Uint8Array([169, 254, 0, 0]), 16], // Link-local address (Autoconfiguration)
|
|
34
33
|
[new Uint8Array([172, 16, 0, 0]), 12], // Private network (RFC 1918)
|
|
35
34
|
[new Uint8Array([192, 0, 0, 0]), 24], // IETF protocol assignments
|
|
36
35
|
[new Uint8Array([192, 0, 2, 0]), 24], // TEST-NET-1
|
|
@@ -61,10 +60,9 @@ const ipv6 = {
|
|
|
61
60
|
segmentLength: 16,
|
|
62
61
|
segmentMask: 0xffffn,
|
|
63
62
|
base: 16,
|
|
64
|
-
linkLocalPrefix: new Uint16Array([0xfe80]),
|
|
65
|
-
linkLocalPrefixLength: 64,
|
|
66
63
|
localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
|
|
67
|
-
localHostPrefixLenth: 128
|
|
64
|
+
localHostPrefixLenth: 128,
|
|
65
|
+
wellKnownAddresses: [[new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64]]
|
|
68
66
|
};
|
|
69
67
|
|
|
70
68
|
const families = [ipv4, ipv6];
|
|
@@ -250,6 +248,11 @@ export function prefixIP(address, length) {
|
|
|
250
248
|
return _decode(family, _prefix(family, address, length));
|
|
251
249
|
}
|
|
252
250
|
|
|
251
|
+
export function prefixOnlyIP(address, length) {
|
|
252
|
+
const family = _family(address);
|
|
253
|
+
return _decode(family, _prefix(family, address, length), length);
|
|
254
|
+
}
|
|
255
|
+
|
|
253
256
|
function _prefix(family, address, length) {
|
|
254
257
|
return (
|
|
255
258
|
_asBigInt(family, address) & (-1n << BigInt(family.bitLength - length))
|
|
@@ -280,34 +283,45 @@ export function normalizeCIDR(address) {
|
|
|
280
283
|
|
|
281
284
|
const family = isIPv6(prefix) ? ipv6 : ipv4;
|
|
282
285
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
prefix = _decode(
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
longPrefix = prefix;
|
|
286
|
+
const wns = _wellKnownSubnet(family, address);
|
|
287
|
+
if (family === ipv4 && wns && !prefixLength) {
|
|
288
|
+
prefixLength = wns[1];
|
|
289
|
+
prefix = _decode(
|
|
290
|
+
family,
|
|
291
|
+
_prefix(family, wns[0], prefixLength),
|
|
292
|
+
prefixLength
|
|
293
|
+
);
|
|
294
|
+
longPrefix = wns[0];
|
|
295
295
|
} else {
|
|
296
|
-
|
|
296
|
+
if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
|
|
297
|
+
prefixLength = family.wellKnownAddresses[0][1]; // TODO
|
|
298
|
+
const n = _prefix(family, address, prefixLength);
|
|
299
|
+
prefix = _decode(family, n, prefixLength);
|
|
300
|
+
|
|
301
|
+
if (family === ipv6) {
|
|
302
|
+
if (!prefix.endsWith("::")) {
|
|
303
|
+
// TODO
|
|
304
|
+
prefix += prefix.endsWith(":") ? ":" : "::";
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
longPrefix = prefix;
|
|
308
|
+
} else {
|
|
309
|
+
prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
|
|
297
310
|
|
|
298
|
-
|
|
311
|
+
let n;
|
|
299
312
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
313
|
+
if (prefixLength) {
|
|
314
|
+
n = _prefix(family, prefix, prefixLength);
|
|
315
|
+
} else {
|
|
316
|
+
n = _encode(family, prefix);
|
|
304
317
|
|
|
305
|
-
|
|
306
|
-
|
|
318
|
+
if (isLocalhost(n)) {
|
|
319
|
+
prefixLength = family.localHostPrefixLenth;
|
|
320
|
+
}
|
|
307
321
|
}
|
|
322
|
+
prefix = _decode(family, n, prefixLength);
|
|
323
|
+
longPrefix = _decode(family, n);
|
|
308
324
|
}
|
|
309
|
-
prefix = _decode(family, n, prefixLength);
|
|
310
|
-
longPrefix = _decode(family, n);
|
|
311
325
|
}
|
|
312
326
|
|
|
313
327
|
return {
|
|
@@ -377,7 +391,7 @@ export function isLinkLocal(address) {
|
|
|
377
391
|
}
|
|
378
392
|
|
|
379
393
|
export function _isLinkLocal(family, address) {
|
|
380
|
-
return
|
|
394
|
+
return _wellKnownSubnet(family, address) === family.wellKnownAddresses[0];
|
|
381
395
|
}
|
|
382
396
|
|
|
383
397
|
export function isUniqueLocal(address) {
|
|
@@ -406,21 +420,15 @@ export function _wellKnownSubnet(family, address) {
|
|
|
406
420
|
if (_equal(family.localHost, encoded)) {
|
|
407
421
|
return [family.localHost, family.localHostPrefixLenth];
|
|
408
422
|
}
|
|
409
|
-
|
|
410
|
-
return [family.linkLocalPrefix, family.linkLocalPrefixLength];
|
|
411
|
-
}
|
|
423
|
+
|
|
412
424
|
if (_isUniqueLocal(encoded)) {
|
|
413
425
|
return [encoded[0], 64];
|
|
414
426
|
}
|
|
415
427
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
pl > 0 &&
|
|
421
|
-
_prefix(family, c[0], pl) === _prefix(family, encoded, pl)
|
|
422
|
-
) {
|
|
423
|
-
/*console.log(
|
|
428
|
+
for (const c of family.wellKnownAddresses) {
|
|
429
|
+
const pl = c[1];
|
|
430
|
+
if (pl > 0 && _prefix(family, c[0], pl) === _prefix(family, encoded, pl)) {
|
|
431
|
+
/*console.log(
|
|
424
432
|
c,
|
|
425
433
|
encoded,
|
|
426
434
|
_prefix(family, c[0], pl),
|
|
@@ -428,8 +436,7 @@ export function _wellKnownSubnet(family, address) {
|
|
|
428
436
|
prefixIP(c[0], pl),
|
|
429
437
|
prefixIP(encoded, pl)
|
|
430
438
|
);*/
|
|
431
|
-
|
|
432
|
-
}
|
|
439
|
+
return c;
|
|
433
440
|
}
|
|
434
441
|
}
|
|
435
442
|
}
|
package/types/ip.d.mts
CHANGED
|
@@ -37,6 +37,7 @@ export declare function isIPv6(address: any): boolean;
|
|
|
37
37
|
export declare function familyIP(address: string | Uint8Array | Uint16Array): string | undefined;
|
|
38
38
|
export declare function asBigInt(address: any): any;
|
|
39
39
|
export declare function prefixIP(address: any, length: any): string;
|
|
40
|
+
export declare function prefixOnlyIP(address: any, length: any): string;
|
|
40
41
|
export declare function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
|
|
41
42
|
export declare function matchPrefixIP(prefix: any, length: any, address: any): boolean;
|
|
42
43
|
export declare function normalizeCIDR(address: any): {
|