cidr-tools
Advanced tools
Comparing version 3.0.5 to 3.0.6
93
index.js
"use strict"; | ||
const cidrTools = module.exports = {}; | ||
const IPCIDR = require("ip-cidr"); | ||
@@ -23,9 +22,28 @@ const isIp = require("is-ip"); | ||
module.exports.normalize = (cidr) => { | ||
const cidrVersion = isCidr(cidr); | ||
if (cidrVersion === 4) { | ||
return cidr; | ||
} else if (cidrVersion === 6) { | ||
const [ip, prefix] = cidr.split("/"); | ||
return `${ipv6Normalize(ip)}/${prefix}`; | ||
} | ||
const parsed = parse(cidr); | ||
if (parsed && parsed.address && parsed.address.v4) { | ||
return cidr; | ||
} else if (parsed && parsed.address && parsed.address.v4 === false) { | ||
return ipv6Normalize(cidr); | ||
} | ||
throw new Error(`Invalid network: ${cidr}`); | ||
}; | ||
function parse(str) { | ||
if (isCidr(str)) { | ||
return new IPCIDR(cidrTools.normalize(str)); | ||
return new IPCIDR(module.exports.normalize(str)); | ||
} else { | ||
const version = isIp.version(str); | ||
if (version) { | ||
return new IPCIDR(cidrTools.normalize(`${str}/${bits[`v${version}`]}`)); | ||
return new IPCIDR(module.exports.normalize(`${str}/${bits[`v${version}`]}`)); | ||
} else { | ||
@@ -40,3 +58,3 @@ throw new Error(`Network is not a CIDR or IP: ${str}`); | ||
if (!(number instanceof BigInteger)) number = bigint(number); | ||
return cidrTools.normalize(cls.fromBigInteger(number).address); | ||
return module.exports.normalize(cls.fromBigInteger(number).address); | ||
} | ||
@@ -48,3 +66,3 @@ | ||
function overlap(a, b) { | ||
function doNetsOverlap(a, b) { | ||
const aStart = a.start({type: "bigInteger"}); | ||
@@ -67,3 +85,3 @@ const bStart = b.start({type: "bigInteger"}); | ||
// exclude b from a and return remainder cidrs | ||
function exclude(a, b, v) { | ||
function excludeNets(a, b, v) { | ||
const aStart = a.start({type: "bigInteger"}); | ||
@@ -102,6 +120,3 @@ const bStart = b.start({type: "bigInteger"}); | ||
if (aStart.compareTo(bStart) < 0 && aEnd.compareTo(bEnd) <= 0) { | ||
parts.push({ | ||
start: aStart, | ||
end: bStart.subtract(one), | ||
}); | ||
parts.push({start: aStart, end: bStart.subtract(one)}); | ||
} | ||
@@ -114,6 +129,3 @@ | ||
if (aStart.compareTo(bStart) >= 0 && aEnd.compareTo(bEnd) > 0) { | ||
parts.push({ | ||
start: bEnd.add(one), | ||
end: aEnd, | ||
}); | ||
parts.push({start: bEnd.add(one), end: aEnd}); | ||
} | ||
@@ -124,10 +136,6 @@ | ||
if (aStart.compareTo(bStart) < 0 && aEnd.compareTo(bEnd) > 0) { | ||
parts.push({ | ||
start: aStart, | ||
end: bStart.subtract(one), | ||
}); | ||
parts.push({ | ||
start: bEnd.add(one), | ||
end: aEnd, | ||
}); | ||
parts.push( | ||
{start: aStart, end: bStart.subtract(one)}, | ||
{start: bEnd.add(one), end: aEnd}, | ||
); | ||
} | ||
@@ -142,3 +150,3 @@ | ||
return cidrTools.merge(remaining); | ||
return module.exports.merge(remaining); | ||
} | ||
@@ -221,21 +229,2 @@ | ||
cidrTools.normalize = (cidr) => { | ||
const cidrVersion = isCidr(cidr); | ||
if (cidrVersion === 4) { | ||
return cidr; | ||
} else if (cidrVersion === 6) { | ||
const [ip, prefix] = cidr.split("/"); | ||
return `${ipv6Normalize(ip)}/${prefix}`; | ||
} | ||
const parsed = parse(cidr); | ||
if (parsed && parsed.address && parsed.address.v4) { | ||
return cidr; | ||
} else if (parsed && parsed.address && parsed.address.v4 === false) { | ||
return ipv6Normalize(cidr); | ||
} | ||
throw new Error(`Invalid network: ${cidr}`); | ||
}; | ||
function mapNets(nets) { | ||
@@ -266,3 +255,3 @@ const maps = {v4: {}, v6: {}}; | ||
cidrTools.merge = function(nets) { | ||
module.exports.merge = function(nets) { | ||
nets = uniq((Array.isArray(nets) ? nets : [nets]).map(parse)); | ||
@@ -311,8 +300,8 @@ const maps = mapNets(nets); | ||
cidrTools.exclude = function(basenets, exclnets) { | ||
module.exports.exclude = (basenets, exclnets) => { | ||
basenets = uniq(Array.isArray(basenets) ? basenets : [basenets]); | ||
exclnets = uniq(Array.isArray(exclnets) ? exclnets : [exclnets]); | ||
basenets = cidrTools.merge(basenets); | ||
exclnets = cidrTools.merge(exclnets); | ||
basenets = module.exports.merge(basenets); | ||
exclnets = module.exports.merge(exclnets); | ||
@@ -335,3 +324,3 @@ const bases = {v4: [], v6: []}; | ||
const excl = parse(exclcidr); | ||
const remainders = exclude(base, excl, v); | ||
const remainders = excludeNets(base, excl, v); | ||
if (base.toString() !== remainders.toString()) { | ||
@@ -348,13 +337,13 @@ bases[v] = bases[v].concat(remainders); | ||
cidrTools.expand = function(nets) { | ||
module.exports.expand = (nets) => { | ||
nets = uniq(Array.isArray(nets) ? nets : [nets]); | ||
let ips = []; | ||
for (const net of cidrTools.merge(nets)) { | ||
for (const net of module.exports.merge(nets)) { | ||
ips = ips.concat((new IPCIDR(net)).toArray()); | ||
} | ||
return ips.map(cidrTools.normalize); | ||
return ips.map(module.exports.normalize); | ||
}; | ||
cidrTools.overlap = (a, b) => { | ||
module.exports.overlap = (a, b) => { | ||
const aNets = uniq(Array.isArray(a) ? a : [a]); | ||
@@ -372,3 +361,3 @@ const bNets = uniq(Array.isArray(b) ? b : [b]); | ||
if (overlap(aParsed, bParsed)) { | ||
if (doNetsOverlap(aParsed, bParsed)) { | ||
return true; | ||
@@ -375,0 +364,0 @@ } |
{ | ||
"name": "cidr-tools", | ||
"version": "3.0.5", | ||
"version": "3.0.6", | ||
"author": "silverwind <me@silverwind.io>", | ||
@@ -29,4 +29,4 @@ "description": "Tools to work with IPv4 and IPv6 CIDR network lists", | ||
"dependencies": { | ||
"ip-address": "^7.1.0", | ||
"ip-cidr": "^2.1.2", | ||
"ip-address": "^8.1.0", | ||
"ip-cidr": "^3.0.4", | ||
"ipv6-normalize": "^1.0.1", | ||
@@ -39,7 +39,7 @@ "is-cidr": "^4.0.2", | ||
"devDependencies": { | ||
"eslint": "7.20.0", | ||
"eslint-config-silverwind": "28.0.0", | ||
"jest": "26.6.3", | ||
"updates": "11.4.2", | ||
"versions": "8.4.4" | ||
"eslint": "8.4.1", | ||
"eslint-config-silverwind": "47.1.0", | ||
"jest": "27.4.5", | ||
"updates": "12.2.3", | ||
"versions": "9.1.1" | ||
}, | ||
@@ -46,0 +46,0 @@ "jest": { |
14258
309
+ Addedip-address@8.1.0(transitive)
+ Addedip-cidr@3.1.0(transitive)
- Removedip-address@6.4.0(transitive)
- Removedip-cidr@2.1.5(transitive)
- Removedlodash.find@4.6.0(transitive)
- Removedlodash.max@4.0.1(transitive)
- Removedlodash.merge@4.6.2(transitive)
- Removedlodash.padstart@4.6.1(transitive)
- Removedlodash.repeat@4.1.0(transitive)
Updatedip-address@^8.1.0
Updatedip-cidr@^3.0.4