cidr-tools 11.0.2 → 11.0.4

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) silverwind
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # cidr-tools
2
- [![](https://img.shields.io/npm/v/cidr-tools.svg?style=flat)](https://www.npmjs.org/package/cidr-tools) [![](https://img.shields.io/npm/dm/cidr-tools.svg)](https://www.npmjs.org/package/cidr-tools) [![](https://img.shields.io/bundlephobia/minzip/cidr-tools.svg)](https://bundlephobia.com/package/cidr-tools) [![](https://packagephobia.com/badge?p=cidr-tools)](https://packagephobia.com/result?p=cidr-tools)
2
+ [![](https://img.shields.io/npm/v/cidr-tools.svg?style=flat)](https://www.npmjs.org/package/cidr-tools) [![](https://img.shields.io/npm/dm/cidr-tools.svg)](https://www.npmjs.org/package/cidr-tools) [![](https://img.shields.io/bundlephobia/minzip/cidr-tools.svg)](https://bundlephobia.com/package/cidr-tools) [![](https://packagephobia.com/badge?p=cidr-tools)](https://packagephobia.com/result?p=cidr-tools) [![](https://depx.co/api/badge/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[]>(cidr: T, opts?: NormalizeOpts): T;
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 'ip-bigint';
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 }];
@@ -175,21 +180,21 @@ function doMerge(maps) {
175
180
  const numbers = Object.keys(maps);
176
181
  let depth = 0;
177
182
  const merged = [];
178
- for (const [index, number] of numbers.entries()) {
183
+ for (const [index2, number] of numbers.entries()) {
179
184
  const marker = maps[String(number)];
180
185
  if (start === null && marker.start) start = BigInt(number);
181
186
  if (marker.end) end = BigInt(number);
182
187
  if (start === null) continue;
183
188
  if (marker.start) depth += marker.start;
184
189
  if (marker.end) depth -= marker.end;
185
- const next = numbers[index + 1];
190
+ const next = numbers[index2 + 1];
186
191
  if (marker.end && depth === 0 && next && BigInt(next) - BigInt(number) > 1) {
187
192
  for (const sub of subparts({ start, end })) {
188
193
  merged.push(sub);
189
194
  }
190
195
  start = null;
191
196
  end = null;
192
- } else if (index === numbers.length - 1) {
197
+ } else if (index2 === numbers.length - 1) {
193
198
  for (const sub of subparts({ start, end })) {
194
199
  merged.push(sub);
195
200
  }
@@ -221,13 +226,13 @@ function excludeCidr(base, excl) {
221
226
  }
222
227
  for (const v of [4, 6]) {
223
228
  for (const exclcidr of excls[v]) {
224
- for (const [index, basecidr] of bases[v].entries()) {
229
+ for (const [index2, basecidr] of bases[v].entries()) {
225
230
  const base2 = parseCidr(basecidr);
226
231
  const excl2 = parseCidr(exclcidr);
227
232
  const remainders = excludeNets(base2, excl2, v);
228
233
  if (base2.cidr !== remainders.toString()) {
229
234
  bases[v] = bases[v].concat(remainders);
230
- bases[v].splice(index, 1);
235
+ bases[v].splice(index2, 1);
231
236
  }
232
237
  }
233
238
  }
@@ -289,5 +294,13 @@ const index = {
289
294
  normalizeCidr,
290
295
  parseCidr
291
296
  };
292
-
293
- export { containsCidr, index as default, excludeCidr, expandCidr, mergeCidr, normalizeCidr, overlapCidr, parseCidr };
297
+ export {
298
+ containsCidr,
299
+ index as default,
300
+ excludeCidr,
301
+ expandCidr,
302
+ mergeCidr,
303
+ normalizeCidr,
304
+ overlapCidr,
305
+ parseCidr
306
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "11.0.2",
3
+ "version": "11.0.4",
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,18 +17,19 @@
17
17
  "node": ">=18"
18
18
  },
19
19
  "dependencies": {
20
- "ip-bigint": "^8.2.0"
20
+ "ip-bigint": "^8.2.3"
21
21
  },
22
22
  "devDependencies": {
23
- "eslint": "8.57.0",
24
- "eslint-config-silverwind": "91.0.6",
25
- "eslint-config-silverwind-typescript": "8.0.6",
26
- "typescript-config-silverwind": "6.0.0",
27
- "updates": "16.4.0",
28
- "versions": "12.1.2",
29
- "vite": "5.4.2",
30
- "vite-config-silverwind": "3.0.0",
31
- "vitest": "2.0.5",
32
- "vitest-config-silverwind": "9.1.0"
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"
33
34
  }
34
35
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAIA,KAAK,WAAW,GAAG,MAAM,CAAC;AAC1B,KAAK,WAAW,GAAG,MAAM,CAAC;AAC1B,KAAK,QAAQ,GAAG,MAAM,CAAC;AACvB,KAAK,QAAQ,GAAG,MAAM,CAAC;AAEvB,KAAK,OAAO,GAAG,WAAW,GAAG,QAAQ,GAAG,WAAW,GAAG,QAAQ,CAAC;AAC/D,KAAK,QAAQ,GAAG,OAAO,GAAG,OAAO,EAAE,CAAC;AAEpC,KAAK,cAAc,GAAG,CAAC,GAAG,CAA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