ip-utilties 3.5.0 → 3.5.2

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 (3) hide show
  1. package/package.json +3 -3
  2. package/src/ip.mjs +48 -18
  3. package/types/ip.d.mts +41 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "3.5.0",
3
+ "version": "3.5.2",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -41,11 +41,11 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "ava": "^8.0.1",
44
- "browser-ava": "^2.3.65",
44
+ "browser-ava": "^2.3.66",
45
45
  "c8": "^11.0.0",
46
46
  "documentation": "^14.0.3",
47
47
  "semantic-release": "^25.0.5",
48
- "typescript": "^6.0.3"
48
+ "typescript": "^7.0.2"
49
49
  },
50
50
  "overrides": {
51
51
  "c8": {
package/src/ip.mjs CHANGED
@@ -22,7 +22,23 @@ const ipv4 = {
22
22
  linkLocalPrefixLength: 16,
23
23
  localHost: new Uint8Array([127, 0, 0, 1]),
24
24
  localHostPrefixLenth: 8,
25
- wellKnownClasses: [[new Uint8Array([192, 168]), 8]]
25
+
26
+ // https://www.geeksforgeeks.org/computer-networks/subnet-cheat-sheet/
27
+ wellKnownAddresses: [
28
+ [new Uint8Array([0, 0, 0, 0]), 8], // This network
29
+ [new Uint8Array([10, 0, 0, 0]), 8], // Private Address Block
30
+ [new Uint8Array([100, 64, 0, 0]), 10], // Carrier-grade NAT
31
+ [new Uint8Array([127, 0, 0, 0]), 8], // Loopback
32
+ [new Uint8Array([127, 0, 53, 53]), 0], // Name collision occurrence
33
+ [new Uint8Array([169, 254, 0, 0]), 16], // Link local
34
+ [new Uint8Array([172, 16, 0, 0]), 12], // Private Address Block
35
+ [new Uint8Array([192, 0, 0, 0]), 24], // IETF protocol assignments
36
+ [new Uint8Array([192, 0, 2, 0]), 24], // TEST-NET-1
37
+ [new Uint8Array([192, 168, 0, 0]), 16], // Private Address Block
38
+ [new Uint8Array([198, 18, 0, 0]), 15], // Network benchmark testing
39
+ [new Uint8Array([198, 51, 100, 0]), 24], // TEST-NET-2
40
+ [new Uint8Array([255, 255, 255, 255]), 0] // Limited Broadcast address
41
+ ]
26
42
  };
27
43
 
28
44
  const ipv6 = {
@@ -378,28 +394,42 @@ export function hasWellKnownSubnet(address) {
378
394
  export function wellKnownSubnet(address) {
379
395
  const family = _family(address);
380
396
  if (family) {
381
- const encoded = _encode(family, address);
397
+ return _wellKnownSubnet(family, address);
398
+ }
399
+ }
382
400
 
383
- if (_equal(family.localHost, encoded)) {
384
- return [family.localHost, family.localHostPrefixLenth];
385
- }
386
- if (_equal(family.linkLocalPrefix, encoded)) {
387
- return [family.linkLocalPrefix, family.linkLocalPrefixLength];
388
- }
389
- if (_isUniqueLocal(encoded)) {
390
- return [encoded[0], 64];
391
- }
401
+ export function _wellKnownSubnet(family, address) {
402
+ const encoded = _encode(family, address);
392
403
 
393
- if (family === ipv4) {
394
- for (const c of family.wellKnownClasses) {
395
- if (_equal(c[0], encoded)) {
396
- return c;
397
- }
404
+ if (_equal(family.localHost, encoded)) {
405
+ return [family.localHost, family.localHostPrefixLenth];
406
+ }
407
+ if (_equal(family.linkLocalPrefix, encoded)) {
408
+ return [family.linkLocalPrefix, family.linkLocalPrefixLength];
409
+ }
410
+ if (_isUniqueLocal(encoded)) {
411
+ return [encoded[0], 64];
412
+ }
413
+
414
+ if (family === ipv4) {
415
+ for (const c of family.wellKnownAddresses) {
416
+ const pl = c[1];
417
+ if (
418
+ pl > 0 &&
419
+ _prefix(family, c[0], pl) === _prefix(family, encoded, pl)
420
+ ) {
421
+ /*console.log(
422
+ c,
423
+ encoded,
424
+ _prefix(family, c[0], pl),
425
+ _prefix(family, encoded, pl),
426
+ prefixIP(c[0], pl),
427
+ prefixIP(encoded, pl)
428
+ );*/
429
+ return c;
398
430
  }
399
431
  }
400
432
  }
401
-
402
- return undefined;
403
433
  }
404
434
 
405
435
  /*
package/types/ip.d.mts CHANGED
@@ -1,65 +1,66 @@
1
+ /**
2
+ * Address familiy IPv4
3
+ */
4
+ export declare const FAMILY_IPV4 = "IPv4";
5
+ /**
6
+ * Address familiy IPv6
7
+ */
8
+ export declare const FAMILY_IPV6 = "IPv6";
1
9
  /**
2
10
  * Encode ipv4 or ipv6 address into number array.
3
11
  * @param {string|number[]} address
4
12
  * @returns number[]
5
13
  */
6
- export function encodeIP(address: string | number[]): any;
14
+ export declare function encodeIP(address: string | number[]): any;
7
15
  /**
8
16
  * Encode ipv6 address into number array.
9
17
  * @param {string|number[]|bigint} address
10
18
  * @returns number[]
11
19
  */
12
- export function encodeIPv6(address: string | number[] | bigint): any;
20
+ export declare function encodeIPv6(address: string | number[] | bigint): any;
13
21
  /**
14
22
  * Encode ipv4 address into number array.
15
23
  * @param {string|number[]|bigint} address
16
24
  * @returns number[]
17
25
  */
18
- export function encodeIPv4(address: string | number[] | bigint): any;
19
- export function decodeIPv6(address: any, length: any): string;
20
- export function decodeIPv4(address: any, length: any): string;
21
- export function decodeIP(address: any, length: any): string;
22
- export function isIPv4(address: any): boolean;
23
- export function isIPv6(address: any): boolean;
26
+ export declare function encodeIPv4(address: string | number[] | bigint): any;
27
+ export declare function decodeIPv6(address: any, length: any): string;
28
+ export declare function decodeIPv4(address: any, length: any): string;
29
+ export declare function decodeIP(address: any, length: any): string;
30
+ export declare function isIPv4(address: any): boolean;
31
+ export declare function isIPv6(address: any): boolean;
24
32
  /**
25
33
  * IP address family for a given address.
26
34
  * @param {string|Uint8Array|Uint16Array} address
27
35
  * @return {string|undefined}
28
36
  */
29
- export function familyIP(address: string | Uint8Array | Uint16Array): string | undefined;
30
- export function asBigInt(address: any): any;
31
- export function prefixIP(address: any, length: any): string;
32
- export function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
33
- export function matchPrefixIP(prefix: any, length: any, address: any): boolean;
34
- export function normalizeCIDR(address: any): {
37
+ export declare function familyIP(address: string | Uint8Array | Uint16Array): string | undefined;
38
+ export declare function asBigInt(address: any): any;
39
+ export declare function prefixIP(address: any, length: any): string;
40
+ export declare function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
41
+ export declare function matchPrefixIP(prefix: any, length: any, address: any): boolean;
42
+ export declare function normalizeCIDR(address: any): {
35
43
  longPrefix: any;
36
44
  prefix: any;
37
45
  prefixLength: any;
38
46
  cidr: string;
39
47
  };
40
- export function formatCIDR(address: any, prefixLength: any): any;
41
- export function normalizeIP(address: any): string | undefined;
42
- export function reverseArpa(address: any): string;
43
- export function isLocalhost(address: any): boolean;
44
- export function isLinkLocal(address: any): boolean;
45
- export function _isLinkLocal(family: any, address: any): boolean;
46
- export function isUniqueLocal(address: any): boolean;
47
- export function _isUniqueLocal(eaddr: any): boolean;
48
- export function hasWellKnownSubnet(address: any): boolean;
49
- export function wellKnownSubnet(address: any): any[] | undefined;
50
- /**
51
- * Address familiy IPv4
52
- */
53
- export const FAMILY_IPV4: "IPv4";
54
- /**
55
- * Address familiy IPv6
56
- */
57
- export const FAMILY_IPV6: "IPv6";
58
- export const IPV6_NODE_LOCAL_ALL_NODES: any;
59
- export const IPV6_NODE_LOCAL_ALL_ROUTERS: any;
60
- export const IPV6_LINK_LOCAL_ALL_NODES: any;
61
- export const IPV6_LINK_LOCAL_ALL_ROUTERS: any;
62
- export const IPV6_SITE_LOCAL_ALL_ROUTERS: any;
63
- export const IPV6_SITE_LOCAL_ALL_DHCP_SERVERS: any;
64
- export const IPV4_LOCALHOST: Uint8Array<ArrayBuffer>;
65
- export const IPV6_LOCALHOST: Uint16Array<ArrayBuffer>;
48
+ export declare function formatCIDR(address: any, prefixLength: any): any;
49
+ export declare function normalizeIP(address: any): string | undefined;
50
+ export declare function reverseArpa(address: any): string;
51
+ export declare function isLocalhost(address: any): boolean;
52
+ export declare function isLinkLocal(address: any): boolean;
53
+ export declare function _isLinkLocal(family: any, address: any): boolean;
54
+ export declare function isUniqueLocal(address: any): boolean;
55
+ export declare function _isUniqueLocal(eaddr: any): boolean;
56
+ export declare function hasWellKnownSubnet(address: any): boolean;
57
+ export declare function wellKnownSubnet(address: any): any;
58
+ export declare function _wellKnownSubnet(family: any, address: any): any;
59
+ export declare const IPV6_NODE_LOCAL_ALL_NODES: any;
60
+ export declare const IPV6_NODE_LOCAL_ALL_ROUTERS: any;
61
+ export declare const IPV6_LINK_LOCAL_ALL_NODES: any;
62
+ export declare const IPV6_LINK_LOCAL_ALL_ROUTERS: any;
63
+ export declare const IPV6_SITE_LOCAL_ALL_ROUTERS: any;
64
+ export declare const IPV6_SITE_LOCAL_ALL_DHCP_SERVERS: any;
65
+ export declare const IPV4_LOCALHOST: Uint8Array<ArrayBuffer>;
66
+ export declare const IPV6_LOCALHOST: Uint16Array<ArrayBuffer>;