cidr-tools 11.0.3 → 11.0.5
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/README.md +1 -1
- package/dist/index.d.ts +4 -5
- package/dist/index.js +30 -13
- package/package.json +13 -13
- package/dist/index.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# cidr-tools
|
|
2
|
-
[](https://www.npmjs.org/package/cidr-tools) [](https://www.npmjs.org/package/cidr-tools) [](https://bundlephobia.com/package/cidr-tools) [](https://packagephobia.com/result?p=cidr-tools)
|
|
2
|
+
[](https://www.npmjs.org/package/cidr-tools) [](https://www.npmjs.org/package/cidr-tools) [](https://bundlephobia.com/package/cidr-tools) [](https://packagephobia.com/result?p=cidr-tools) [](https://depx.co/pkg/cidr-tools)
|
|
3
3
|
> Tools to work with IPv4 and IPv6 CIDR
|
|
4
4
|
|
|
5
5
|
## Usage
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type IPv6Address = string;
|
|
|
3
3
|
type IPv4CIDR = string;
|
|
4
4
|
type IPv6CIDR = string;
|
|
5
5
|
type Network = IPv4Address | IPv4CIDR | IPv6Address | IPv6CIDR;
|
|
6
|
-
type Networks = Network | Network
|
|
6
|
+
type Networks = Network | Array<Network>;
|
|
7
7
|
type ValidIpVersion = 4 | 6;
|
|
8
8
|
type ParsedCidr = {
|
|
9
9
|
cidr: string;
|
|
@@ -19,13 +19,13 @@ type NormalizeOpts = {
|
|
|
19
19
|
hexify?: boolean;
|
|
20
20
|
};
|
|
21
21
|
/** Returns a string or array (depending on input) with a normalized representation. Will not include a prefix on single IPs. Will set network address to the start of the network. */
|
|
22
|
-
export declare function normalizeCidr<T extends Network | Network
|
|
22
|
+
export declare function normalizeCidr<T extends Network | Array<Network>>(cidr: T, opts?: NormalizeOpts): T;
|
|
23
23
|
/** Returns a `parsed` Object which is used internally by this module. It can be used to test whether the passed network is IPv4 or IPv6 or to work with the BigInts directly. */
|
|
24
24
|
export declare function parseCidr(str: Network): ParsedCidr;
|
|
25
25
|
/** Returns an array of merged networks */
|
|
26
|
-
export declare function mergeCidr(nets: Networks): Network
|
|
26
|
+
export declare function mergeCidr(nets: Networks): Array<Network>;
|
|
27
27
|
/** Returns an array of merged remaining networks of the subtraction of `excludeNetworks` from `baseNetworks`. */
|
|
28
|
-
export declare function excludeCidr(base: Networks, excl: Networks): Network
|
|
28
|
+
export declare function excludeCidr(base: Networks, excl: Networks): Array<Network>;
|
|
29
29
|
export declare function expandCidr(nets: Networks): Generator<Network>;
|
|
30
30
|
/** Returns a boolean that indicates if `networksA` overlap (intersect) with `networksB`. */
|
|
31
31
|
export declare function overlapCidr(a: Networks, b: Networks): boolean;
|
|
@@ -41,4 +41,3 @@ declare const _default: {
|
|
|
41
41
|
parseCidr: typeof parseCidr;
|
|
42
42
|
};
|
|
43
43
|
export default _default;
|
|
44
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { ipVersion, parseIp, stringifyIp, normalizeIp } from
|
|
2
|
-
|
|
1
|
+
import { ipVersion, parseIp, stringifyIp, normalizeIp } from "ip-bigint";
|
|
3
2
|
const bits = { 4: 32, 6: 128 };
|
|
4
3
|
function uniq(arr) {
|
|
5
4
|
return Array.from(new Set(arr));
|
|
@@ -45,12 +44,12 @@ function parseCidr(str) {
|
|
|
45
44
|
cidr = `${str}/${bits[version2]}`;
|
|
46
45
|
parsed.version = version2;
|
|
47
46
|
} else {
|
|
48
|
-
throw new Error(`Network is not a CIDR or IP: ${str}`);
|
|
47
|
+
throw new Error(`Network is not a CIDR or IP: "${str}"`);
|
|
49
48
|
}
|
|
50
49
|
}
|
|
51
50
|
const [ipAndMisc, prefix] = cidr.split("/");
|
|
52
51
|
if (!/^[0-9]+$/.test(prefix)) {
|
|
53
|
-
throw new Error(`Network is not a CIDR or IP: ${str}`);
|
|
52
|
+
throw new Error(`Network is not a CIDR or IP: "${str}"`);
|
|
54
53
|
}
|
|
55
54
|
const { number, version, ipv4mapped, scopeid } = parseIp(ipAndMisc);
|
|
56
55
|
parsed.ip = stringifyIp({ number, version, ipv4mapped, scopeid });
|
|
@@ -111,6 +110,12 @@ function biggestPowerOfTwo(num) {
|
|
|
111
110
|
return 2n ** BigInt(String(num.toString(2).length - 1));
|
|
112
111
|
}
|
|
113
112
|
function subparts(part) {
|
|
113
|
+
if (part.end < part.start) {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
if (part.end === part.start) {
|
|
117
|
+
return [part];
|
|
118
|
+
}
|
|
114
119
|
if (part.end - part.start === 1n) {
|
|
115
120
|
if (part.end % 2n === 0n) {
|
|
116
121
|
return [{ start: part.start, end: part.start }, { start: part.end, end: part.end }];
|
|
@@ -172,24 +177,28 @@ function mapNets(nets) {
|
|
|
172
177
|
function doMerge(maps) {
|
|
173
178
|
let start = null;
|
|
174
179
|
let end = null;
|
|
175
|
-
const numbers = Object.keys(maps)
|
|
180
|
+
const numbers = Object.keys(maps).sort((a, b) => {
|
|
181
|
+
const aBig = BigInt(a);
|
|
182
|
+
const bBig = BigInt(b);
|
|
183
|
+
return aBig > bBig ? 1 : aBig < bBig ? -1 : 0;
|
|
184
|
+
});
|
|
176
185
|
let depth = 0;
|
|
177
186
|
const merged = [];
|
|
178
|
-
for (const [
|
|
187
|
+
for (const [index2, number] of numbers.entries()) {
|
|
179
188
|
const marker = maps[String(number)];
|
|
180
189
|
if (start === null && marker.start) start = BigInt(number);
|
|
181
190
|
if (marker.end) end = BigInt(number);
|
|
182
191
|
if (start === null) continue;
|
|
183
192
|
if (marker.start) depth += marker.start;
|
|
184
193
|
if (marker.end) depth -= marker.end;
|
|
185
|
-
const next = numbers[
|
|
194
|
+
const next = numbers[index2 + 1];
|
|
186
195
|
if (marker.end && depth === 0 && next && BigInt(next) - BigInt(number) > 1) {
|
|
187
196
|
for (const sub of subparts({ start, end })) {
|
|
188
197
|
merged.push(sub);
|
|
189
198
|
}
|
|
190
199
|
start = null;
|
|
191
200
|
end = null;
|
|
192
|
-
} else if (
|
|
201
|
+
} else if (index2 === numbers.length - 1) {
|
|
193
202
|
for (const sub of subparts({ start, end })) {
|
|
194
203
|
merged.push(sub);
|
|
195
204
|
}
|
|
@@ -198,7 +207,7 @@ function doMerge(maps) {
|
|
|
198
207
|
return merged;
|
|
199
208
|
}
|
|
200
209
|
function mergeCidr(nets) {
|
|
201
|
-
const arr = uniq((Array.isArray(nets) ? nets : [nets]).
|
|
210
|
+
const arr = uniq((Array.isArray(nets) ? nets : [nets]).map(parseCidr));
|
|
202
211
|
const maps = mapNets(arr);
|
|
203
212
|
const merged = { 4: [], 6: [] };
|
|
204
213
|
for (const v of [4, 6]) {
|
|
@@ -221,13 +230,13 @@ function excludeCidr(base, excl) {
|
|
|
221
230
|
}
|
|
222
231
|
for (const v of [4, 6]) {
|
|
223
232
|
for (const exclcidr of excls[v]) {
|
|
224
|
-
for (const [
|
|
233
|
+
for (const [index2, basecidr] of bases[v].entries()) {
|
|
225
234
|
const base2 = parseCidr(basecidr);
|
|
226
235
|
const excl2 = parseCidr(exclcidr);
|
|
227
236
|
const remainders = excludeNets(base2, excl2, v);
|
|
228
237
|
if (base2.cidr !== remainders.toString()) {
|
|
229
238
|
bases[v] = bases[v].concat(remainders);
|
|
230
|
-
bases[v].splice(
|
|
239
|
+
bases[v].splice(index2, 1);
|
|
231
240
|
}
|
|
232
241
|
}
|
|
233
242
|
}
|
|
@@ -289,5 +298,13 @@ const index = {
|
|
|
289
298
|
normalizeCidr,
|
|
290
299
|
parseCidr
|
|
291
300
|
};
|
|
292
|
-
|
|
293
|
-
|
|
301
|
+
export {
|
|
302
|
+
containsCidr,
|
|
303
|
+
index as default,
|
|
304
|
+
excludeCidr,
|
|
305
|
+
expandCidr,
|
|
306
|
+
mergeCidr,
|
|
307
|
+
normalizeCidr,
|
|
308
|
+
overlapCidr,
|
|
309
|
+
parseCidr
|
|
310
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cidr-tools",
|
|
3
|
-
"version": "11.0.
|
|
3
|
+
"version": "11.0.5",
|
|
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,19 +17,19 @@
|
|
|
17
17
|
"node": ">=18"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"ip-bigint": "^8.2.
|
|
20
|
+
"ip-bigint": "^8.2.3"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"
|
|
24
|
-
"eslint
|
|
25
|
-
"eslint-config-silverwind
|
|
26
|
-
"typescript": "5.
|
|
27
|
-
"typescript-config-silverwind": "
|
|
28
|
-
"updates": "
|
|
29
|
-
"versions": "
|
|
30
|
-
"vite": "
|
|
31
|
-
"vite-config-silverwind": "
|
|
32
|
-
"vitest": "
|
|
33
|
-
"vitest-config-silverwind": "10.
|
|
23
|
+
"@typescript/native-preview": "7.0.0-dev.20260128.1",
|
|
24
|
+
"eslint": "9.39.2",
|
|
25
|
+
"eslint-config-silverwind": "118.0.1",
|
|
26
|
+
"typescript": "5.9.3",
|
|
27
|
+
"typescript-config-silverwind": "14.0.0",
|
|
28
|
+
"updates": "17.0.9",
|
|
29
|
+
"versions": "14.0.3",
|
|
30
|
+
"vite": "7.3.1",
|
|
31
|
+
"vite-config-silverwind": "6.0.9",
|
|
32
|
+
"vitest": "4.0.18",
|
|
33
|
+
"vitest-config-silverwind": "10.6.1"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAIA,KAAK,WAAW,GAAG,MAAM,CAAC;AAC1B,KAAK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