cidr-tools 3.0.5 → 3.0.6

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 (2) hide show
  1. package/index.js +41 -52
  2. package/package.json +8 -8
package/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
 
3
- const cidrTools = module.exports = {};
4
3
  const IPCIDR = require("ip-cidr");
5
4
  const isIp = require("is-ip");
6
5
  const isCidr = require("is-cidr");
@@ -20,13 +19,32 @@ const zero = bigint("0");
20
19
  const one = bigint("1");
21
20
  const two = bigint("2");
22
21
 
22
+ module.exports.normalize = (cidr) => {
23
+ const cidrVersion = isCidr(cidr);
24
+ if (cidrVersion === 4) {
25
+ return cidr;
26
+ } else if (cidrVersion === 6) {
27
+ const [ip, prefix] = cidr.split("/");
28
+ return `${ipv6Normalize(ip)}/${prefix}`;
29
+ }
30
+
31
+ const parsed = parse(cidr);
32
+ if (parsed && parsed.address && parsed.address.v4) {
33
+ return cidr;
34
+ } else if (parsed && parsed.address && parsed.address.v4 === false) {
35
+ return ipv6Normalize(cidr);
36
+ }
37
+
38
+ throw new Error(`Invalid network: ${cidr}`);
39
+ };
40
+
23
41
  function parse(str) {
24
42
  if (isCidr(str)) {
25
- return new IPCIDR(cidrTools.normalize(str));
43
+ return new IPCIDR(module.exports.normalize(str));
26
44
  } else {
27
45
  const version = isIp.version(str);
28
46
  if (version) {
29
- return new IPCIDR(cidrTools.normalize(`${str}/${bits[`v${version}`]}`));
47
+ return new IPCIDR(module.exports.normalize(`${str}/${bits[`v${version}`]}`));
30
48
  } else {
31
49
  throw new Error(`Network is not a CIDR or IP: ${str}`);
32
50
  }
@@ -36,14 +54,14 @@ function parse(str) {
36
54
  function format(number, v) {
37
55
  const cls = v === "v6" ? Address6 : Address4;
38
56
  if (!(number instanceof BigInteger)) number = bigint(number);
39
- return cidrTools.normalize(cls.fromBigInteger(number).address);
57
+ return module.exports.normalize(cls.fromBigInteger(number).address);
40
58
  }
41
59
 
42
60
  function uniq(arr) {
43
61
  return [...new Set(arr)];
44
62
  }
45
63
 
46
- function overlap(a, b) {
64
+ function doNetsOverlap(a, b) {
47
65
  const aStart = a.start({type: "bigInteger"});
48
66
  const bStart = b.start({type: "bigInteger"});
49
67
  const aEnd = a.end({type: "bigInteger"});
@@ -61,7 +79,7 @@ function overlap(a, b) {
61
79
  }
62
80
 
63
81
  // exclude b from a and return remainder cidrs
64
- function exclude(a, b, v) {
82
+ function excludeNets(a, b, v) {
65
83
  const aStart = a.start({type: "bigInteger"});
66
84
  const bStart = b.start({type: "bigInteger"});
67
85
  const aEnd = a.end({type: "bigInteger"});
@@ -95,10 +113,7 @@ function exclude(a, b, v) {
95
113
  // aaaa
96
114
  // bb
97
115
  if (aStart.compareTo(bStart) < 0 && aEnd.compareTo(bEnd) <= 0) {
98
- parts.push({
99
- start: aStart,
100
- end: bStart.subtract(one),
101
- });
116
+ parts.push({start: aStart, end: bStart.subtract(one)});
102
117
  }
103
118
 
104
119
  // aaa
@@ -106,23 +121,16 @@ function exclude(a, b, v) {
106
121
  // aaaa
107
122
  // bbb
108
123
  if (aStart.compareTo(bStart) >= 0 && aEnd.compareTo(bEnd) > 0) {
109
- parts.push({
110
- start: bEnd.add(one),
111
- end: aEnd,
112
- });
124
+ parts.push({start: bEnd.add(one), end: aEnd});
113
125
  }
114
126
 
115
127
  // aaaa
116
128
  // bb
117
129
  if (aStart.compareTo(bStart) < 0 && aEnd.compareTo(bEnd) > 0) {
118
- parts.push({
119
- start: aStart,
120
- end: bStart.subtract(one),
121
- });
122
- parts.push({
123
- start: bEnd.add(one),
124
- end: aEnd,
125
- });
130
+ parts.push(
131
+ {start: aStart, end: bStart.subtract(one)},
132
+ {start: bEnd.add(one), end: aEnd},
133
+ );
126
134
  }
127
135
 
128
136
  const remaining = [];
@@ -132,7 +140,7 @@ function exclude(a, b, v) {
132
140
  }
133
141
  }
134
142
 
135
- return cidrTools.merge(remaining);
143
+ return module.exports.merge(remaining);
136
144
  }
137
145
 
138
146
  function biggestPowerOfTwo(num) {
@@ -210,25 +218,6 @@ function formatPart(part, v) {
210
218
  return `${ip}/${prefix}`;
211
219
  }
212
220
 
213
- cidrTools.normalize = (cidr) => {
214
- const cidrVersion = isCidr(cidr);
215
- if (cidrVersion === 4) {
216
- return cidr;
217
- } else if (cidrVersion === 6) {
218
- const [ip, prefix] = cidr.split("/");
219
- return `${ipv6Normalize(ip)}/${prefix}`;
220
- }
221
-
222
- const parsed = parse(cidr);
223
- if (parsed && parsed.address && parsed.address.v4) {
224
- return cidr;
225
- } else if (parsed && parsed.address && parsed.address.v4 === false) {
226
- return ipv6Normalize(cidr);
227
- }
228
-
229
- throw new Error(`Invalid network: ${cidr}`);
230
- };
231
-
232
221
  function mapNets(nets) {
233
222
  const maps = {v4: {}, v6: {}};
234
223
  for (const net of nets) {
@@ -254,7 +243,7 @@ function mapNets(nets) {
254
243
  return maps;
255
244
  }
256
245
 
257
- cidrTools.merge = function(nets) {
246
+ module.exports.merge = function(nets) {
258
247
  nets = uniq((Array.isArray(nets) ? nets : [nets]).map(parse));
259
248
  const maps = mapNets(nets);
260
249
 
@@ -298,12 +287,12 @@ cidrTools.merge = function(nets) {
298
287
  return merged.v4.concat(merged.v6);
299
288
  };
300
289
 
301
- cidrTools.exclude = function(basenets, exclnets) {
290
+ module.exports.exclude = (basenets, exclnets) => {
302
291
  basenets = uniq(Array.isArray(basenets) ? basenets : [basenets]);
303
292
  exclnets = uniq(Array.isArray(exclnets) ? exclnets : [exclnets]);
304
293
 
305
- basenets = cidrTools.merge(basenets);
306
- exclnets = cidrTools.merge(exclnets);
294
+ basenets = module.exports.merge(basenets);
295
+ exclnets = module.exports.merge(exclnets);
307
296
 
308
297
  const bases = {v4: [], v6: []};
309
298
  const excls = {v4: [], v6: []};
@@ -321,7 +310,7 @@ cidrTools.exclude = function(basenets, exclnets) {
321
310
  for (const [index, basecidr] of bases[v].entries()) {
322
311
  const base = parse(basecidr);
323
312
  const excl = parse(exclcidr);
324
- const remainders = exclude(base, excl, v);
313
+ const remainders = excludeNets(base, excl, v);
325
314
  if (base.toString() !== remainders.toString()) {
326
315
  bases[v] = bases[v].concat(remainders);
327
316
  bases[v].splice(index, 1);
@@ -333,17 +322,17 @@ cidrTools.exclude = function(basenets, exclnets) {
333
322
  return bases.v4.concat(bases.v6);
334
323
  };
335
324
 
336
- cidrTools.expand = function(nets) {
325
+ module.exports.expand = (nets) => {
337
326
  nets = uniq(Array.isArray(nets) ? nets : [nets]);
338
327
 
339
328
  let ips = [];
340
- for (const net of cidrTools.merge(nets)) {
329
+ for (const net of module.exports.merge(nets)) {
341
330
  ips = ips.concat((new IPCIDR(net)).toArray());
342
331
  }
343
- return ips.map(cidrTools.normalize);
332
+ return ips.map(module.exports.normalize);
344
333
  };
345
334
 
346
- cidrTools.overlap = (a, b) => {
335
+ module.exports.overlap = (a, b) => {
347
336
  const aNets = uniq(Array.isArray(a) ? a : [a]);
348
337
  const bNets = uniq(Array.isArray(b) ? b : [b]);
349
338
 
@@ -356,7 +345,7 @@ cidrTools.overlap = (a, b) => {
356
345
  continue;
357
346
  }
358
347
 
359
- if (overlap(aParsed, bParsed)) {
348
+ if (doNetsOverlap(aParsed, bParsed)) {
360
349
  return true;
361
350
  }
362
351
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "3.0.5",
3
+ "version": "3.0.6",
4
4
  "author": "silverwind <me@silverwind.io>",
5
5
  "description": "Tools to work with IPv4 and IPv6 CIDR network lists",
6
6
  "repository": "silverwind/cidr-tools",
@@ -26,8 +26,8 @@
26
26
  ],
27
27
  "types": "./index.d.ts",
28
28
  "dependencies": {
29
- "ip-address": "^7.1.0",
30
- "ip-cidr": "^2.1.2",
29
+ "ip-address": "^8.1.0",
30
+ "ip-cidr": "^3.0.4",
31
31
  "ipv6-normalize": "^1.0.1",
32
32
  "is-cidr": "^4.0.2",
33
33
  "is-ip": "^3.1.0",
@@ -35,11 +35,11 @@
35
35
  "string-natural-compare": "^3.0.1"
36
36
  },
37
37
  "devDependencies": {
38
- "eslint": "7.20.0",
39
- "eslint-config-silverwind": "28.0.0",
40
- "jest": "26.6.3",
41
- "updates": "11.4.2",
42
- "versions": "8.4.4"
38
+ "eslint": "8.4.1",
39
+ "eslint-config-silverwind": "47.1.0",
40
+ "jest": "27.4.5",
41
+ "updates": "12.2.3",
42
+ "versions": "9.1.1"
43
43
  },
44
44
  "jest": {
45
45
  "verbose": false,