cidr-tools 8.0.0 → 9.0.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.
Files changed (4) hide show
  1. package/README.md +17 -17
  2. package/index.d.ts +14 -14
  3. package/index.js +30 -30
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -5,15 +5,15 @@
5
5
  ## Usage
6
6
 
7
7
  ```js
8
- import {merge, exclude, expand, overlap, contains, normalize, parse} from "cidr-tools";
9
-
10
- merge(["1.0.0.0/24", "1.0.1.0/24"]); //=> ["1.0.0.0/23"]
11
- exclude(["::1/127"], "::1/128") //=> ["::/128"]
12
- expand(["2001:db8::/126"]) //=> ["2001:db8::", "2001:db8::1", "2001:db8::2", "2001:db8::3"]
13
- overlap("1.0.0.0/24", "1.0.0.128/25") //=> true
14
- contains(["1.0.0.0/24", "2.0.0.0/24"], "1.0.0.1") //=> true
15
- normalize("::ffff/64") //=> "::/64"
16
- parse("::/64"); // => {cidr: "::/64", version: 6, prefix: "64", start: 0n, end: 18446744073709551615n}
8
+ import {mergeCidr, excludeCidr, expandCidr, overlapCidr, containsCidr, normalizeCidr, parseCidr} from "cidr-tools";
9
+
10
+ mergeCidr(["1.0.0.0/24", "1.0.1.0/24"]); //=> ["1.0.0.0/23"]
11
+ excludeCidr(["::1/127"], "::1/128") //=> ["::/128"]
12
+ expandCidr(["2001:db8::/126"]) //=> ["2001:db8::", "2001:db8::1", "2001:db8::2", "2001:db8::3"]
13
+ overlapCidr("1.0.0.0/24", "1.0.0.128/25") //=> true
14
+ containsCidr(["1.0.0.0/24", "2.0.0.0/24"], "1.0.0.1") //=> true
15
+ normalizeCidr("::ffff/64") //=> "::/64"
16
+ parseCidr("::/64"); // => {cidr: "::/64", version: 6, prefix: "64", start: 0n, end: 18446744073709551615n}
17
17
  ```
18
18
 
19
19
  ## API
@@ -24,40 +24,40 @@ It is expected that the passed CIDRs and IPs are validated as the module's own i
24
24
 
25
25
  This module requires [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility) support in your environment.
26
26
 
27
- ### merge(networks)
27
+ ### mergeCidr(networks)
28
28
 
29
29
  - `networks` *String* or *Array*: One or more CIDR or IP addresses.
30
30
 
31
31
  Returns an array of merged networks.
32
32
 
33
- ### exclude(baseNetworks, excludeNetworks)
33
+ ### excludeCidr(baseNetworks, excludeNetworks)
34
34
 
35
35
  - `baseNetworks` *String* or *Array*: One or more CIDR or IP addresses.
36
36
  - `excludeNetworks` *String* or *Array*: One or more CIDR or IP addresses to exclude from `baseNetworks`.
37
37
 
38
38
  Returns an array of merged remaining networks.
39
39
 
40
- ### expand(networks)
40
+ ### expandCidr(networks)
41
41
 
42
42
  - `networks` *String* or *Array*: One or more CIDR or IP addresses.
43
43
 
44
44
  Returns an array of individual IPs contained in the networks.
45
45
 
46
- ### overlap(networksA, networksB)
46
+ ### overlapCidr(networksA, networksB)
47
47
 
48
48
  - `networksA` *String* or *Array*: One or more CIDR or IP address.
49
49
  - `networksB` *String* or *Array*: One or more CIDR or IP address.
50
50
 
51
51
  Returns a boolean that indicates if `networksA` overlap (intersect) with `networksB`.
52
52
 
53
- ### contains(networksA, networksB)
53
+ ### containsCidr(networksA, networksB)
54
54
 
55
55
  - `networksA` *String* or *Array*: One or more CIDR or IP address.
56
56
  - `networksB` *String* or *Array*: One or more CIDR or IP address.
57
57
 
58
58
  Returns a boolean that indicates whether `networksA` fully contain all `networksB`.
59
59
 
60
- ### normalize(networks, [opts])
60
+ ### normalizeCidr(networks, [opts])
61
61
 
62
62
  - `networks` *String* or *Array*: One or more CIDR or IP address.
63
63
 
@@ -67,7 +67,7 @@ Returns a string or array (depending on input) with a normalized representation.
67
67
  - `compress`: Whether to compress the IP. For IPv6, this means the "best representation" all-lowercase shortest possible form. Default: `true`.
68
68
  - `hexify`: Whether to convert IPv4-Mapped IPv6 addresses to hex. Default: `false`.
69
69
 
70
- ### parse(network)
70
+ ### parseCidr(network)
71
71
 
72
72
  - `network` *String*: A CIDR or IP address.
73
73
 
