@ac-kit/net-address 0.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.
Files changed (128) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +28 -0
  3. package/README.md +65 -0
  4. package/dist/errors.d.ts +6 -0
  5. package/dist/errors.d.ts.map +1 -0
  6. package/dist/errors.js +10 -0
  7. package/dist/errors.js.map +1 -0
  8. package/dist/eui/constants.d.ts +7 -0
  9. package/dist/eui/constants.d.ts.map +1 -0
  10. package/dist/eui/constants.js +7 -0
  11. package/dist/eui/constants.js.map +1 -0
  12. package/dist/eui/eui-bits.d.ts +99 -0
  13. package/dist/eui/eui-bits.d.ts.map +1 -0
  14. package/dist/eui/eui-bits.js +138 -0
  15. package/dist/eui/eui-bits.js.map +1 -0
  16. package/dist/eui/eui-convert.d.ts +37 -0
  17. package/dist/eui/eui-convert.d.ts.map +1 -0
  18. package/dist/eui/eui-convert.js +76 -0
  19. package/dist/eui/eui-convert.js.map +1 -0
  20. package/dist/eui/interface-identifier.d.ts +24 -0
  21. package/dist/eui/interface-identifier.d.ts.map +1 -0
  22. package/dist/eui/interface-identifier.js +47 -0
  23. package/dist/eui/interface-identifier.js.map +1 -0
  24. package/dist/eui/parse-eui.d.ts +31 -0
  25. package/dist/eui/parse-eui.d.ts.map +1 -0
  26. package/dist/eui/parse-eui.js +93 -0
  27. package/dist/eui/parse-eui.js.map +1 -0
  28. package/dist/eui/print-eui.d.ts +44 -0
  29. package/dist/eui/print-eui.d.ts.map +1 -0
  30. package/dist/eui/print-eui.js +64 -0
  31. package/dist/eui/print-eui.js.map +1 -0
  32. package/dist/index.d.ts +29 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +29 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/ipv4/classify-ipv4.d.ts +69 -0
  37. package/dist/ipv4/classify-ipv4.d.ts.map +1 -0
  38. package/dist/ipv4/classify-ipv4.js +129 -0
  39. package/dist/ipv4/classify-ipv4.js.map +1 -0
  40. package/dist/ipv4/constants.d.ts +5 -0
  41. package/dist/ipv4/constants.d.ts.map +1 -0
  42. package/dist/ipv4/constants.js +5 -0
  43. package/dist/ipv4/constants.js.map +1 -0
  44. package/dist/ipv4/ipv4-prefix.d.ts +52 -0
  45. package/dist/ipv4/ipv4-prefix.d.ts.map +1 -0
  46. package/dist/ipv4/ipv4-prefix.js +81 -0
  47. package/dist/ipv4/ipv4-prefix.js.map +1 -0
  48. package/dist/ipv4/parse-ipv4.d.ts +49 -0
  49. package/dist/ipv4/parse-ipv4.d.ts.map +1 -0
  50. package/dist/ipv4/parse-ipv4.js +169 -0
  51. package/dist/ipv4/parse-ipv4.js.map +1 -0
  52. package/dist/ipv4/print-ipv4.d.ts +50 -0
  53. package/dist/ipv4/print-ipv4.d.ts.map +1 -0
  54. package/dist/ipv4/print-ipv4.js +76 -0
  55. package/dist/ipv4/print-ipv4.js.map +1 -0
  56. package/dist/ipv6/classify-ipv6.d.ts +79 -0
  57. package/dist/ipv6/classify-ipv6.d.ts.map +1 -0
  58. package/dist/ipv6/classify-ipv6.js +148 -0
  59. package/dist/ipv6/classify-ipv6.js.map +1 -0
  60. package/dist/ipv6/constants.d.ts +5 -0
  61. package/dist/ipv6/constants.d.ts.map +1 -0
  62. package/dist/ipv6/constants.js +5 -0
  63. package/dist/ipv6/constants.js.map +1 -0
  64. package/dist/ipv6/ipv4-mapped.d.ts +43 -0
  65. package/dist/ipv6/ipv4-mapped.d.ts.map +1 -0
  66. package/dist/ipv6/ipv4-mapped.js +65 -0
  67. package/dist/ipv6/ipv4-mapped.js.map +1 -0
  68. package/dist/ipv6/ipv6-prefix.d.ts +51 -0
  69. package/dist/ipv6/ipv6-prefix.d.ts.map +1 -0
  70. package/dist/ipv6/ipv6-prefix.js +78 -0
  71. package/dist/ipv6/ipv6-prefix.js.map +1 -0
  72. package/dist/ipv6/parse-ipv6.d.ts +50 -0
  73. package/dist/ipv6/parse-ipv6.d.ts.map +1 -0
  74. package/dist/ipv6/parse-ipv6.js +148 -0
  75. package/dist/ipv6/parse-ipv6.js.map +1 -0
  76. package/dist/ipv6/print-ipv6.d.ts +37 -0
  77. package/dist/ipv6/print-ipv6.d.ts.map +1 -0
  78. package/dist/ipv6/print-ipv6.js +93 -0
  79. package/dist/ipv6/print-ipv6.js.map +1 -0
  80. package/dist/port/endpoint.d.ts +47 -0
  81. package/dist/port/endpoint.d.ts.map +1 -0
  82. package/dist/port/endpoint.js +104 -0
  83. package/dist/port/endpoint.js.map +1 -0
  84. package/dist/port/ephemeral-port.d.ts +17 -0
  85. package/dist/port/ephemeral-port.d.ts.map +1 -0
  86. package/dist/port/ephemeral-port.js +26 -0
  87. package/dist/port/ephemeral-port.js.map +1 -0
  88. package/dist/port/port.d.ts +69 -0
  89. package/dist/port/port.d.ts.map +1 -0
  90. package/dist/port/port.js +94 -0
  91. package/dist/port/port.js.map +1 -0
  92. package/dist/prefix/_numeric-range.d.ts +23 -0
  93. package/dist/prefix/_numeric-range.d.ts.map +1 -0
  94. package/dist/prefix/_numeric-range.js +57 -0
  95. package/dist/prefix/_numeric-range.js.map +1 -0
  96. package/dist/prefix/_prefix-length.d.ts +7 -0
  97. package/dist/prefix/_prefix-length.d.ts.map +1 -0
  98. package/dist/prefix/_prefix-length.js +13 -0
  99. package/dist/prefix/_prefix-length.js.map +1 -0
  100. package/dist/prefix/aggregate.d.ts +45 -0
  101. package/dist/prefix/aggregate.d.ts.map +1 -0
  102. package/dist/prefix/aggregate.js +97 -0
  103. package/dist/prefix/aggregate.js.map +1 -0
  104. package/dist/prefix/bit-address.d.ts +110 -0
  105. package/dist/prefix/bit-address.d.ts.map +1 -0
  106. package/dist/prefix/bit-address.js +165 -0
  107. package/dist/prefix/bit-address.js.map +1 -0
  108. package/dist/prefix/bit-prefix.d.ts +79 -0
  109. package/dist/prefix/bit-prefix.d.ts.map +1 -0
  110. package/dist/prefix/bit-prefix.js +135 -0
  111. package/dist/prefix/bit-prefix.js.map +1 -0
  112. package/dist/prefix/bit-range.d.ts +49 -0
  113. package/dist/prefix/bit-range.d.ts.map +1 -0
  114. package/dist/prefix/bit-range.js +100 -0
  115. package/dist/prefix/bit-range.js.map +1 -0
  116. package/dist/prefix/compare.d.ts +21 -0
  117. package/dist/prefix/compare.d.ts.map +1 -0
  118. package/dist/prefix/compare.js +33 -0
  119. package/dist/prefix/compare.js.map +1 -0
  120. package/dist/prefix/mask.d.ts +26 -0
  121. package/dist/prefix/mask.d.ts.map +1 -0
  122. package/dist/prefix/mask.js +48 -0
  123. package/dist/prefix/mask.js.map +1 -0
  124. package/dist/prefix/set-ops.d.ts +38 -0
  125. package/dist/prefix/set-ops.d.ts.map +1 -0
  126. package/dist/prefix/set-ops.js +71 -0
  127. package/dist/prefix/set-ops.js.map +1 -0
  128. package/package.json +66 -0
