cidr-tools 11.0.11 → 11.0.12
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/dist/index.js +50 -11
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -5,9 +5,29 @@ const bits = {
|
|
|
5
5
|
4: 32,
|
|
6
6
|
6: 128
|
|
7
7
|
};
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
function parseIPv4Fast(s) {
|
|
9
|
+
let num = 0;
|
|
10
|
+
let octet = 0;
|
|
11
|
+
let dots = 0;
|
|
12
|
+
let digits = 0;
|
|
13
|
+
for (let i = 0; i < s.length; i++) {
|
|
14
|
+
const c = s.charCodeAt(i);
|
|
15
|
+
if (c === 46) {
|
|
16
|
+
if (digits === 0 || octet > 255) return -1;
|
|
17
|
+
num = (num << 8 | octet) >>> 0;
|
|
18
|
+
octet = 0;
|
|
19
|
+
dots++;
|
|
20
|
+
digits = 0;
|
|
21
|
+
} else if (c >= 48 && c <= 57) {
|
|
22
|
+
octet = octet * 10 + (c - 48);
|
|
23
|
+
digits++;
|
|
24
|
+
} else return -1;
|
|
25
|
+
}
|
|
26
|
+
if (dots !== 3 || digits === 0 || octet > 255) return -1;
|
|
27
|
+
return (num << 8 | octet) >>> 0;
|
|
28
|
+
}
|
|
29
|
+
function formatIPv4Fast(n) {
|
|
30
|
+
return `${n >>> 24 & 255}.${n >>> 16 & 255}.${n >>> 8 & 255}.${n & 255}`;
|
|
11
31
|
}
|
|
12
32
|
function doNormalize(cidr, { compress = true, hexify = false } = {}) {
|
|
13
33
|
const { start, end, prefix, version, prefixPresent } = parseCidr(cidr);
|
|
@@ -75,6 +95,20 @@ function parseCidrLean(str) {
|
|
|
75
95
|
ipPart = str;
|
|
76
96
|
prefixNum = -1;
|
|
77
97
|
}
|
|
98
|
+
if (!ipPart.includes(":")) {
|
|
99
|
+
const v4num = parseIPv4Fast(ipPart);
|
|
100
|
+
if (v4num !== -1) {
|
|
101
|
+
if (prefixNum === -1) prefixNum = 32;
|
|
102
|
+
const bigNum = BigInt(v4num);
|
|
103
|
+
const hostBits = 32 - prefixNum;
|
|
104
|
+
const mask = hostBits > 0 ? (1n << BigInt(hostBits)) - 1n : 0n;
|
|
105
|
+
return {
|
|
106
|
+
start: bigNum & ~mask,
|
|
107
|
+
end: bigNum | mask,
|
|
108
|
+
version: 4
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
78
112
|
const { number, version } = parseIp(ipPart);
|
|
79
113
|
if (!version) throw new Error(`Network is not a CIDR or IP: "${str}"`);
|
|
80
114
|
if (prefixNum === -1) prefixNum = bits[version];
|
|
@@ -153,6 +187,11 @@ function diff(a, b) {
|
|
|
153
187
|
return a + 1n - b;
|
|
154
188
|
}
|
|
155
189
|
function formatPart(part, version) {
|
|
190
|
+
if (version === 4) {
|
|
191
|
+
const ip = formatIPv4Fast(Number(part.start));
|
|
192
|
+
const sizeNum = Number(part.end - part.start) + 1;
|
|
193
|
+
return `${ip}/${32 - (sizeNum <= 1 ? 0 : sizeNum >= 4294967296 ? 32 : 31 - Math.clz32(sizeNum))}`;
|
|
194
|
+
}
|
|
156
195
|
const ip = stringifyIp({
|
|
157
196
|
number: part.start,
|
|
158
197
|
version
|
|
@@ -218,7 +257,7 @@ function subtractSorted(bases, excls) {
|
|
|
218
257
|
}
|
|
219
258
|
/** Returns an array of merged networks */
|
|
220
259
|
function mergeCidr(nets) {
|
|
221
|
-
const arr =
|
|
260
|
+
const arr = (Array.isArray(nets) ? nets : [nets]).map(parseCidrLean);
|
|
222
261
|
const byVersion = {
|
|
223
262
|
4: [],
|
|
224
263
|
6: []
|
|
@@ -230,8 +269,8 @@ function mergeCidr(nets) {
|
|
|
230
269
|
}
|
|
231
270
|
/** Returns an array of merged remaining networks of the subtraction of `excludeNetworks` from `baseNetworks`. */
|
|
232
271
|
function excludeCidr(base, excl) {
|
|
233
|
-
const baseArr =
|
|
234
|
-
const exclArr =
|
|
272
|
+
const baseArr = (Array.isArray(base) ? base : [base]).map(parseCidrLean);
|
|
273
|
+
const exclArr = (Array.isArray(excl) ? excl : [excl]).map(parseCidrLean);
|
|
235
274
|
const baseByVersion = {
|
|
236
275
|
4: [],
|
|
237
276
|
6: []
|
|
@@ -253,7 +292,7 @@ function excludeCidr(base, excl) {
|
|
|
253
292
|
return result;
|
|
254
293
|
}
|
|
255
294
|
function* expandCidr(nets) {
|
|
256
|
-
const arr =
|
|
295
|
+
const arr = Array.isArray(nets) ? nets : [nets];
|
|
257
296
|
for (const net of mergeCidr(arr)) {
|
|
258
297
|
const { start, end, version } = parseCidrLean(net);
|
|
259
298
|
for (let number = start; number <= end; number++) yield normalizeIp(stringifyIp({
|
|
@@ -264,8 +303,8 @@ function* expandCidr(nets) {
|
|
|
264
303
|
}
|
|
265
304
|
/** Returns a boolean that indicates if `networksA` overlap (intersect) with `networksB`. */
|
|
266
305
|
function overlapCidr(a, b) {
|
|
267
|
-
const aNets =
|
|
268
|
-
const bNets =
|
|
306
|
+
const aNets = (Array.isArray(a) ? a : [a]).map(parseCidrLean);
|
|
307
|
+
const bNets = (Array.isArray(b) ? b : [b]).map(parseCidrLean);
|
|
269
308
|
const aByVersion = {
|
|
270
309
|
4: [],
|
|
271
310
|
6: []
|
|
@@ -292,8 +331,8 @@ function overlapCidr(a, b) {
|
|
|
292
331
|
}
|
|
293
332
|
/** Returns a boolean that indicates whether `networksA` fully contain all `networksB`. */
|
|
294
333
|
function containsCidr(a, b) {
|
|
295
|
-
const aNets =
|
|
296
|
-
const bNets =
|
|
334
|
+
const aNets = (Array.isArray(a) ? a : [a]).map(parseCidrLean);
|
|
335
|
+
const bNets = (Array.isArray(b) ? b : [b]).map(parseCidrLean);
|
|
297
336
|
const aByVersion = {
|
|
298
337
|
4: [],
|
|
299
338
|
6: []
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cidr-tools",
|
|
3
|
-
"version": "11.0.
|
|
3
|
+
"version": "11.0.12",
|
|
4
4
|
"author": "silverwind <me@silverwind.io>",
|
|
5
5
|
"description": "Tools to work with IPv4 and IPv6 CIDR",
|
|
6
6
|
"repository": "silverwind/cidr-tools",
|
|
@@ -17,21 +17,21 @@
|
|
|
17
17
|
"node": ">=18"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"ip-bigint": "^8.2.
|
|
20
|
+
"ip-bigint": "^8.2.8"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@types/node": "25.3.
|
|
24
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
23
|
+
"@types/node": "25.3.3",
|
|
24
|
+
"@typescript/native-preview": "7.0.0-dev.20260305.1",
|
|
25
25
|
"eslint": "9.39.3",
|
|
26
|
-
"eslint-config-silverwind": "121.
|
|
26
|
+
"eslint-config-silverwind": "121.2.0",
|
|
27
27
|
"jest-extended": "7.0.0",
|
|
28
|
-
"tsdown": "0.
|
|
29
|
-
"tsdown-config-silverwind": "
|
|
28
|
+
"tsdown": "0.21.0",
|
|
29
|
+
"tsdown-config-silverwind": "2.0.0",
|
|
30
30
|
"typescript": "5.9.3",
|
|
31
31
|
"typescript-config-silverwind": "15.0.0",
|
|
32
|
-
"updates": "17.
|
|
32
|
+
"updates": "17.8.1",
|
|
33
33
|
"updates-config-silverwind": "1.0.3",
|
|
34
|
-
"versions": "14.2.
|
|
34
|
+
"versions": "14.2.1",
|
|
35
35
|
"vitest": "4.0.18",
|
|
36
36
|
"vitest-config-silverwind": "10.6.3"
|
|
37
37
|
}
|