@@ -76,7 +76,7 @@ Returns a `parsed` Object which is used internally by this module. It can be use
76
76
  `parsed`: `Object`
77
77
  - `cidr` String: The CIDR of the network.
78
78
  - `version` Number: IP protocol version. Either `4` or `6`.
79
- - `prefix` String: The network prefix, e.g. `/64`.
79
+ - `prefix` String: The network prefix, e.g. `64`.
80
80
  - `start` BigInt: Start number of the network.
81
81
  - `end` BigInt: End number of the network.
82
82
 
package/index.d.ts CHANGED
@@ -18,21 +18,21 @@ type NormalizeOpts = {
18
18
  hexify?: boolean;
19
19
  };
20
20
 
21
- export function merge(networks: Networks): Network[];
22
- export function exclude(baseNetworks: Networks, excludeNetworks: Networks): Network[];
23
- export function expand(networks: Networks): Network[];
24
- export function overlap(networksA: Networks, networksB: Networks): boolean;
25
- export function normalize(cidr: Networks, opts?: NormalizeOpts): Networks;
26
- export function contains(networksA: Networks, networksB: Networks): boolean;
27
- export function parse(network: Network): Parsed;
21
+ export function mergeCidr(networks: Networks): Network[];
22
+ export function excludeCidr(baseNetworks: Networks, excludeNetworks: Networks): Network[];
23
+ export function expandCidr(networks: Networks): Network[];
24
+ export function overlapCidr(networksA: Networks, networksB: Networks): boolean;
25
+ export function normalizeCidr(cidr: Networks, opts?: NormalizeOpts): Networks;
26
+ export function containsCidr(networksA: Networks, networksB: Networks): boolean;
27
+ export function parseCidr(network: Network): Parsed;
28
28
 
29
29
  declare const _default: {
30
- merge: typeof merge;
31
- exclude: typeof exclude;
32
- expand: typeof expand;
33
- overlap: typeof overlap;
34
- normalize: typeof normalize;
35
- contains: typeof contains;
36
- parse: typeof parse;
30
+ mergeCidr: typeof mergeCidr;
31
+ excludeCidr: typeof excludeCidr;
32
+ expandCidr: typeof expandCidr;
33
+ overlapCidr: typeof overlapCidr;
34
+ normalizeCidr: typeof normalizeCidr;
35
+ containsCidr: typeof containsCidr;
36
+ parseCidr: typeof parseCidr;
37
37
  };
38
38
  export default _default;
package/index.js CHANGED
@@ -16,7 +16,7 @@ function compare(a, b) {
16
16
  }
17
17
 
