cidr-tools
Advanced tools
Comparing version 4.3.0 to 5.0.0
83
index.js
@@ -1,11 +0,9 @@ | ||
"use strict"; | ||
import IPCIDR from "ip-cidr"; | ||
import ipRegex from "ip-regex"; | ||
import isCidr from "is-cidr"; | ||
import ipv6Normalize from "ipv6-normalize"; | ||
import naturalCompare from "string-natural-compare"; | ||
import {Address4, Address6} from "@silverwind/ip-address"; // https://github.com/beaugunderson/ip-address/issues/153 | ||
import {BigInteger} from "jsbn"; | ||
const IPCIDR = require("ip-cidr"); | ||
const isIp = require("is-ip"); | ||
const isCidr = require("is-cidr"); | ||
const ipv6Normalize = require("ipv6-normalize"); | ||
const naturalCompare = require("string-natural-compare"); | ||
const {Address4, Address6} = require("ip-address"); | ||
const {BigInteger} = require("jsbn"); | ||
const bits = { | ||
@@ -22,11 +20,22 @@ "v4": 32, | ||
function isIP(ip) { | ||
if (ipRegex.v4({exact: true}).test(ip)) return 4; | ||
if (ipRegex.v6({exact: true}).test(ip)) return 6; | ||
return 0; | ||
} | ||
function doNormalize(cidr) { | ||
const cidrVersion = isCidr(cidr); | ||
if (cidrVersion === 4) { | ||
return cidr; | ||
} else if (cidrVersion === 6) { | ||
const [ip, prefix] = cidr.split("/"); | ||
return `${ipv6Normalize(ip)}/${prefix}`; | ||
// cidr | ||
if (cidrVersion) { | ||
// set network address to first address | ||
let start = (new IPCIDR(cidr)).start(); | ||
if (cidrVersion === 6) start = ipv6Normalize(start).toString(); | ||
if (start) { | ||
return `${start}${cidr.match(/\/.+/)}`.toLowerCase(); | ||
} | ||
} | ||
// single ip | ||
const parsed = parse(cidr); | ||
@@ -42,13 +51,13 @@ if (parsed && parsed.address && parsed.address.v4) { | ||
module.exports.normalize = (cidr) => { | ||
export function normalize(cidr) { | ||
return Array.isArray(cidr) ? cidr.map(doNormalize) : doNormalize(cidr); | ||
}; | ||
} | ||
function parse(str) { | ||
if (isCidr(str)) { | ||
return new IPCIDR(module.exports.normalize(str)); | ||
return new IPCIDR(normalize(str)); | ||
} else { | ||
const version = isIp.version(str); | ||
const version = isIP(str); | ||
if (version) { | ||
return new IPCIDR(module.exports.normalize(`${str}/${bits[`v${version}`]}`)); | ||
return new IPCIDR(normalize(`${str}/${bits[`v${version}`]}`)); | ||
} else { | ||
@@ -63,3 +72,3 @@ throw new Error(`Network is not a CIDR or IP: ${str}`); | ||
if (!(number instanceof BigInteger)) number = bigint(number); | ||
return module.exports.normalize(cls.fromBigInteger(number).address); | ||
return normalize(cls.fromBigInteger(number).address); | ||
} | ||
@@ -96,3 +105,3 @@ | ||
// returns whether network a fully contains network b; | ||
function contains(a, b) { | ||
function netContains(a, b) { | ||
const {aStart, bStart, aEnd, bEnd} = getBoundaries(a, b); | ||
@@ -170,3 +179,3 @@ | ||
return module.exports.merge(remaining); | ||
return merge(remaining); | ||
} | ||
@@ -274,3 +283,3 @@ | ||
module.exports.merge = function(nets) { | ||
export function merge(nets) { | ||
nets = uniq((Array.isArray(nets) ? nets : [nets]).map(parse)); | ||
@@ -317,10 +326,10 @@ const maps = mapNets(nets); | ||
return merged.v4.concat(merged.v6); | ||
}; | ||
} | ||
module.exports.exclude = (basenets, exclnets) => { | ||
export function exclude(basenets, exclnets) { | ||
basenets = uniq(Array.isArray(basenets) ? basenets : [basenets]); | ||
exclnets = uniq(Array.isArray(exclnets) ? exclnets : [exclnets]); | ||
basenets = module.exports.merge(basenets); | ||
exclnets = module.exports.merge(exclnets); | ||
basenets = merge(basenets); | ||
exclnets = merge(exclnets); | ||
@@ -353,15 +362,15 @@ const bases = {v4: [], v6: []}; | ||
return bases.v4.concat(bases.v6); | ||
}; | ||
} | ||
module.exports.expand = (nets) => { | ||
export function expand(nets) { | ||
nets = uniq(Array.isArray(nets) ? nets : [nets]); | ||
let ips = []; | ||
for (const net of module.exports.merge(nets)) { | ||
for (const net of merge(nets)) { | ||
ips = ips.concat((new IPCIDR(net)).toArray()); | ||
} | ||
return ips.map(module.exports.normalize); | ||
}; | ||
return ips.map(normalize); | ||
} | ||
module.exports.overlap = (a, b) => { | ||
export function overlap(a, b) { | ||
const aNets = uniq(Array.isArray(a) ? a : [a]); | ||
@@ -387,5 +396,5 @@ const bNets = uniq(Array.isArray(b) ? b : [b]); | ||
return false; | ||
}; | ||
} | ||
module.exports.contains = (a, b) => { | ||
export function contains(a, b) { | ||
const aNets = uniq(Array.isArray(a) ? a : [a]); | ||
@@ -406,3 +415,3 @@ const bNets = uniq(Array.isArray(b) ? b : [b]); | ||
if (contains(aParsed, bParsed)) { | ||
if (netContains(aParsed, bParsed)) { | ||
numFound++; | ||
@@ -415,2 +424,2 @@ continue; | ||
return numFound === numExpected; | ||
}; | ||
} |
{ | ||
"name": "cidr-tools", | ||
"version": "4.3.0", | ||
"version": "5.0.0", | ||
"author": "silverwind <me@silverwind.io>", | ||
@@ -8,4 +8,5 @@ "description": "Tools to work with IPv4 and IPv6 CIDR network lists", | ||
"license": "BSD-2-Clause", | ||
"type": "module", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=14" | ||
}, | ||
@@ -30,7 +31,7 @@ "keywords": [ | ||
"dependencies": { | ||
"ip-address": "^8.1.0", | ||
"ip-cidr": "^3.0.4", | ||
"@silverwind/ip-address": "8.1.0", | ||
"ip-cidr": "^3.0.10", | ||
"ip-regex": "5.0.0", | ||
"ipv6-normalize": "^1.0.1", | ||
"is-cidr": "^4.0.2", | ||
"is-ip": "^3.1.0", | ||
"jsbn": "^1.1.0", | ||
@@ -40,8 +41,8 @@ "string-natural-compare": "^3.0.1" | ||
"devDependencies": { | ||
"eslint": "8.9.0", | ||
"eslint-config-silverwind": "48.1.0", | ||
"jest": "27.5.1", | ||
"updates": "13.0.0", | ||
"versions": "9.2.1" | ||
"eslint": "8.23.1", | ||
"eslint-config-silverwind": "54.0.2", | ||
"jest": "29.0.3", | ||
"updates": "13.1.5", | ||
"versions": "9.3.0" | ||
} | ||
} |
@@ -14,10 +14,10 @@ # cidr-tools | ||
```js | ||
const cidrTools = require('cidr-tools'); | ||
import {merge, exclude, expand, overlap, contains, normalize} from 'cidr-tools'; | ||
cidrTools.merge(['1.0.0.0/24', '1.0.1.0/24']); //=> ['1.0.0.0/23'] | ||
cidrTools.exclude(['::1/127'], ['::1/128']) //=> ['::/128'] | ||
cidrTools.expand(['2001:db8::/126']) //=> ['2001:db8::', '2001:db8::1', '2001:db8::2', '2001:db8::3'] | ||
cidrTools.overlap('1.0.0.0/24', '1.0.0.128/25') //=> true | ||
cidrTools.contains(["1.0.0.0/24", "2.0.0.0/24"], "1.0.0.1") //=> true | ||
cidrTools.normalize('0:0:0:0:0:0:0:0/0') //=> '::/0' | ||
merge(['1.0.0.0/24', '1.0.1.0/24']); //=> ['1.0.0.0/23'] | ||
exclude(['::1/127'], ['::1/128']) //=> ['::/128'] | ||
expand(['2001:db8::/126']) //=> ['2001:db8::', '2001:db8::1', '2001:db8::2', '2001:db8::3'] | ||
overlap('1.0.0.0/24', '1.0.0.128/25') //=> true | ||
contains(["1.0.0.0/24", "2.0.0.0/24"], "1.0.0.1") //=> true | ||
normalize('0:0:0:0:0:0:0:0/0') //=> '::/0' | ||
``` | ||
@@ -29,3 +29,3 @@ | ||
### cidrTools.merge(networks) | ||
### merge(networks) | ||
@@ -36,3 +36,3 @@ - `networks` *String* or *Array*: One or more CIDR or IP addresses. | ||
### cidrTools.exclude(baseNetworks, excludeNetworks) | ||
### exclude(baseNetworks, excludeNetworks) | ||
@@ -44,3 +44,3 @@ - `baseNetworks` *String* or *Array*: One or more CIDR or IP addresses. | ||
### cidrTools.expand(networks) | ||
### expand(networks) | ||
@@ -51,3 +51,3 @@ - `networks` *String* or *Array*: One or more CIDR or IP addresses. | ||
### cidrTools.overlap(networksA, networksB) | ||
### overlap(networksA, networksB) | ||
@@ -59,3 +59,3 @@ - `networksA` *String* or *Array*: One or more CIDR or IP address. | ||
### cidrTools.contains(networksA, networksB) | ||
### contains(networksA, networksB) | ||
@@ -67,3 +67,3 @@ - `networksA` *String* or *Array*: One or more CIDR or IP address. | ||
### cidrTools.normalize(networks) | ||
### normalize(networks) | ||
@@ -70,0 +70,0 @@ - `networks` *String* or *Array*: One or more CIDR or IP address. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15876
357
Yes
+ Added@silverwind/ip-address@8.1.0
+ Addedip-regex@5.0.0
+ Added@silverwind/ip-address@8.1.0(transitive)
+ Addedip-regex@5.0.0(transitive)
- Removedip-address@^8.1.0
- Removedis-ip@^3.1.0
- Removedip-address@8.1.0(transitive)
- Removedis-ip@3.1.0(transitive)
Updatedip-cidr@^3.0.10