@@ -0,0 +1,135 @@
1
+ import { assertPrefixLength } from "./_prefix-length.js";
2
+ import { assertSameBitWidth, bitAddressAnd, bitAddressCommonPrefixLength, bitAddressFromBigInt, bitAddressOr, bitAddressToBigInt, } from "./bit-address.js";
3
+ import { compareBitAddresses } from "./compare.js";
4
+ import { maskFromPrefixLength, wildcardMaskFromPrefixLength } from "./mask.js";
5
+ /**
6
+ * Pairs an address with a prefix length, keeping any host bits it carries.
7
+ *
8
+ * @throws RangeError When `prefixLength` is outside `[0, address.bitWidth]`.
9
+ */
10
+ export function createBitPrefix(address, prefixLength) {
11
+ assertPrefixLength(prefixLength, address.bitWidth);
12
+ return { address, prefixLength };
13
+ }
14
+ /** The netmask matching the prefix length. */
15
+ export function bitPrefixNetmask(prefix) {
16
+ return maskFromPrefixLength(prefix.prefixLength, prefix.address.bitWidth);
17
+ }
18
+ /** The wildcard mask matching the prefix length. */
19
+ export function bitPrefixWildcardMask(prefix) {
20
+ return wildcardMaskFromPrefixLength(prefix.prefixLength, prefix.address.bitWidth);
21
+ }
22
+ /** The lowest address covered — the network address, host bits cleared. */
23
+ export function bitPrefixFirstAddress(prefix) {
24
+ return bitAddressAnd(prefix.address, bitPrefixNetmask(prefix));
25
+ }
26
+ /** The highest address covered — host bits set. */
27
+ export function bitPrefixLastAddress(prefix) {
28
+ return bitAddressOr(bitPrefixFirstAddress(prefix), bitPrefixWildcardMask(prefix));
29
+ }
30
+ /** The same prefix with its host bits cleared. */
31
+ export function normalizeBitPrefix(prefix) {
32
+ return {
33
+ address: bitPrefixFirstAddress(prefix),
34
+ prefixLength: prefix.prefixLength,
35
+ };
36
+ }
37
+ /** Whether the prefix's address already has every host bit clear. */
38
+ export function isNormalizedBitPrefix(prefix) {
39
+ return (compareBitAddresses(prefix.address, bitPrefixFirstAddress(prefix)) === 0);
40
+ }
41
+ /** How many addresses the prefix covers: `2 ** (bitWidth - prefixLength)`. */
42
+ export function bitPrefixAddressCount(prefix) {
43
+ return 1n << BigInt(prefix.address.bitWidth - prefix.prefixLength);
44
+ }
45
+ /**
46
+ * Whether `address` falls inside `prefix`.
47
+ *
48
+ * @throws RangeError When the widths differ.
49
+ */
50
+ export function bitPrefixContains(prefix, address) {
51
+ assertSameBitWidth(prefix.address, address);
52
+ return (bitAddressCommonPrefixLength(prefix.address, address) >= prefix.prefixLength);
53
+ }
54
+ /**
55
+ * Whether every address of `inner` falls inside `outer`.
56
+ *
57
+ * @throws RangeError When the widths differ.
58
+ */
59
+ export function bitPrefixContainsPrefix(outer, inner) {
60
+ return (inner.prefixLength >= outer.prefixLength &&
61
+ bitPrefixContains(outer, inner.address));
62
+ }
63
+ /**
64
+ * Whether `subject` is contained in `container`. The inverse reading of
65
+ * {@link bitPrefixContainsPrefix}, kept because routing code says it this way.
66
+ */
67
+ export function bitPrefixIsSubnetOf(subject, container) {
68
+ return bitPrefixContainsPrefix(container, subject);
69
+ }
70
+ /** Whether `subject` contains `contained`. */
71
+ export function bitPrefixIsSupernetOf(subject, contained) {
72
+ return bitPrefixContainsPrefix(subject, contained);
73
+ }
74
+ /**
75
+ * Whether the two prefixes share any address. Two prefixes of the same family
76
+ * either nest or are disjoint, so this is "one contains the other".
77
+ *
78
+ * @throws RangeError When the widths differ.
79
+ */
80
+ export function bitPrefixOverlaps(a, b) {
81
+ assertSameBitWidth(a.address, b.address);
82
+ return (bitAddressCommonPrefixLength(a.address, b.address) >=
83
+ Math.min(a.prefixLength, b.prefixLength));
84
+ }
85
+ /** Whether both cover exactly the same addresses. Host bits are ignored. */
86
+ export function bitPrefixEquals(a, b) {
87
+ return (a.prefixLength === b.prefixLength &&
88
+ compareBitAddresses(bitPrefixFirstAddress(a), bitPrefixFirstAddress(b)) ===
89
+ 0);
90
+ }
91
+ /**
92
+ * Orders by network address, then by prefix length — so a supernet sorts before
93
+ * every subnet it contains. Exact, and agrees with {@link bitPrefixEquals}.
94
+ */
95
+ export function compareBitPrefixes(a, b) {
96
+ const byNetwork = compareBitAddresses(bitPrefixFirstAddress(a), bitPrefixFirstAddress(b));
97
+ if (byNetwork !== 0) {
98
+ return byNetwork;
99
+ }
100
+ if (a.prefixLength === b.prefixLength) {
101
+ return 0;
102
+ }
103
+ return a.prefixLength < b.prefixLength ? -1 : 1;
104
+ }
105
+ /**
106
+ * The prefix one bit shorter — the block this one was halved out of.
107
+ *
108
+ * @returns `null` for a `/0`, which has no supernet.
109
+ */
110
+ export function bitPrefixSupernet(prefix) {
111
+ if (prefix.prefixLength === 0) {
112
+ return null;
113
+ }
114
+ return normalizeBitPrefix({
115
+ address: prefix.address,
116
+ prefixLength: prefix.prefixLength - 1,
117
+ });
118
+ }
119
+ /**
120
+ * The other half of this prefix's supernet — the block it would merge with.
121
+ *
122
+ * @returns `null` for a `/0`, which has no sibling.
123
+ */
124
+ export function bitPrefixSibling(prefix) {
125
+ if (prefix.prefixLength === 0) {
126
+ return null;
127
+ }
128
+ const network = bitPrefixFirstAddress(prefix);
129
+ const siblingBit = 1n << BigInt(network.bitWidth - prefix.prefixLength);
130
+ return {
131
+ address: bitAddressFromBigInt(bitAddressToBigInt(network) ^ siblingBit, network.bitWidth),
132
+ prefixLength: prefix.prefixLength,
133
+ };
134
+ }
135
+ //# sourceMappingURL=bit-prefix.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bit-prefix.js","sourceRoot":"","sources":["../../src/prefix/bit-prefix.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAEN,kBAAkB,EAClB,aAAa,EACb,4BAA4B,EAC5B,oBAAoB,EACpB,YAAY,EACZ,kBAAkB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,4BAA4B,EAAE,MAAM,WAAW,CAAC;AAc/E;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC9B,OAAmB,EACnB,YAAoB;IAEpB,kBAAkB,CAAC,YAAY,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEnD,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;AAClC,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IACjD,OAAO,oBAAoB,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC3E,CAAC;AAED,oDAAoD;AACpD,MAAM,UAAU,qBAAqB,CAAC,MAAiB;IACtD,OAAO,4BAA4B,CAClC,MAAM,CAAC,YAAY,EACnB,MAAM,CAAC,OAAO,CAAC,QAAQ,CACvB,CAAC;AACH,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,qBAAqB,CAAC,MAAiB;IACtD,OAAO,aAAa,CAAC,MAAM,CAAC,OAAO,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC;AAChE,CAAC;AAED,mDAAmD;AACnD,MAAM,UAAU,oBAAoB,CAAC,MAAiB;IACrD,OAAO,YAAY,CAClB,qBAAqB,CAAC,MAAM,CAAC,EAC7B,qBAAqB,CAAC,MAAM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AAkB,CAAC;QACzB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,YAAY,EAAE,MAAM,CAAC,YAAY,GAAG,CAAC;KACrC,CAAC,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IACjD,IAAI,MAAM,CAAC,YAAY,KAAK,CAAC,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,OAAO,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC9C,MAAM,UAAU,GAAG,EAAE,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IAExE,OAAO;QACN,OAAO,EAAE,oBAAoB,CAC5B,kBAAkB,CAAC,OAAO,CAAC,GAAG,UAAU,EACxC,OAAO,CAAC,QAAQ,CAChB;QACD,YAAY,EAAE,MAAM,CAAC,YAAY;KACjC,CAAC;AACH,CAAC"}
@@ -0,0 +1,49 @@
1
+ import { type ComparatorResult } from "@ac-kit/core";
2
+ import { type BitAddress } from "./bit-address.js";
3
+ import { type BitPrefix } from "./bit-prefix.js";
4
+ /**
5
+ * An arbitrary inclusive run of addresses — `10.0.0.5` through `10.0.0.9` —
6
+ * which no single prefix can express.
7
+ */
8
+ export type BitRange = {
9
+ readonly first: BitAddress;
10
+ readonly last: BitAddress;
11
+ };
12
+ /**
13
+ * Pairs two bounds into an inclusive range.
14
+ *
15
+ * @throws RangeError When the widths differ or `last` precedes `first`.
16
+ */
17
+ export declare function createBitRange(first: BitAddress, last: BitAddress): BitRange;
18
+ /** The addresses a prefix covers, as a range. */
19
+ export declare function bitRangeFromPrefix(prefix: BitPrefix): BitRange;
20
+ /** How many addresses the range covers, both bounds included. */
21
+ export declare function bitRangeAddressCount(range: BitRange): bigint;
22
+ /**
23
+ * Whether `address` falls within the range.
24
+ *
25
+ * @throws RangeError When the widths differ.
26
+ */
27
+ export declare function bitRangeContains(range: BitRange, address: BitAddress): boolean;
28
+ /**
29
+ * Whether the two ranges share any address.
30
+ *
31
+ * @throws RangeError When the widths differ.
32
+ */
33
+ export declare function bitRangeOverlaps(a: BitRange, b: BitRange): boolean;
34
+ /** Whether both ranges have the same bounds. */
35
+ export declare function bitRangeEquals(a: BitRange, b: BitRange): boolean;
36
+ /** Orders by start address, then by end address. Exact. */
37
+ export declare function compareBitRanges(a: BitRange, b: BitRange): ComparatorResult;
38
+ /**
39
+ * The shortest list of prefixes covering exactly the range and nothing more.
40
+ *
41
+ * At each step it emits the largest block that both starts at the current
42
+ * address and fits in what is left, which is the standard range-to-CIDR
43
+ * decomposition. At most `2 * bitWidth - 2` prefixes come out, so the result is
44
+ * bounded however wide the range is.
45
+ *
46
+ * @throws RangeError When the widths differ or `last` precedes `first`.
47
+ */
48
+ export declare function bitRangeToPrefixes(range: BitRange): BitPrefix[];
49
+ //# sourceMappingURL=bit-range.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bit-range.d.ts","sourceRoot":"","sources":["../../src/prefix/bit-range.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,gBAAgB,EAAmB,MAAM,cAAc,CAAC;AAEtE,OAAO,EACN,KAAK,UAAU,EAIf,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACN,KAAK,SAAS,EAGd,MAAM,iBAAiB,CAAC;AAGzB;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG;IACtB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;CAC1B,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,QAAQ,CAQ5E;AAED,iDAAiD;AACjD,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,QAAQ,CAK9D;AAED,iEAAiE;AACjE,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,CAE5D;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC/B,KAAK,EAAE,QAAQ,EACf,OAAO,EAAE,UAAU,GACjB,OAAO,CAOT;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,OAAO,CAOlE;AAED,gDAAgD;AAChD,wBAAgB,cAAc,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,OAAO,CAKhE;AAED,2DAA2D;AAC3D,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,gBAAgB,CAI3E;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,EAAE,CAiC/D"}
@@ -0,0 +1,100 @@
1
+ import { bigIntBitLength } from "@ac-kit/core";
2
+ import { assertSameBitWidth, bitAddressFromBigInt, bitAddressToBigInt, } from "./bit-address.js";
3
+ import { bitPrefixFirstAddress, bitPrefixLastAddress, } from "./bit-prefix.js";
4
+ import { compareBitAddresses } from "./compare.js";
5
+ /**
6
+ * Pairs two bounds into an inclusive range.
7
+ *
8
+ * @throws RangeError When the widths differ or `last` precedes `first`.
9
+ */
10
+ export function createBitRange(first, last) {
11
+ assertSameBitWidth(first, last);
12
+ if (compareBitAddresses(first, last) > 0) {
13
+ throw new RangeError("range end precedes its start");
14
+ }
15
+ return { first, last };
16
+ }
17
+ /** The addresses a prefix covers, as a range. */
18
+ export function bitRangeFromPrefix(prefix) {
19
+ return {
20
+ first: bitPrefixFirstAddress(prefix),
21
+ last: bitPrefixLastAddress(prefix),
22
+ };
23
+ }
24
+ /** How many addresses the range covers, both bounds included. */
25
+ export function bitRangeAddressCount(range) {
26
+ return bitAddressToBigInt(range.last) - bitAddressToBigInt(range.first) + 1n;
27
+ }
28
+ /**
29
+ * Whether `address` falls within the range.
30
+ *
31
+ * @throws RangeError When the widths differ.
32
+ */
33
+ export function bitRangeContains(range, address) {
34
+ assertSameBitWidth(range.first, address);
35
+ return (compareBitAddresses(range.first, address) <= 0 &&
36
+ compareBitAddresses(address, range.last) <= 0);
37
+ }
38
+ /**
39
+ * Whether the two ranges share any address.
40
+ *
41
+ * @throws RangeError When the widths differ.
42
+ */
43
+ export function bitRangeOverlaps(a, b) {
44
+ assertSameBitWidth(a.first, b.first);
45
+ return (compareBitAddresses(a.first, b.last) <= 0 &&
46
+ compareBitAddresses(b.first, a.last) <= 0);
47
+ }
48
+ /** Whether both ranges have the same bounds. */
49
+ export function bitRangeEquals(a, b) {
50
+ return (compareBitAddresses(a.first, b.first) === 0 &&
51
+ compareBitAddresses(a.last, b.last) === 0);
52
+ }
53
+ /** Orders by start address, then by end address. Exact. */
54
+ export function compareBitRanges(a, b) {
55
+ const byFirst = compareBitAddresses(a.first, b.first);
56
+ return byFirst === 0 ? compareBitAddresses(a.last, b.last) : byFirst;
57
+ }
58
+ /**
59
+ * The shortest list of prefixes covering exactly the range and nothing more.
60
+ *
61
+ * At each step it emits the largest block that both starts at the current
62
+ * address and fits in what is left, which is the standard range-to-CIDR
63
+ * decomposition. At most `2 * bitWidth - 2` prefixes come out, so the result is
64
+ * bounded however wide the range is.
65
+ *
66
+ * @throws RangeError When the widths differ or `last` precedes `first`.
67
+ */
68
+ export function bitRangeToPrefixes(range) {
69
+ assertSameBitWidth(range.first, range.last);
70
+ const { bitWidth } = range.first;
71
+ const last = bitAddressToBigInt(range.last);
72
+ let current = bitAddressToBigInt(range.first);
73
+ if (current > last) {
74
+ throw new RangeError("range end precedes its start");
75
+ }
76
+ const prefixes = [];
77
+ for (;;) {
78
+ // The block may be no larger than the alignment of its start, and no
79
+ // larger than what is left to cover.
80
+ const alignment = current === 0n ? bitWidth : lowestSetBitIndex(current);
81
+ const remaining = last - current + 1n;
82
+ const hostBits = Math.min(alignment, bigIntBitLength(remaining) - 1);
83
+ prefixes.push({
84
+ address: bitAddressFromBigInt(current, bitWidth),
85
+ prefixLength: bitWidth - hostBits,
86
+ });
87
+ const step = 1n << BigInt(hostBits);
88
+ if (current + step > last) {
89
+ return prefixes;
90
+ }
91
+ current += step;
92
+ }
93
+ }
94
+ /** The index of the lowest set bit of a strictly positive `value`. */
95
+ function lowestSetBitIndex(value) {
96
+ // `value & -value` isolates that bit, so its bit length names the index.
97
+ // Not `ctz64`: an IPv6 address is 128 bits wide.
98
+ return bigIntBitLength(value & -value) - 1;
99
+ }
100
+ //# sourceMappingURL=bit-range.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bit-range.js","sourceRoot":"","sources":["../../src/prefix/bit-range.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,eAAe,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,EAEN,kBAAkB,EAClB,oBAAoB,EACpB,kBAAkB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAEN,qBAAqB,EACrB,oBAAoB,GACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAWnD;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,KAAiB,EAAE,IAAgB;IACjE,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAEhC,IAAI,mBAAmB,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,UAAU,CAAC,8BAA8B,CAAC,CAAC;IACtD,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AACxB,CAAC;AAED,iDAAiD;AACjD,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IACnD,OAAO;QACN,KAAK,EAAE,qBAAqB,CAAC,MAAM,CAAC;QACpC,IAAI,EAAE,oBAAoB,CAAC,MAAM,CAAC;KAClC,CAAC;AACH,CAAC;AAED,iEAAiE;AACjE,MAAM,UAAU,oBAAoB,CAAC,KAAe;IACnD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,CAAC;IACjB,CAAC;AACF,CAAC;AAED,sEAAsE;AACtE,SAAS,iBAAiB,CAAC,KAAa;IACvC,yEAAyE;IACzE,iDAAiD;IACjD,OAAO,eAAe,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { ComparatorResult } from "@ac-kit/core";
2
+ import type { BitAddress } from "./bit-address.js";
3
+ /**
4
+ * Orders two addresses exactly, narrower widths first and then by unsigned
5
+ * big-endian value.
6
+ *
7
+ * Mixed widths are ordered rather than rejected so that a heterogeneous
8
+ * collection still sorts; within one width the order is the numeric one. Exact,
9
+ * as every comparator in this tree is — there is no tolerance to apply to a bit
10
+ * string. O(1).
11
+ */
12
+ export declare function compareBitAddresses(a: BitAddress, b: BitAddress): ComparatorResult;
13
+ /** Whether two addresses have the same width and the same bits. */
14
+ export declare function bitAddressEquals(a: BitAddress, b: BitAddress): boolean;
15
+ /**
16
+ * A new array holding `addresses` in {@link compareBitAddresses} order.
17
+ *
18
+ * O(n log n) comparisons, each O(bitWidth / 8); allocates one array of `n`.
19
+ */
20
+ export declare function sortBitAddresses(addresses: Iterable<BitAddress>): BitAddress[];
21
+ //# sourceMappingURL=compare.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../../src/prefix/compare.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAClC,CAAC,EAAE,UAAU,EACb,CAAC,EAAE,UAAU,GACX,gBAAgB,CAYlB;AAED,mEAAmE;AACnE,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAEtE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC/B,SAAS,EAAE,QAAQ,CAAC,UAAU,CAAC,GAC7B,UAAU,EAAE,CAEd"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Orders two addresses exactly, narrower widths first and then by unsigned
3
+ * big-endian value.
4
+ *
5
+ * Mixed widths are ordered rather than rejected so that a heterogeneous
6
+ * collection still sorts; within one width the order is the numeric one. Exact,
7
+ * as every comparator in this tree is — there is no tolerance to apply to a bit
8
+ * string. O(1).
9
+ */
10
+ export function compareBitAddresses(a, b) {
11
+ // Width first: `255.255.255.255` is the larger number but the narrower
12
+ // address, and must still sort before any 128-bit one.
13
+ if (a.bitWidth !== b.bitWidth) {
14
+ return a.bitWidth < b.bitWidth ? -1 : 1;
15
+ }
16
+ if (a.value === b.value) {
17
+ return 0;
18
+ }
19
+ return a.value < b.value ? -1 : 1;
20
+ }
21
+ /** Whether two addresses have the same width and the same bits. */
22
+ export function bitAddressEquals(a, b) {
23
+ return compareBitAddresses(a, b) === 0;
24
+ }
25
+ /**
26
+ * A new array holding `addresses` in {@link compareBitAddresses} order.
27
+ *
28
+ * O(n log n) comparisons, each O(bitWidth / 8); allocates one array of `n`.
29
+ */
30
+ export function sortBitAddresses(addresses) {
31
+ return Array.from(addresses).sort(compareBitAddresses);
32
+ }
33
+ //# sourceMappingURL=compare.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compare.js","sourceRoot":"","sources":["../../src/prefix/compare.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAClC,CAAa,EACb,CAAa;IAEb,uEAAuE;IACvE,uDAAuD;IACvD,IAAI,CAAC,CAAC,QAAQ,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;QAC/B,OAAO,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACzC,CAAC;IAED,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,KAAK,EAAE,CAAC;QACzB,OAAO,CAAC,CAAC;IACV,CAAC;IAED,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,gBAAgB,CAAC,CAAa,EAAE,CAAa;IAC5D,OAAO,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC/B,SAA+B;IAE/B,OAAO,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;AACxD,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { type BitAddress } from "./bit-address.js";
2
+ /**
3
+ * The netmask for a prefix length: `prefixLength` leading ones, the rest zero.
4
+ *
5
+ * @throws RangeError When `bitWidth` is not a positive multiple of 8, or
6
+ * `prefixLength` is outside `[0, bitWidth]`.
7
+ */
8
+ export declare function maskFromPrefixLength(prefixLength: number, bitWidth: number): BitAddress;
9
+ /**
10
+ * The wildcard (inverse) mask for a prefix length, as Cisco ACLs spell it:
11
+ * `prefixLength` leading zeros, the rest ones.
12
+ *
13
+ * @throws RangeError When `bitWidth` is not a positive multiple of 8, or
14
+ * `prefixLength` is outside `[0, bitWidth]`.
15
+ */
16
+ export declare function wildcardMaskFromPrefixLength(prefixLength: number, bitWidth: number): BitAddress;
17
+ /** Whether a mask is a run of ones followed by a run of zeros. */
18
+ export declare function isContiguousMask(mask: BitAddress): boolean;
19
+ /**
20
+ * How many leading ones a netmask has.
21
+ *
22
+ * @throws RangeError When the mask is not contiguous — `255.0.255.0` names no
23
+ * prefix length, and rounding it to one would silently widen an ACL.
24
+ */
25
+ export declare function prefixLengthFromMask(mask: BitAddress): number;
26
+ //# sourceMappingURL=mask.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mask.d.ts","sourceRoot":"","sources":["../../src/prefix/mask.ts"],"names":[],"mappings":"AAQA,OAAO,EACN,KAAK,UAAU,EAGf,MAAM,kBAAkB,CAAC;AAE1B;;;;;GAKG;AACH,wBAAgB,oBAAoB,CACnC,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,MAAM,GACd,UAAU,CAWZ;AAED;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC3C,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,MAAM,GACd,UAAU,CAOZ;AAED,kEAAkE;AAClE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAM1D;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAW7D"}
@@ -0,0 +1,48 @@
1
+ import { bigIntBitLength, bigIntIsPowerOfTwo } from "@ac-kit/core";
2
+ import { fixedUIntBigMax, fixedUIntBigNot, fixedUIntBigShiftLeft, } from "@ac-kit/math-numbers";
3
+ import { assertPrefixLength } from "./_prefix-length.js";
4
+ import { bitAddressFromBigInt, bitAddressToBigInt, } from "./bit-address.js";
5
+ /**
6
+ * The netmask for a prefix length: `prefixLength` leading ones, the rest zero.
7
+ *
8
+ * @throws RangeError When `bitWidth` is not a positive multiple of 8, or
9
+ * `prefixLength` is outside `[0, bitWidth]`.
10
+ */
11
+ export function maskFromPrefixLength(prefixLength, bitWidth) {
12
+ assertPrefixLength(prefixLength, bitWidth);
13
+ return bitAddressFromBigInt(fixedUIntBigShiftLeft(fixedUIntBigMax(prefixLength), bitWidth - prefixLength, bitWidth), bitWidth);
14
+ }
15
+ /**
16
+ * The wildcard (inverse) mask for a prefix length, as Cisco ACLs spell it:
17
+ * `prefixLength` leading zeros, the rest ones.
18
+ *
19
+ * @throws RangeError When `bitWidth` is not a positive multiple of 8, or
20
+ * `prefixLength` is outside `[0, bitWidth]`.
21
+ */
22
+ export function wildcardMaskFromPrefixLength(prefixLength, bitWidth) {
23
+ assertPrefixLength(prefixLength, bitWidth);
24
+ return bitAddressFromBigInt(fixedUIntBigMax(bitWidth - prefixLength), bitWidth);
25
+ }
26
+ /** Whether a mask is a run of ones followed by a run of zeros. */
27
+ export function isContiguousMask(mask) {
28
+ // The complement of a left-aligned run of ones is a right-aligned run of
29
+ // ones, and those are exactly the values one below a power of two.
30
+ const complement = fixedUIntBigNot(bitAddressToBigInt(mask), mask.bitWidth);
31
+ return complement === 0n || bigIntIsPowerOfTwo(complement + 1n);
32
+ }
33
+ /**
34
+ * How many leading ones a netmask has.
35
+ *
36
+ * @throws RangeError When the mask is not contiguous — `255.0.255.0` names no
37
+ * prefix length, and rounding it to one would silently widen an ACL.
38
+ */
39
+ export function prefixLengthFromMask(mask) {
40
+ if (!isContiguousMask(mask)) {
41
+ throw new RangeError("netmask is not a contiguous run of leading ones");
42
+ }
43
+ // The trailing zeros are the wildcard part, and on a contiguous mask its
44
+ // complement is exactly that many low bits set.
45
+ return (mask.bitWidth -
46
+ bigIntBitLength(fixedUIntBigNot(bitAddressToBigInt(mask), mask.bitWidth)));
47
+ }
48
+ //# sourceMappingURL=mask.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mask.js","sourceRoot":"","sources":["../../src/prefix/mask.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,EACN,eAAe,EACf,eAAe,EACf,qBAAqB,GACrB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAEN,oBAAoB,EACpB,kBAAkB,GAClB,MAAM,kBAAkB,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CACnC,YAAoB,EACpB,QAAgB;IAEhB,kBAAkB,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IAE3C,OAAO,oBAAoB,CAC1B,qBAAqB,CACpB,eAAe,CAAC,YAAY,CAAC,EAC7B,QAAQ,GAAG,YAAY,EACvB,QAAQ,CACR,EACD,QAAQ,CACR,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,4BAA4B,CAC3C,YAAoB,EACpB,QAAgB;IAEhB,kBAAkB,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IAE3C,OAAO,oBAAoB,CAC1B,eAAe,CAAC,QAAQ,GAAG,YAAY,CAAC,EACxC,QAAQ,CACR,CAAC;AACH,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,gBAAgB,CAAC,IAAgB;IAChD,yEAAyE;IACzE,mEAAmE;IACnE,MAAM,UAAU,GAAG,eAAe,CAAC,kBAAkB,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IAE5E,OAAO,UAAU,KAAK,EAAE,IAAI,kBAAkB,CAAC,UAAU,GAAG,EAAE,CAAC,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAAgB;IACpD,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,UAAU,CAAC,iDAAiD,CAAC,CAAC;IACzE,CAAC;IAED,yEAAyE;IACzE,gDAAgD;IAChD,OAAO,CACN,IAAI,CAAC,QAAQ;QACb,eAAe,CAAC,eAAe,CAAC,kBAAkB,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CACzE,CAAC;AACH,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { BitAddress } from "./bit-address.js";
2
+ import { type BitPrefix } from "./bit-prefix.js";
3
+ /**
4
+ * Every address in either collection, as the shortest prefix list.
5
+ *
6
+ * O((n + m) log (n + m)).
7
+ *
8
+ * @throws RangeError When the two collections mix address widths.
9
+ */
10
+ export declare function unionBitPrefixes(a: Iterable<BitPrefix>, b: Iterable<BitPrefix>): BitPrefix[];
11
+ /**
12
+ * Every address in both collections, as the shortest prefix list.
13
+ *
14
+ * O((n + m) log (n + m)).
15
+ *
16
+ * @throws RangeError When the two collections mix address widths.
17
+ */
18
+ export declare function intersectBitPrefixes(a: Iterable<BitPrefix>, b: Iterable<BitPrefix>): BitPrefix[];
19
+ /**
20
+ * Every address in `a` but not in `b`, as the shortest prefix list.
21
+ *
22
+ * This is how a firewall carves an exception out of an allowlist, so the result
23
+ * is exact: no address of `b` survives, and none of `a` is lost. O((n + m) log
24
+ * (n + m)).
25
+ *
26
+ * @throws RangeError When the two collections mix address widths.
27
+ */
28
+ export declare function subtractBitPrefixes(a: Iterable<BitPrefix>, b: Iterable<BitPrefix>): BitPrefix[];
29
+ /**
30
+ * Whether any prefix in the collection covers `address`.
31
+ *
32
+ * Linear. A routing table queried repeatedly wants a longest-prefix-match
33
+ * structure instead — see this package's `TODO.md`.
34
+ */
35
+ export declare function bitPrefixesContain(prefixes: Iterable<BitPrefix>, address: BitAddress): boolean;
36
+ /** Whether every address of `b` is also in `a`. */
37
+ export declare function bitPrefixesContainAll(a: Iterable<BitPrefix>, b: Iterable<BitPrefix>): boolean;
38
+ //# sourceMappingURL=set-ops.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-ops.d.ts","sourceRoot":"","sources":["../../src/prefix/set-ops.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,KAAK,SAAS,EAAqB,MAAM,iBAAiB,CAAC;AAEpE;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC/B,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,EACtB,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,GACpB,SAAS,EAAE,CAQb;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CACnC,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,EACtB,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,GACpB,SAAS,EAAE,CAkBb;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAClC,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,EACtB,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,GACpB,SAAS,EAAE,CAqBb;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CACjC,QAAQ,EAAE,QAAQ,CAAC,SAAS,CAAC,EAC7B,OAAO,EAAE,UAAU,GACjB,OAAO,CAQT;AAED,mDAAmD;AACnD,wBAAgB,qBAAqB,CACpC,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,EACtB,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,GACpB,OAAO,CAET"}
@@ -0,0 +1,71 @@
1
+ import { BIG_INT_INTERVAL_STEP, intersectIntervals, subtractIntervals, } from "@ac-kit/algo";
2
+ import { compareNaturalAscending } from "@ac-kit/core";
3
+ import { commonBitWidth, fromNumericRanges, toNumericRanges, } from "./_numeric-range.js";
4
+ import { bitPrefixContains } from "./bit-prefix.js";
5
+ /**
6
+ * Every address in either collection, as the shortest prefix list.
7
+ *
8
+ * O((n + m) log (n + m)).
9
+ *
10
+ * @throws RangeError When the two collections mix address widths.
11
+ */
12
+ export function unionBitPrefixes(a, b) {
13
+ const list = [...a, ...b];
14
+ if (list.length === 0) {
15
+ return [];
16
+ }
17
+ return fromNumericRanges(toNumericRanges(list), commonBitWidth(list));
18
+ }
19
+ /**
20
+ * Every address in both collections, as the shortest prefix list.
21
+ *
22
+ * O((n + m) log (n + m)).
23
+ *
24
+ * @throws RangeError When the two collections mix address widths.
25
+ */
26
+ export function intersectBitPrefixes(a, b) {
27
+ const left = Array.from(a);
28
+ const right = Array.from(b);
29
+ if (left.length === 0 || right.length === 0) {
30
+ return [];
31
+ }
32
+ const bitWidth = commonBitWidth([...left, ...right]);
33
+ return fromNumericRanges(intersectIntervals(toNumericRanges(left), toNumericRanges(right), compareNaturalAscending), bitWidth);
34
+ }
35
+ /**
36
+ * Every address in `a` but not in `b`, as the shortest prefix list.
37
+ *
38
+ * This is how a firewall carves an exception out of an allowlist, so the result
39
+ * is exact: no address of `b` survives, and none of `a` is lost. O((n + m) log
40
+ * (n + m)).
41
+ *
42
+ * @throws RangeError When the two collections mix address widths.
43
+ */
44
+ export function subtractBitPrefixes(a, b) {
45
+ const left = Array.from(a);
46
+ const right = Array.from(b);
47
+ if (left.length === 0) {
48
+ return [];
49
+ }
50
+ const bitWidth = commonBitWidth(right.length === 0 ? left : [...left, ...right]);
51
+ return fromNumericRanges(subtractIntervals(toNumericRanges(left), toNumericRanges(right), compareNaturalAscending, BIG_INT_INTERVAL_STEP), bitWidth);
52
+ }
53
+ /**
54
+ * Whether any prefix in the collection covers `address`.
55
+ *
56
+ * Linear. A routing table queried repeatedly wants a longest-prefix-match
57
+ * structure instead — see this package's `TODO.md`.
58
+ */
59
+ export function bitPrefixesContain(prefixes, address) {
60
+ for (const prefix of prefixes) {
61
+ if (bitPrefixContains(prefix, address)) {
62
+ return true;
63
+ }
64
+ }
65
+ return false;
66
+ }
67
+ /** Whether every address of `b` is also in `a`. */
68
+ export function bitPrefixesContainAll(a, b) {
69
+ return subtractBitPrefixes(b, a).length === 0;
70
+ }
71
+ //# sourceMappingURL=set-ops.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-ops.js","sourceRoot":"","sources":["../../src/prefix/set-ops.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,qBAAqB,EACrB,kBAAkB,EAClB,iBAAiB,GACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEvD,OAAO,EACN,cAAc,EACd,iBAAiB,EACjB,eAAe,GACf,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAkB,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpE;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAC/B,CAAsB,EACtB,CAAsB;IAEtB,MAAM,IAAI,GAAG,CAAC,G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E,GAAG,KAAK,CAAC,CAC/C,CAAC;IAEF,OAAO,iBAAiB,CACvB,iBAAiB,CAChB,eAAe,CAAC,IAAI,CAAC,EACrB,eAAe,CAAC,KAAK,CAAC,EACtB,uBAAuB,EACvB,qBAAqB,CACrB,EACD,QAAQ,CACR,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CACjC,QAA6B,EAC7B,OAAmB;IAEnB,KAAK,MAAM,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC/B,IAAI,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;YACxC,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC;AAED,mDAAmD;AACnD,MAAM,UAAU,qBAAqB,CACpC,CAAsB,EACtB,CAAsB;IAEtB,OAAO,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;AAC/C,CAAC"}
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@ac-kit/net-address",
3
+ "version": "0.2.0",
4
+ "description": "Network addresses: IPv4, IPv6, EUI-48/EUI-64 and ports, over one fixed-width prefix core.",
5
+ "keywords": [
6
+ "cidr",
7
+ "eui64",
8
+ "ipv4",
9
+ "ipv6",
10
+ "mac-address",
11
+ "subnet"
12
+ ],
13
+ "homepage": "https://anthochamp.github.io/node-essentials/api/net-address/",
14
+ "bugs": {
15
+ "url": "https://github.com/anthochamp/node-essentials/issues"
16
+ },
17
+ "license": "BSD-3-Clause",
18
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
22
+ "directory": "packages/net-address"
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "type": "module",
28
+ "sideEffects": false,
29
+ "main": "./dist/index.js",
30
+ "exports": {
31
+ ".": "./dist/index.js"
32
+ },
33
+ "publishConfig": {
34
+ "main": "./dist/index.js",
35
+ "exports": {
36
+ ".": "./dist/index.js"
37
+ },
38
+ "access": "public"
39
+ },
40
+ "scripts": {
41
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
42
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
43
+ "clean": "rimraf dist",
44
+ "build": "yarn clean && yarn compile",
45
+ "test": "yarn run -T vitest run",
46
+ "prepack": "yarn build"
47
+ },
48
+ "dependencies": {
49
+ "@ac-kit/algo": "^0.2.0",
50
+ "@ac-kit/core": "^0.3.0",
51
+ "@ac-kit/math-numbers": "^0.2.0",
52
+ "@types/web": "^0.0.354"
53
+ },
54
+ "devDependencies": {
55
+ "@ac-kit/tsconfig": "0.1.0",
56
+ "@ac-kit/typedoc-config": "0.1.0",
57
+ "@ac-kit/vitest-config": "0.1.1",
58
+ "@typescript/native": "typescript@7.0.2",
59
+ "rimraf": "6.1.3",
60
+ "typedoc": "0.28.20",
61
+ "typescript": "@typescript/typescript6@6.0.2"
62
+ },
63
+ "engines": {
64
+ "node": ">=24"
65
+ }
66
+ }