pmcf 2.16.0 → 2.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ip.mjs +39 -5
- package/src/subnet.mjs +9 -5
- package/types/ip.d.mts +1 -0
package/package.json
CHANGED
package/src/ip.mjs
CHANGED
|
@@ -21,8 +21,7 @@ const ipv6 = {
|
|
|
21
21
|
if (i >= 0) {
|
|
22
22
|
parts.splice(i, 1, ..."0".repeat(9 - parts.length));
|
|
23
23
|
}
|
|
24
|
-
return parts
|
|
25
|
-
.join(":");
|
|
24
|
+
return parts.join(":");
|
|
26
25
|
},
|
|
27
26
|
separator: ":",
|
|
28
27
|
compressor: "::",
|
|
@@ -51,7 +50,8 @@ function _create(definition, ...args) {
|
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
export function encodeIP(address) {
|
|
54
|
-
|
|
53
|
+
const d = _definition(address);
|
|
54
|
+
return d && _encode(d, address);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export function encodeIPv6(address) {
|
|
@@ -76,6 +76,9 @@ export function _encode(definition, address) {
|
|
|
76
76
|
|
|
77
77
|
return res;
|
|
78
78
|
|
|
79
|
+
case "bigint":
|
|
80
|
+
return _encodeBigInt(definition, address);
|
|
81
|
+
|
|
79
82
|
case "object":
|
|
80
83
|
if (
|
|
81
84
|
address instanceof definition.factory &&
|
|
@@ -156,6 +159,14 @@ export function isIPv6(address) {
|
|
|
156
159
|
return _is(ipv6, address);
|
|
157
160
|
}
|
|
158
161
|
|
|
162
|
+
function _definition(address) {
|
|
163
|
+
for (const defintion of [ipv4, ipv6]) {
|
|
164
|
+
if (_is(defintion, address)) {
|
|
165
|
+
return defintion;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
159
170
|
export function _is(definition, address) {
|
|
160
171
|
switch (typeof address) {
|
|
161
172
|
case "string":
|
|
@@ -215,6 +226,24 @@ export function _prefix(definition, address, length) {
|
|
|
215
226
|
);
|
|
216
227
|
}
|
|
217
228
|
|
|
229
|
+
export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
|
|
230
|
+
const definition = isIPv4(address) ? ipv4 : ipv6;
|
|
231
|
+
|
|
232
|
+
const from = _prefix(definition, address, prefix);
|
|
233
|
+
const to = _encode(definition, from); // /*from ||*/ definition.mask >> BigInt(prefix);
|
|
234
|
+
|
|
235
|
+
for (
|
|
236
|
+
let i = prefix / definition.segmentLength;
|
|
237
|
+
i < definition.segments;
|
|
238
|
+
i++
|
|
239
|
+
) {
|
|
240
|
+
to[i] = 0xffff;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
//console.log(from, to);
|
|
244
|
+
return [_encode(definition, from + BigInt(lowerAdd)), to];
|
|
245
|
+
}
|
|
246
|
+
|
|
218
247
|
export function normalizeCIDR(address) {
|
|
219
248
|
let [prefix, prefixLength] = address.split(/\//);
|
|
220
249
|
let longPrefix;
|
|
@@ -244,7 +273,12 @@ export function normalizeCIDR(address) {
|
|
|
244
273
|
longPrefix = _decode(definition, n);
|
|
245
274
|
}
|
|
246
275
|
|
|
247
|
-
return {
|
|
276
|
+
return {
|
|
277
|
+
longPrefix,
|
|
278
|
+
prefix,
|
|
279
|
+
prefixLength,
|
|
280
|
+
cidr: `${prefix}/${prefixLength}`
|
|
281
|
+
};
|
|
248
282
|
}
|
|
249
283
|
|
|
250
284
|
export function formatCIDR(address, subnet) {
|
|
@@ -285,7 +319,7 @@ export function isLocalhost(address) {
|
|
|
285
319
|
|
|
286
320
|
export function isLinkLocal(address) {
|
|
287
321
|
const eaddr = encodeIP(address);
|
|
288
|
-
return eaddr[0] === 0xfe80;
|
|
322
|
+
return eaddr?.[0] === 0xfe80;
|
|
289
323
|
}
|
|
290
324
|
|
|
291
325
|
export function hasWellKnownSubnet(address) {
|
package/src/subnet.mjs
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
normalizeCIDR,
|
|
3
|
+
isLinkLocal,
|
|
4
|
+
isIPv4,
|
|
5
|
+
isIPv6,
|
|
6
|
+
rangeIP,
|
|
7
|
+
decodeIP
|
|
8
|
+
} from "./ip.mjs";
|
|
2
9
|
import { Base } from "./base.mjs";
|
|
3
10
|
import { addType } from "./types.mjs";
|
|
4
11
|
|
|
@@ -61,10 +68,7 @@ export class Subnet extends Base {
|
|
|
61
68
|
}
|
|
62
69
|
|
|
63
70
|
get addressRange() {
|
|
64
|
-
return
|
|
65
|
-
prefixIP(this.prefix, this.prefixLength),
|
|
66
|
-
this.prefix + ".255".repeat((32 - this.prefixLength) / 8)
|
|
67
|
-
];
|
|
71
|
+
return rangeIP(this.prefix, this.prefixLength, 0, 0).map(a => decodeIP(a));
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
get address() {
|
package/types/ip.d.mts
CHANGED
|
@@ -13,6 +13,7 @@ export function _is(definition: any, address: any): boolean;
|
|
|
13
13
|
export function asBigInt(address: any): bigint;
|
|
14
14
|
export function prefixIP(address: any, length: any): string;
|
|
15
15
|
export function _prefix(definition: any, address: any, length: any): bigint;
|
|
16
|
+
export function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
|
|
16
17
|
export function normalizeCIDR(address: any): {
|
|
17
18
|
longPrefix?: undefined;
|
|
18
19
|
prefix?: undefined;
|