18
18
  function doNormalize(cidr, {compress = true, hexify = false} = {}) {
19
- const {start, end, prefix, version} = parse(cidr);
19
+ const {start, end, prefix, version} = parseCidr(cidr);
20
20
  if (start !== end) { // cidr
21
21
  // set network address to first address
22
22
  const ip = normalizeIp(stringifyIp({number: start, version}), {compress, hexify});
@@ -26,15 +26,15 @@ function doNormalize(cidr, {compress = true, hexify = false} = {}) {
26
26
  }
27
27
  }
28
28
 
29
- export function normalize(cidr, {compress = true, hexify = false} = {}) {
29
+ export function normalizeCidr(cidr, {compress = true, hexify = false} = {}) {
30
30
  if (Array.isArray(cidr)) {
31
- return cidr.map(entry => normalize(entry, {compress, hexify}));
31
+ return cidr.map(entry => normalizeCidr(entry, {compress, hexify}));
32
32
  } else {
33
33
  return doNormalize(cidr, {compress, hexify});
34
34
  }
35
35
  }
36
36
 
37
- export function parse(str) {
37
+ export function parseCidr(str) {
38
38
  const cidrVer = cidrVersion(str);
39
39
  const parsed = Object.create(null);
40
40
 
@@ -152,7 +152,7 @@ function excludeNets(a, b, v) {
152
152
  }
153
153
  }
154
154
 
155
- return merge(remaining);
155
+ return mergeCidr(remaining);
156
156
  }
157
157
 
158
158
  function biggestPowerOfTwo(num) {
@@ -223,7 +223,7 @@ function diff(a, b) {
223
223
  }
224
224
 
225
225
  function formatPart(part, version) {
226
- const ip = normalize(stringifyIp({number: BigInt(part.start.toString()), version}));
226
+ const ip = normalizeCidr(stringifyIp({number: BigInt(part.start.toString()), version}));
227
227
  const zeroes = diff(part.end, part.start).toString(2);
228
228
  const prefix = bits[version] - (zeroes.match(/0/g) || []).length;
229
229
  return `${ip}/${prefix}`;
@@ -288,9 +288,9 @@ function doMerge(maps, v) {
288
288
  return merged;
289
289
  }
290
290
 
291
- export function merge(nets) {
291
+ export function mergeCidr(nets) {
292
292
  // sort to workaround https://github.com/silverwind/cidr-tools/issues/17
293
- nets = uniq((Array.isArray(nets) ? nets : [nets]).sort(compare).map(parse));
293
+ nets = uniq((Array.isArray(nets) ? nets : [nets]).sort(compare).map(parseCidr));
294
294
  const maps = mapNets(nets);
295
295
 
296
296
  const merged = {4: [], 6: []};
@@ -301,12 +301,12 @@ export function merge(nets) {
301
301
  return [...merged[4].sort(compare), ...merged[6].sort(compare)];
302
302
  }
303
303
 
304
- export function exclude(basenets, exclnets) {
304
+ export function excludeCidr(basenets, exclnets) {
305
305
  basenets = uniq(Array.isArray(basenets) ? basenets : [basenets]);
306
306
  exclnets = uniq(Array.isArray(exclnets) ? exclnets : [exclnets]);
307
307
 
308
- basenets = merge(basenets);
309
- exclnets = merge(exclnets);
308
+ basenets = mergeCidr(basenets);
309
+ exclnets = mergeCidr(exclnets);
310
310
 
311
311
  const bases = {4: [], 6: []};
312
312
  const excls = {4: [], 6: []};
@@ -322,8 +322,8 @@ export function exclude(basenets, exclnets) {
322
322
  for (const v of [4, 6]) {
323
323
  for (const exclcidr of excls[v]) {
324
324
  for (const [index, basecidr] of bases[v].entries()) {
325
- const base = parse(basecidr);
326
- const excl = parse(exclcidr);
325
+ const base = parseCidr(basecidr);
326
+ const excl = parseCidr(exclcidr);
327
327
  const remainders = excludeNets(base, excl, v);
328
328
  if (base.cidr !== remainders.toString()) {
329
329
  bases[v] = bases[v].concat(remainders);
@@ -336,27 +336,27 @@ export function exclude(basenets, exclnets) {
336
336
  return bases[4].concat(bases[6]).sort(compare);
337
337
  }
338
338
 
339
- export function expand(nets) {
339
+ export function expandCidr(nets) {
340
340
  nets = uniq(Array.isArray(nets) ? nets : [nets]);
341
341
 
342
342
  const ips = [];
343
- for (const net of merge(nets)) {
344
- const {start, end, version} = parse(net);
343
+ for (const net of mergeCidr(nets)) {
344
+ const {start, end, version} = parseCidr(net);
345
345
  for (let number = start; number <= end; number++) {
346
346
  ips.push(stringifyIp({number, version}));
347
347
  }
348
348
  }
349
- return ips.map(normalize);
349
+ return ips.map(normalizeCidr);
350
350
  }
351
351
 
352
- export function overlap(a, b) {
352
+ export function overlapCidr(a, b) {
353
353
  const aNets = uniq(Array.isArray(a) ? a : [a]);
354
354
  const bNets = uniq(Array.isArray(b) ? b : [b]);
355
355
 
356
356
  for (const a of aNets) {
357
- const aParsed = parse(a);
357
+ const aParsed = parseCidr(a);
358
358
  for (const b of bNets) {
359
- const bParsed = parse(b);
359
+ const bParsed = parseCidr(b);
360
360
 
361
361
  // version mismatch
362
362
  if (aParsed.version !== bParsed.version) {
@@ -372,16 +372,16 @@ export function overlap(a, b) {
372
372
  return false;
373
373
  }
374
374
 
375
- export function contains(a, b) {
375
+ export function containsCidr(a, b) {
376
376
  const aNets = uniq(Array.isArray(a) ? a : [a]);
377
377
  const bNets = uniq(Array.isArray(b) ? b : [b]);
378
378
 
379
379
  const numExpected = bNets.length;
380
380
  let numFound = 0;
381
381
  for (const a of aNets) {
382
- const aParsed = parse(a);
382
+ const aParsed = parseCidr(a);
383
383
  for (const b of bNets) {
384
- const bParsed = parse(b);
384
+ const bParsed = parseCidr(b);
385
385
 
386
386
  // version mismatch
387
387
  if (aParsed.version !== bParsed.version) {
@@ -399,11 +399,11 @@ export function contains(a, b) {
399
399
  }
400
400
 
401
401
  export default {
402
- merge,
403
- exclude,
404
- expand,
405
- overlap,
406
- contains,
407
- normalize,
408
- parse,
402
+ mergeCidr,
403
+ excludeCidr,
404
+ expandCidr,
405
+ overlapCidr,
406
+ containsCidr,
407
+ normalizeCidr,
408
+ parseCidr,
409
409
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "8.0.0",
3
+ "version": "9.0.0",
4
4
  "author": "silverwind <me@silverwind.io>",
5
5
  "description": "Tools to work with IPv4 and IPv6 CIDR",
6
6
  "repository": "silverwind/cidr-tools",