@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.
- package/CHANGELOG.md +46 -0
- package/LICENSE +28 -0
- package/README.md +65 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +10 -0
- package/dist/errors.js.map +1 -0
- package/dist/eui/constants.d.ts +7 -0
- package/dist/eui/constants.d.ts.map +1 -0
- package/dist/eui/constants.js +7 -0
- package/dist/eui/constants.js.map +1 -0
- package/dist/eui/eui-bits.d.ts +99 -0
- package/dist/eui/eui-bits.d.ts.map +1 -0
- package/dist/eui/eui-bits.js +138 -0
- package/dist/eui/eui-bits.js.map +1 -0
- package/dist/eui/eui-convert.d.ts +37 -0
- package/dist/eui/eui-convert.d.ts.map +1 -0
- package/dist/eui/eui-convert.js +76 -0
- package/dist/eui/eui-convert.js.map +1 -0
- package/dist/eui/interface-identifier.d.ts +24 -0
- package/dist/eui/interface-identifier.d.ts.map +1 -0
- package/dist/eui/interface-identifier.js +47 -0
- package/dist/eui/interface-identifier.js.map +1 -0
- package/dist/eui/parse-eui.d.ts +31 -0
- package/dist/eui/parse-eui.d.ts.map +1 -0
- package/dist/eui/parse-eui.js +93 -0
- package/dist/eui/parse-eui.js.map +1 -0
- package/dist/eui/print-eui.d.ts +44 -0
- package/dist/eui/print-eui.d.ts.map +1 -0
- package/dist/eui/print-eui.js +64 -0
- package/dist/eui/print-eui.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/ipv4/classify-ipv4.d.ts +69 -0
- package/dist/ipv4/classify-ipv4.d.ts.map +1 -0
- package/dist/ipv4/classify-ipv4.js +129 -0
- package/dist/ipv4/classify-ipv4.js.map +1 -0
- package/dist/ipv4/constants.d.ts +5 -0
- package/dist/ipv4/constants.d.ts.map +1 -0
- package/dist/ipv4/constants.js +5 -0
- package/dist/ipv4/constants.js.map +1 -0
- package/dist/ipv4/ipv4-prefix.d.ts +52 -0
- package/dist/ipv4/ipv4-prefix.d.ts.map +1 -0
- package/dist/ipv4/ipv4-prefix.js +81 -0
- package/dist/ipv4/ipv4-prefix.js.map +1 -0
- package/dist/ipv4/parse-ipv4.d.ts +49 -0
- package/dist/ipv4/parse-ipv4.d.ts.map +1 -0
- package/dist/ipv4/parse-ipv4.js +169 -0
- package/dist/ipv4/parse-ipv4.js.map +1 -0
- package/dist/ipv4/print-ipv4.d.ts +50 -0
- package/dist/ipv4/print-ipv4.d.ts.map +1 -0
- package/dist/ipv4/print-ipv4.js +76 -0
- package/dist/ipv4/print-ipv4.js.map +1 -0
- package/dist/ipv6/classify-ipv6.d.ts +79 -0
- package/dist/ipv6/classify-ipv6.d.ts.map +1 -0
- package/dist/ipv6/classify-ipv6.js +148 -0
- package/dist/ipv6/classify-ipv6.js.map +1 -0
- package/dist/ipv6/constants.d.ts +5 -0
- package/dist/ipv6/constants.d.ts.map +1 -0
- package/dist/ipv6/constants.js +5 -0
- package/dist/ipv6/constants.js.map +1 -0
- package/dist/ipv6/ipv4-mapped.d.ts +43 -0
- package/dist/ipv6/ipv4-mapped.d.ts.map +1 -0
- package/dist/ipv6/ipv4-mapped.js +65 -0
- package/dist/ipv6/ipv4-mapped.js.map +1 -0
- package/dist/ipv6/ipv6-prefix.d.ts +51 -0
- package/dist/ipv6/ipv6-prefix.d.ts.map +1 -0
- package/dist/ipv6/ipv6-prefix.js +78 -0
- package/dist/ipv6/ipv6-prefix.js.map +1 -0
- package/dist/ipv6/parse-ipv6.d.ts +50 -0
- package/dist/ipv6/parse-ipv6.d.ts.map +1 -0
- package/dist/ipv6/parse-ipv6.js +148 -0
- package/dist/ipv6/parse-ipv6.js.map +1 -0
- package/dist/ipv6/print-ipv6.d.ts +37 -0
- package/dist/ipv6/print-ipv6.d.ts.map +1 -0
- package/dist/ipv6/print-ipv6.js +93 -0
- package/dist/ipv6/print-ipv6.js.map +1 -0
- package/dist/port/endpoint.d.ts +47 -0
- package/dist/port/endpoint.d.ts.map +1 -0
- package/dist/port/endpoint.js +104 -0
- package/dist/port/endpoint.js.map +1 -0
- package/dist/port/ephemeral-port.d.ts +17 -0
- package/dist/port/ephemeral-port.d.ts.map +1 -0
- package/dist/port/ephemeral-port.js +26 -0
- package/dist/port/ephemeral-port.js.map +1 -0
- package/dist/port/port.d.ts +69 -0
- package/dist/port/port.d.ts.map +1 -0
- package/dist/port/port.js +94 -0
- package/dist/port/port.js.map +1 -0
- package/dist/prefix/_numeric-range.d.ts +23 -0
- package/dist/prefix/_numeric-range.d.ts.map +1 -0
- package/dist/prefix/_numeric-range.js +57 -0
- package/dist/prefix/_numeric-range.js.map +1 -0
- package/dist/prefix/_prefix-length.d.ts +7 -0
- package/dist/prefix/_prefix-length.d.ts.map +1 -0
- package/dist/prefix/_prefix-length.js +13 -0
- package/dist/prefix/_prefix-length.js.map +1 -0
- package/dist/prefix/aggregate.d.ts +45 -0
- package/dist/prefix/aggregate.d.ts.map +1 -0
- package/dist/prefix/aggregate.js +97 -0
- package/dist/prefix/aggregate.js.map +1 -0
- package/dist/prefix/bit-address.d.ts +110 -0
- package/dist/prefix/bit-address.d.ts.map +1 -0
- package/dist/prefix/bit-address.js +165 -0
- package/dist/prefix/bit-address.js.map +1 -0
- package/dist/prefix/bit-prefix.d.ts +79 -0
- package/dist/prefix/bit-prefix.d.ts.map +1 -0
- package/dist/prefix/bit-prefix.js +135 -0
- package/dist/prefix/bit-prefix.js.map +1 -0
- package/dist/prefix/bit-range.d.ts +49 -0
- package/dist/prefix/bit-range.d.ts.map +1 -0
- package/dist/prefix/bit-range.js +100 -0
- package/dist/prefix/bit-range.js.map +1 -0
- package/dist/prefix/compare.d.ts +21 -0
- package/dist/prefix/compare.d.ts.map +1 -0
- package/dist/prefix/compare.js +33 -0
- package/dist/prefix/compare.js.map +1 -0
- package/dist/prefix/mask.d.ts +26 -0
- package/dist/prefix/mask.d.ts.map +1 -0
- package/dist/prefix/mask.js +48 -0
- package/dist/prefix/mask.js.map +1 -0
- package/dist/prefix/set-ops.d.ts +38 -0
- package/dist/prefix/set-ops.d.ts.map +1 -0
- package/dist/prefix/set-ops.js +71 -0
- package/dist/prefix/set-ops.js.map +1 -0
- package/package.json +66 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws unless `prefixLength` names a valid cut of a `bitWidth`-bit address.
|
|
3
|
+
*
|
|
4
|
+
* @throws RangeError When it is not an integer in `[0, bitWidth]`.
|
|
5
|
+
*/
|
|
6
|
+
export function assertPrefixLength(prefixLength, bitWidth) {
|
|
7
|
+
if (!Number.isInteger(prefixLength) ||
|
|
8
|
+
prefixLength < 0 ||
|
|
9
|
+
prefixLength > bitWidth) {
|
|
10
|
+
throw new RangeError(`prefix length must be an integer in [0, ${bitWidth}], got ${prefixLength}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=_prefix-length.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_prefix-length.js","sourceRoot":"","sources":["../../src/prefix/_prefix-length.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CACjC,YAAoB,EACpB,QAAgB;IAEhB,IACC,CAAC,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC;QAC/B,YAAY,GAAG,CAAC;QAChB,YAAY,GAAG,QAAQ,EACtB,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,2CAA2C,QAAQ,UAAU,YAAY,EAAE,CAC3E,CAAC;IACH,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type BitPrefix } from "./bit-prefix.js";
|
|
2
|
+
/**
|
|
3
|
+
* Summarises a collection into the shortest equivalent prefix list: overlaps
|
|
4
|
+
* absorbed, adjacent siblings merged, everything sorted.
|
|
5
|
+
*
|
|
6
|
+
* `10.0.0.0/25` and `10.0.0.128/25` come back as `10.0.0.0/24`. O(n log n) in
|
|
7
|
+
* the number of prefixes, plus at most `2 * bitWidth` prefixes per resulting
|
|
8
|
+
* run.
|
|
9
|
+
*
|
|
10
|
+
* @throws RangeError When the prefixes are not all the same width.
|
|
11
|
+
*/
|
|
12
|
+
export declare function aggregateBitPrefixes(prefixes: Iterable<BitPrefix>): BitPrefix[];
|
|
13
|
+
/**
|
|
14
|
+
* Divides a prefix into every subnet of `subnetPrefixLength`.
|
|
15
|
+
*
|
|
16
|
+
* Allocates `2 ** (subnetPrefixLength - prefix.prefixLength)` entries, so the
|
|
17
|
+
* cost is exponential in the difference and the caller chooses it.
|
|
18
|
+
*
|
|
19
|
+
* @throws RangeError When `subnetPrefixLength` is not between the prefix's own
|
|
20
|
+
* length and its bit width, or when the subnet count exceeds what an array
|
|
21
|
+
* can hold.
|
|
22
|
+
*/
|
|
23
|
+
export declare function splitBitPrefix(prefix: BitPrefix, subnetPrefixLength: number): BitPrefix[];
|
|
24
|
+
/**
|
|
25
|
+
* Divides a prefix into at least `subnetCount` equal subnets.
|
|
26
|
+
*
|
|
27
|
+
* Subnets only come in powers of two, so asking for 3 gives 4. The extra blocks
|
|
28
|
+
* are returned rather than dropped: a caller splitting for allocation needs to
|
|
29
|
+
* know they exist.
|
|
30
|
+
*
|
|
31
|
+
* @throws RangeError When `subnetCount` is not a positive integer, or when the
|
|
32
|
+
* prefix cannot be divided that far.
|
|
33
|
+
*/
|
|
34
|
+
export declare function splitBitPrefixInto(prefix: BitPrefix, subnetCount: number): BitPrefix[];
|
|
35
|
+
/**
|
|
36
|
+
* The smallest single prefix covering every input.
|
|
37
|
+
*
|
|
38
|
+
* Unlike {@link aggregateBitPrefixes} this may cover addresses none of the
|
|
39
|
+
* inputs did — that is what makes it a supernet rather than a summary. O(n) in
|
|
40
|
+
* the number of prefixes.
|
|
41
|
+
*
|
|
42
|
+
* @throws RangeError When the collection is empty or mixes widths.
|
|
43
|
+
*/
|
|
44
|
+
export declare function coveringBitPrefix(prefixes: Iterable<BitPrefix>): BitPrefix;
|
|
45
|
+
//# sourceMappingURL=aggregate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate.d.ts","sourceRoot":"","sources":["../../src/prefix/aggregate.ts"],"names":[],"mappings":"AAUA,OAAO,EACN,KAAK,SAAS,EAGd,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CACnC,QAAQ,EAAE,QAAQ,CAAC,SAAS,CAAC,GAC3B,SAAS,EAAE,CAUb;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,SAAS,EACjB,kBAAkB,EAAE,MAAM,GACxB,SAAS,EAAE,CAiCb;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,SAAS,EACjB,WAAW,EAAE,MAAM,GACjB,SAAS,EAAE,CAUb;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,CAAC,SAAS,CAAC,GAAG,SAAS,CAmB1E"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { commonBitWidth, fromNumericRanges, toNumericRanges, } from "./_numeric-range.js";
|
|
2
|
+
import { bitAddressCommonPrefixLength, bitAddressFromBigInt, bitAddressToBigInt, } from "./bit-address.js";
|
|
3
|
+
import { bitPrefixFirstAddress, normalizeBitPrefix, } from "./bit-prefix.js";
|
|
4
|
+
/**
|
|
5
|
+
* Summarises a collection into the shortest equivalent prefix list: overlaps
|
|
6
|
+
* absorbed, adjacent siblings merged, everything sorted.
|
|
7
|
+
*
|
|
8
|
+
* `10.0.0.0/25` and `10.0.0.128/25` come back as `10.0.0.0/24`. O(n log n) in
|
|
9
|
+
* the number of prefixes, plus at most `2 * bitWidth` prefixes per resulting
|
|
10
|
+
* run.
|
|
11
|
+
*
|
|
12
|
+
* @throws RangeError When the prefixes are not all the same width.
|
|
13
|
+
*/
|
|
14
|
+
export function aggregateBitPrefixes(prefixes) {
|
|
15
|
+
const list = Array.from(prefixes);
|
|
16
|
+
if (list.length === 0) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
const bitWidth = commonBitWidth(list);
|
|
20
|
+
return fromNumericRanges(toNumericRanges(list), bitWidth);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Divides a prefix into every subnet of `subnetPrefixLength`.
|
|
24
|
+
*
|
|
25
|
+
* Allocates `2 ** (subnetPrefixLength - prefix.prefixLength)` entries, so the
|
|
26
|
+
* cost is exponential in the difference and the caller chooses it.
|
|
27
|
+
*
|
|
28
|
+
* @throws RangeError When `subnetPrefixLength` is not between the prefix's own
|
|
29
|
+
* length and its bit width, or when the subnet count exceeds what an array
|
|
30
|
+
* can hold.
|
|
31
|
+
*/
|
|
32
|
+
export function splitBitPrefix(prefix, subnetPrefixLength) {
|
|
33
|
+
const { bitWidth } = prefix.address;
|
|
34
|
+
if (!Number.isInteger(subnetPrefixLength) ||
|
|
35
|
+
subnetPrefixLength < prefix.prefixLength ||
|
|
36
|
+
subnetPrefixLength > bitWidth) {
|
|
37
|
+
throw new RangeError(`subnet prefix length must be an integer in [${prefix.prefixLength}, ${bitWidth}], got ${subnetPrefixLength}`);
|
|
38
|
+
}
|
|
39
|
+
const count = 1n << BigInt(subnetPrefixLength - prefix.prefixLength);
|
|
40
|
+
if (count > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
41
|
+
throw new RangeError(`splitting /${prefix.prefixLength} into /${subnetPrefixLength} yields ${count} subnets`);
|
|
42
|
+
}
|
|
43
|
+
const step = 1n << BigInt(bitWidth - subnetPrefixLength);
|
|
44
|
+
const start = bitAddressToBigInt(bitPrefixFirstAddress(prefix));
|
|
45
|
+
const subnets = [];
|
|
46
|
+
for (let index = 0n; index < count; index++) {
|
|
47
|
+
subnets.push({
|
|
48
|
+
address: bitAddressFromBigInt(start + index * step, bitWidth),
|
|
49
|
+
prefixLength: subnetPrefixLength,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return subnets;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Divides a prefix into at least `subnetCount` equal subnets.
|
|
56
|
+
*
|
|
57
|
+
* Subnets only come in powers of two, so asking for 3 gives 4. The extra blocks
|
|
58
|
+
* are returned rather than dropped: a caller splitting for allocation needs to
|
|
59
|
+
* know they exist.
|
|
60
|
+
*
|
|
61
|
+
* @throws RangeError When `subnetCount` is not a positive integer, or when the
|
|
62
|
+
* prefix cannot be divided that far.
|
|
63
|
+
*/
|
|
64
|
+
export function splitBitPrefixInto(prefix, subnetCount) {
|
|
65
|
+
if (!Number.isInteger(subnetCount) || subnetCount < 1) {
|
|
66
|
+
throw new RangeError(`subnet count must be a positive integer, got ${subnetCount}`);
|
|
67
|
+
}
|
|
68
|
+
const extraBits = Math.ceil(Math.log2(subnetCount));
|
|
69
|
+
return splitBitPrefix(prefix, prefix.prefixLength + extraBits);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The smallest single prefix covering every input.
|
|
73
|
+
*
|
|
74
|
+
* Unlike {@link aggregateBitPrefixes} this may cover addresses none of the
|
|
75
|
+
* inputs did — that is what makes it a supernet rather than a summary. O(n) in
|
|
76
|
+
* the number of prefixes.
|
|
77
|
+
*
|
|
78
|
+
* @throws RangeError When the collection is empty or mixes widths.
|
|
79
|
+
*/
|
|
80
|
+
export function coveringBitPrefix(prefixes) {
|
|
81
|
+
const list = Array.from(prefixes);
|
|
82
|
+
const bitWidth = commonBitWidth(list);
|
|
83
|
+
const ranges = toNumericRanges(list);
|
|
84
|
+
// `toNumericRanges` sorts, so the extremes sit at the two ends.
|
|
85
|
+
const lowest = ranges[0];
|
|
86
|
+
const highest = ranges.at(-1);
|
|
87
|
+
if (lowest === undefined || highest === undefined) {
|
|
88
|
+
throw new RangeError("cannot cover an empty collection of prefixes");
|
|
89
|
+
}
|
|
90
|
+
const first = bitAddressFromBigInt(lowest, bitWidth);
|
|
91
|
+
const last = bitAddressFromBigInt(highest, bitWidth);
|
|
92
|
+
return normalizeBitPrefix({
|
|
93
|
+
address: first,
|
|
94
|
+
prefixLength: bitAddressCommonPrefixLength(first, last),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=aggregate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate.js","sourceRoot":"","sources":["../../src/prefix/aggregate.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,iBAAiB,EACjB,eAAe,GACf,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACN,4BAA4B,EAC5B,oBAAoB,EACpB,kBAAkB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAEN,qBAAqB,EACrB,kBAAkB,GAClB,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB,CACnC,QAA6B;IAE7B,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAElC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,EAAE,CAAC;IACX,CAAC;IAED,MAAM,QAAQ,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IAEtC,OAAO,iBAAiB,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,CAAC;AAC3D,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAC7B,MAAiB,EACjB,kBAA0B;IAE1B,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC;IAEpC,IACC,CAAC,MAAM,CAAC,SAAS,CAAC,kBAAkB,CAAC;QACrC,kBAAkB,GAAG,MAAM,CAAC,YAAY;QACxC,kBAAkB,GAAG,QAAQ,EAC5B,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,+CAA+C,MAAM,CAA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S,CAAC,CAAC;AAChE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAA6B;IAC9D,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAClC,MAAM,QAAQ,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IACrC,gEAAgE;IAChE,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IACzB,MAAM,OAAO,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAE9B,IAAI,MAAM,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CAAC,8CAA8C,CAAC,CAAC;IACtE,CAAC;IAED,MAAM,KAAK,GAAG,oBAAoB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACrD,MAAM,IAAI,GAAG,oBAAoB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAErD,OAAO,kBAAkB,CAAC;QACzB,OAAO,EAAE,KAAK;QACd,YAAY,EAAE,4BAA4B,CAAC,KAAK,EAAE,IAAI,CAAC;KACvD,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A fixed-width bit string — the one representation IPv4, IPv6 and
|
|
3
|
+
* EUI-48/EUI-64 all share.
|
|
4
|
+
*
|
|
5
|
+
* `bitWidth` is **carried, never derived**. Every constructor takes a width the
|
|
6
|
+
* value itself cannot supply: `0x7f000001n` is a 32-bit IPv4 address and also a
|
|
7
|
+
* 128-bit IPv6 address, and a width read back from a value's significant bits
|
|
8
|
+
* would call it 31.
|
|
9
|
+
*
|
|
10
|
+
* `value` is unsigned, because an address has no sign bit: `255.255.255.255` is
|
|
11
|
+
* `4294967295n`, never `-1n`.
|
|
12
|
+
*/
|
|
13
|
+
export type BitAddress = {
|
|
14
|
+
/** Unsigned, and no wider than `bitWidth` bits. */
|
|
15
|
+
readonly value: bigint;
|
|
16
|
+
/** A positive multiple of 8. */
|
|
17
|
+
readonly bitWidth: number;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Builds an address from octets, most significant first.
|
|
21
|
+
*
|
|
22
|
+
* One of the two wire-form boundaries, with {@link bitAddressToBytes}. Every
|
|
23
|
+
* operation in between works on the value, so a parser converts once on the way
|
|
24
|
+
* in rather than per octet.
|
|
25
|
+
*
|
|
26
|
+
* @param bytes The octets, most significant first.
|
|
27
|
+
* @param bitWidth The width in bits; must be `bytes.length * 8`.
|
|
28
|
+
* @throws RangeError When `bitWidth` is not a positive multiple of 8, or does
|
|
29
|
+
* not match `bytes.length`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function bitAddressFromBytes(bytes: Uint8Array, bitWidth: number): BitAddress;
|
|
32
|
+
/**
|
|
33
|
+
* The octets of the address, most significant first, zero-padded on the left to
|
|
34
|
+
* exactly `bitWidth / 8`.
|
|
35
|
+
*
|
|
36
|
+
* O(bitWidth / 8), and it allocates a fresh array each call — reach for
|
|
37
|
+
* {@link bitAddressBitAt} instead when only one bit is wanted.
|
|
38
|
+
*/
|
|
39
|
+
export declare function bitAddressToBytes(address: BitAddress): Uint8Array<ArrayBuffer>;
|
|
40
|
+
/**
|
|
41
|
+
* Throws unless both addresses are the same width.
|
|
42
|
+
*
|
|
43
|
+
* @throws RangeError When the widths differ.
|
|
44
|
+
*/
|
|
45
|
+
export declare function assertSameBitWidth(a: BitAddress, b: BitAddress): void;
|
|
46
|
+
/**
|
|
47
|
+
* Throws unless the address has exactly `bitWidth` bits.
|
|
48
|
+
*
|
|
49
|
+
* @throws RangeError When it does not.
|
|
50
|
+
*/
|
|
51
|
+
export declare function assertBitAddressWidth(address: BitAddress, bitWidth: number): void;
|
|
52
|
+
/**
|
|
53
|
+
* The unsigned big-endian value of the whole address. O(1).
|
|
54
|
+
*
|
|
55
|
+
* Unsigned, deliberately: `@ac-kit/core`'s `bigIntFromBytesBe` reads the same
|
|
56
|
+
* octets as two's complement, so it answers `-1n` for `255.255.255.255` where
|
|
57
|
+
* an address means 4294967295. An address is a bit string with no sign bit, and
|
|
58
|
+
* the two encodings are not interchangeable.
|
|
59
|
+
*/
|
|
60
|
+
export declare function bitAddressToBigInt(address: BitAddress): bigint;
|
|
61
|
+
/**
|
|
62
|
+
* The inverse of {@link bitAddressToBigInt}.
|
|
63
|
+
*
|
|
64
|
+
* @throws RangeError When `value` is negative or does not fit in `bitWidth`
|
|
65
|
+
* bits.
|
|
66
|
+
*/
|
|
67
|
+
export declare function bitAddressFromBigInt(value: bigint, bitWidth: number): BitAddress;
|
|
68
|
+
/**
|
|
69
|
+
* The bit at `bitIndex`, counted from the most significant bit.
|
|
70
|
+
*
|
|
71
|
+
* @throws RangeError When `bitIndex` is outside the address.
|
|
72
|
+
*/
|
|
73
|
+
export declare function bitAddressBitAt(address: BitAddress, bitIndex: number): 0 | 1;
|
|
74
|
+
/** Whether every bit is zero. */
|
|
75
|
+
export declare function bitAddressIsZero(address: BitAddress): boolean;
|
|
76
|
+
/** Whether every bit is one. */
|
|
77
|
+
export declare function bitAddressIsAllOnes(address: BitAddress): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Adds a signed offset. The address space does not wrap.
|
|
80
|
+
*
|
|
81
|
+
* @throws RangeError When the result leaves the address space.
|
|
82
|
+
*/
|
|
83
|
+
export declare function bitAddressAdd(address: BitAddress, delta: bigint): BitAddress;
|
|
84
|
+
/**
|
|
85
|
+
* Bitwise AND of two same-width addresses.
|
|
86
|
+
*
|
|
87
|
+
* @throws RangeError When the widths differ.
|
|
88
|
+
*/
|
|
89
|
+
export declare function bitAddressAnd(a: BitAddress, b: BitAddress): BitAddress;
|
|
90
|
+
/**
|
|
91
|
+
* Bitwise OR of two same-width addresses.
|
|
92
|
+
*
|
|
93
|
+
* @throws RangeError When the widths differ.
|
|
94
|
+
*/
|
|
95
|
+
export declare function bitAddressOr(a: BitAddress, b: BitAddress): BitAddress;
|
|
96
|
+
/**
|
|
97
|
+
* Bitwise XOR of two same-width addresses.
|
|
98
|
+
*
|
|
99
|
+
* @throws RangeError When the widths differ.
|
|
100
|
+
*/
|
|
101
|
+
export declare function bitAddressXor(a: BitAddress, b: BitAddress): BitAddress;
|
|
102
|
+
/** Bitwise complement. */
|
|
103
|
+
export declare function bitAddressNot(address: BitAddress): BitAddress;
|
|
104
|
+
/**
|
|
105
|
+
* How many leading bits `a` and `b` share. O(1).
|
|
106
|
+
*
|
|
107
|
+
* @throws RangeError When the widths differ.
|
|
108
|
+
*/
|
|
109
|
+
export declare function bitAddressCommonPrefixLength(a: BitAddress, b: BitAddress): number;
|
|
110
|
+
//# sourceMappingURL=bit-address.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bit-address.d.ts","sourceRoot":"","sources":["../../src/prefix/bit-address.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,UAAU,GAAG;IACxB,mDAAmD;IACnD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,gCAAgC;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B,CAAC;AAcF;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,MAAM,GACd,UAAU,CAeZ;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAChC,OAAO,EAAE,UAAU,GACjB,UAAU,CAAC,WAAW,CAAC,CAOzB;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,IAAI,CAMrE;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,MAAM,GACd,IAAI,CAMN;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,GACd,UAAU,CAQZ;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,CAAC,GAAG,CAAC,CAc5E;AAED,iCAAiC;AACjC,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAE7D;AAED,gCAAgC;AAChC,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAEhE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,UAAU,CAE5E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CAItE;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CAIrE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CAItE;AAED,0BAA0B;AAC1B,wBAAgB,aAAa,CAAC,OAAO,EAAE,UAAU,GAAG,UAAU,CAK7D;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAC3C,CAAC,EAAE,UAAU,EACb,CAAC,EAAE,UAAU,GACX,MAAM,CAMR"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { bigIntBitLength, bigIntFromBytesBe, bigIntToBytesBe, bytesForBits, } from "@ac-kit/core";
|
|
2
|
+
import { fixedSIntBigToUnsigned, fixedUIntBigIsInRange, fixedUIntBigMax, fixedUIntBigNot, fixedUIntBigToSigned, } from "@ac-kit/math-numbers";
|
|
3
|
+
const BITS_PER_BYTE = 8;
|
|
4
|
+
function assertBitWidth(bitWidth) {
|
|
5
|
+
if (!Number.isInteger(bitWidth) ||
|
|
6
|
+
bitWidth <= 0 ||
|
|
7
|
+
bitWidth % BITS_PER_BYTE !== 0) {
|
|
8
|
+
throw new RangeError(`bit width must be a positive multiple of 8, got ${bitWidth}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Builds an address from octets, most significant first.
|
|
13
|
+
*
|
|
14
|
+
* One of the two wire-form boundaries, with {@link bitAddressToBytes}. Every
|
|
15
|
+
* operation in between works on the value, so a parser converts once on the way
|
|
16
|
+
* in rather than per octet.
|
|
17
|
+
*
|
|
18
|
+
* @param bytes The octets, most significant first.
|
|
19
|
+
* @param bitWidth The width in bits; must be `bytes.length * 8`.
|
|
20
|
+
* @throws RangeError When `bitWidth` is not a positive multiple of 8, or does
|
|
21
|
+
* not match `bytes.length`.
|
|
22
|
+
*/
|
|
23
|
+
export function bitAddressFromBytes(bytes, bitWidth) {
|
|
24
|
+
assertBitWidth(bitWidth);
|
|
25
|
+
if (bytes.length !== bytesForBits(bitWidth)) {
|
|
26
|
+
throw new RangeError(`${bitWidth} bits need ${bytesForBits(bitWidth)} octets, got ${bytes.length}`);
|
|
27
|
+
}
|
|
28
|
+
// `bigIntFromBytesBe` reads the octets as two's complement; reinterpreting
|
|
29
|
+
// the same bit pattern unsigned is what an address means by them.
|
|
30
|
+
return {
|
|
31
|
+
value: fixedSIntBigToUnsigned(bigIntFromBytesBe(bytes), bitWidth),
|
|
32
|
+
bitWidth,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The octets of the address, most significant first, zero-padded on the left to
|
|
37
|
+
* exactly `bitWidth / 8`.
|
|
38
|
+
*
|
|
39
|
+
* O(bitWidth / 8), and it allocates a fresh array each call — reach for
|
|
40
|
+
* {@link bitAddressBitAt} instead when only one bit is wanted.
|
|
41
|
+
*/
|
|
42
|
+
export function bitAddressToBytes(address) {
|
|
43
|
+
// The signed reinterpretation is what fits the pattern in exactly
|
|
44
|
+
// `bitWidth / 8` octets; the unsigned value would need a leading zero.
|
|
45
|
+
return bigIntToBytesBe(fixedUIntBigToSigned(address.value, address.bitWidth), bytesForBits(address.bitWidth));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Throws unless both addresses are the same width.
|
|
49
|
+
*
|
|
50
|
+
* @throws RangeError When the widths differ.
|
|
51
|
+
*/
|
|
52
|
+
export function assertSameBitWidth(a, b) {
|
|
53
|
+
if (a.bitWidth !== b.bitWidth) {
|
|
54
|
+
throw new RangeError(`address widths differ: ${a.bitWidth} and ${b.bitWidth}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Throws unless the address has exactly `bitWidth` bits.
|
|
59
|
+
*
|
|
60
|
+
* @throws RangeError When it does not.
|
|
61
|
+
*/
|
|
62
|
+
export function assertBitAddressWidth(address, bitWidth) {
|
|
63
|
+
if (address.bitWidth !== bitWidth) {
|
|
64
|
+
throw new RangeError(`expected a ${bitWidth}-bit address, got ${address.bitWidth} bits`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The unsigned big-endian value of the whole address. O(1).
|
|
69
|
+
*
|
|
70
|
+
* Unsigned, deliberately: `@ac-kit/core`'s `bigIntFromBytesBe` reads the same
|
|
71
|
+
* octets as two's complement, so it answers `-1n` for `255.255.255.255` where
|
|
72
|
+
* an address means 4294967295. An address is a bit string with no sign bit, and
|
|
73
|
+
* the two encodings are not interchangeable.
|
|
74
|
+
*/
|
|
75
|
+
export function bitAddressToBigInt(address) {
|
|
76
|
+
return address.value;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The inverse of {@link bitAddressToBigInt}.
|
|
80
|
+
*
|
|
81
|
+
* @throws RangeError When `value` is negative or does not fit in `bitWidth`
|
|
82
|
+
* bits.
|
|
83
|
+
*/
|
|
84
|
+
export function bitAddressFromBigInt(value, bitWidth) {
|
|
85
|
+
assertBitWidth(bitWidth);
|
|
86
|
+
if (!fixedUIntBigIsInRange(value, bitWidth)) {
|
|
87
|
+
throw new RangeError(`${value} does not fit in ${bitWidth} bits`);
|
|
88
|
+
}
|
|
89
|
+
return { value, bitWidth };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The bit at `bitIndex`, counted from the most significant bit.
|
|
93
|
+
*
|
|
94
|
+
* @throws RangeError When `bitIndex` is outside the address.
|
|
95
|
+
*/
|
|
96
|
+
export function bitAddressBitAt(address, bitIndex) {
|
|
97
|
+
if (!Number.isInteger(bitIndex) ||
|
|
98
|
+
bitIndex < 0 ||
|
|
99
|
+
bitIndex >= address.bitWidth) {
|
|
100
|
+
throw new RangeError(`bit ${bitIndex} is outside a ${address.bitWidth}-bit address`);
|
|
101
|
+
}
|
|
102
|
+
return Number((address.value >> BigInt(address.bitWidth - 1 - bitIndex)) & 1n);
|
|
103
|
+
}
|
|
104
|
+
/** Whether every bit is zero. */
|
|
105
|
+
export function bitAddressIsZero(address) {
|
|
106
|
+
return address.value === 0n;
|
|
107
|
+
}
|
|
108
|
+
/** Whether every bit is one. */
|
|
109
|
+
export function bitAddressIsAllOnes(address) {
|
|
110
|
+
return address.value === fixedUIntBigMax(address.bitWidth);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Adds a signed offset. The address space does not wrap.
|
|
114
|
+
*
|
|
115
|
+
* @throws RangeError When the result leaves the address space.
|
|
116
|
+
*/
|
|
117
|
+
export function bitAddressAdd(address, delta) {
|
|
118
|
+
return bitAddressFromBigInt(address.value + delta, address.bitWidth);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Bitwise AND of two same-width addresses.
|
|
122
|
+
*
|
|
123
|
+
* @throws RangeError When the widths differ.
|
|
124
|
+
*/
|
|
125
|
+
export function bitAddressAnd(a, b) {
|
|
126
|
+
assertSameBitWidth(a, b);
|
|
127
|
+
return { value: a.value & b.value, bitWidth: a.bitWidth };
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Bitwise OR of two same-width addresses.
|
|
131
|
+
*
|
|
132
|
+
* @throws RangeError When the widths differ.
|
|
133
|
+
*/
|
|
134
|
+
export function bitAddressOr(a, b) {
|
|
135
|
+
assertSameBitWidth(a, b);
|
|
136
|
+
return { value: a.value | b.value, bitWidth: a.bitWidth };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Bitwise XOR of two same-width addresses.
|
|
140
|
+
*
|
|
141
|
+
* @throws RangeError When the widths differ.
|
|
142
|
+
*/
|
|
143
|
+
export function bitAddressXor(a, b) {
|
|
144
|
+
assertSameBitWidth(a, b);
|
|
145
|
+
return { value: a.value ^ b.value, bitWidth: a.bitWidth };
|
|
146
|
+
}
|
|
147
|
+
/** Bitwise complement. */
|
|
148
|
+
export function bitAddressNot(address) {
|
|
149
|
+
return {
|
|
150
|
+
value: fixedUIntBigNot(address.value, address.bitWidth),
|
|
151
|
+
bitWidth: address.bitWidth,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* How many leading bits `a` and `b` share. O(1).
|
|
156
|
+
*
|
|
157
|
+
* @throws RangeError When the widths differ.
|
|
158
|
+
*/
|
|
159
|
+
export function bitAddressCommonPrefixLength(a, b) {
|
|
160
|
+
assertSameBitWidth(a, b);
|
|
161
|
+
// The highest set bit of the difference is the first position they disagree
|
|
162
|
+
// on, so everything above it is shared.
|
|
163
|
+
return a.bitWidth - bigIntBitLength(a.value ^ b.value);
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=bit-address.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bit-address.js","sourceRoot":"","sources":["../../src/prefix/bit-address.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,YAAY,GACZ,MA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,UAAU,aAAa,CAAC,OAAmB;IAChD,OAAO;QACN,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC;QACvD,QAAQ,EAAE,OAAO,CAAC,QAAQ;KAC1B,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,4BAA4B,CAC3C,CAAa,EACb,CAAa;IAEb,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAEzB,4EAA4E;IAC5E,wCAAwC;IACxC,OAAO,CAAC,CAAC,QAAQ,GAAG,eAAe,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ComparatorResult } from "@ac-kit/core";
|
|
2
|
+
import { type BitAddress } from "./bit-address.js";
|
|
3
|
+
/**
|
|
4
|
+
* An address together with how many of its leading bits are significant.
|
|
5
|
+
*
|
|
6
|
+
* The address is kept exactly as given: `192.168.1.10/24` stays that, so a
|
|
7
|
+
* caller can still read the host part back. Use {@link normalizeBitPrefix} for
|
|
8
|
+
* the network form, which is what every comparison here works on.
|
|
9
|
+
*/
|
|
10
|
+
export type BitPrefix = {
|
|
11
|
+
readonly address: BitAddress;
|
|
12
|
+
readonly prefixLength: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Pairs an address with a prefix length, keeping any host bits it carries.
|
|
16
|
+
*
|
|
17
|
+
* @throws RangeError When `prefixLength` is outside `[0, address.bitWidth]`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createBitPrefix(address: BitAddress, prefixLength: number): BitPrefix;
|
|
20
|
+
/** The netmask matching the prefix length. */
|
|
21
|
+
export declare function bitPrefixNetmask(prefix: BitPrefix): BitAddress;
|
|
22
|
+
/** The wildcard mask matching the prefix length. */
|
|
23
|
+
export declare function bitPrefixWildcardMask(prefix: BitPrefix): BitAddress;
|
|
24
|
+
/** The lowest address covered — the network address, host bits cleared. */
|
|
25
|
+
export declare function bitPrefixFirstAddress(prefix: BitPrefix): BitAddress;
|
|
26
|
+
/** The highest address covered — host bits set. */
|
|
27
|
+
export declare function bitPrefixLastAddress(prefix: BitPrefix): BitAddress;
|
|
28
|
+
/** The same prefix with its host bits cleared. */
|
|
29
|
+
export declare function normalizeBitPrefix(prefix: BitPrefix): BitPrefix;
|
|
30
|
+
/** Whether the prefix's address already has every host bit clear. */
|
|
31
|
+
export declare function isNormalizedBitPrefix(prefix: BitPrefix): boolean;
|
|
32
|
+
/** How many addresses the prefix covers: `2 ** (bitWidth - prefixLength)`. */
|
|
33
|
+
export declare function bitPrefixAddressCount(prefix: BitPrefix): bigint;
|
|
34
|
+
/**
|
|
35
|
+
* Whether `address` falls inside `prefix`.
|
|
36
|
+
*
|
|
37
|
+
* @throws RangeError When the widths differ.
|
|
38
|
+
*/
|
|
39
|
+
export declare function bitPrefixContains(prefix: BitPrefix, address: BitAddress): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Whether every address of `inner` falls inside `outer`.
|
|
42
|
+
*
|
|
43
|
+
* @throws RangeError When the widths differ.
|
|
44
|
+
*/
|
|
45
|
+
export declare function bitPrefixContainsPrefix(outer: BitPrefix, inner: BitPrefix): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Whether `subject` is contained in `container`. The inverse reading of
|
|
48
|
+
* {@link bitPrefixContainsPrefix}, kept because routing code says it this way.
|
|
49
|
+
*/
|
|
50
|
+
export declare function bitPrefixIsSubnetOf(subject: BitPrefix, container: BitPrefix): boolean;
|
|
51
|
+
/** Whether `subject` contains `contained`. */
|
|
52
|
+
export declare function bitPrefixIsSupernetOf(subject: BitPrefix, contained: BitPrefix): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Whether the two prefixes share any address. Two prefixes of the same family
|
|
55
|
+
* either nest or are disjoint, so this is "one contains the other".
|
|
56
|
+
*
|
|
57
|
+
* @throws RangeError When the widths differ.
|
|
58
|
+
*/
|
|
59
|
+
export declare function bitPrefixOverlaps(a: BitPrefix, b: BitPrefix): boolean;
|
|
60
|
+
/** Whether both cover exactly the same addresses. Host bits are ignored. */
|
|
61
|
+
export declare function bitPrefixEquals(a: BitPrefix, b: BitPrefix): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Orders by network address, then by prefix length — so a supernet sorts before
|
|
64
|
+
* every subnet it contains. Exact, and agrees with {@link bitPrefixEquals}.
|
|
65
|
+
*/
|
|
66
|
+
export declare function compareBitPrefixes(a: BitPrefix, b: BitPrefix): ComparatorResult;
|
|
67
|
+
/**
|
|
68
|
+
* The prefix one bit shorter — the block this one was halved out of.
|
|
69
|
+
*
|
|
70
|
+
* @returns `null` for a `/0`, which has no supernet.
|
|
71
|
+
*/
|
|
72
|
+
export declare function bitPrefixSupernet(prefix: BitPrefix): BitPrefix | null;
|
|
73
|
+
/**
|
|
74
|
+
* The other half of this prefix's supernet — the block it would merge with.
|
|
75
|
+
*
|
|
76
|
+
* @returns `null` for a `/0`, which has no sibling.
|
|
77
|
+
*/
|
|
78
|
+
export declare function bitPrefixSibling(prefix: BitPrefix): BitPrefix | null;
|
|
79
|
+
//# sourceMappingURL=bit-prefix.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bit-prefix.d.ts","sourceRoot":"","sources":["../../src/prefix/bit-prefix.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGrD,OAAO,EACN,KAAK,UAAU,EAOf,MAAM,kBAAkB,CAAC;AAI1B;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GAAG;IACvB,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,UAAU,EACnB,YAAY,EAAE,MAAM,GAClB,SAAS,CAIX;AAED,8CA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