cidr-tools
Advanced tools
Comparing version 7.0.5 to 7.0.6
@@ -5,3 +5,2 @@ type IPv4Address = string; | ||
type IPv6CIDR = string; | ||
type Network = IPv4Address | IPv4CIDR | IPv6Address | IPv6CIDR; | ||
@@ -16,3 +15,2 @@ type Networks = Network | Network[]; | ||
end: bigint; | ||
single: boolean; | ||
}; | ||
@@ -19,0 +17,0 @@ |
80
index.js
@@ -19,4 +19,4 @@ import {parseIp, stringifyIp, normalizeIp, ipVersion} from "ip-bigint"; | ||
function doNormalize(cidr, {compress = true, hexify = false} = {}) { | ||
const {start, prefix, single, version} = parse(cidr); | ||
if (!single) { // cidr | ||
const {start, end, prefix, version} = parse(cidr); | ||
if (start !== end) { // cidr | ||
// set network address to first address | ||
@@ -56,4 +56,8 @@ const ip = normalizeIp(stringifyIp({number: start, version}), {compress, hexify}); | ||
const [ip, prefix] = parsed.cidr.split("/"); | ||
if (!/^[0-9]+$/.test(prefix)) { | ||
throw new Error(`Network is not a CIDR or IP: ${str}`); | ||
} | ||
parsed.prefix = prefix; | ||
parsed.single = prefix === String(bits[parsed.version]); | ||
const {number, version} = parseIp(ip); | ||
@@ -231,3 +235,3 @@ const numBits = bits[version]; | ||
function mapNets(nets) { | ||
const maps = {4: {}, 6: {}}; | ||
const maps = {4: {}, 6: {}}; // TODO: use Map with BigInt key | ||
for (const {start, end, version} of nets) { | ||
@@ -252,40 +256,48 @@ if (!maps[version][start]) maps[version][start] = {}; | ||
export function merge(nets) { | ||
// sort to workaround https://github.com/silverwind/cidr-tools/issues/17 | ||
nets = uniq((Array.isArray(nets) ? nets : [nets]).sort(compare).map(parse)); | ||
const maps = mapNets(nets); | ||
function doMerge(maps, v) { | ||
let start = null; | ||
let end = null; | ||
const numbers = Object.keys(maps); | ||
let depth = 0; | ||
const merged = []; | ||
const merged = {4: [], 6: []}; | ||
const start = {4: null, 6: null}; | ||
const end = {4: null, 6: null}; | ||
for (const [index, number] of numbers.entries()) { | ||
const marker = maps[number]; | ||
for (const v of [4, 6]) { | ||
const numbers = Object.keys(maps[v]); | ||
let depth = 0; | ||
if (start === null && marker.start) { | ||
start = BigInt(number); | ||
} | ||
if (marker.end) { | ||
end = BigInt(number); | ||
} | ||
for (const [index, number] of numbers.entries()) { | ||
const marker = maps[v][number]; | ||
if (marker.start) depth += marker.start; | ||
if (marker.end) depth -= marker.end; | ||
if (start[v] === null && marker.start) { | ||
start[v] = BigInt(number); | ||
const next = numbers[index + 1]; | ||
if (marker.end && depth === 0 && next && ((BigInt(next) - BigInt(number)) > 1)) { | ||
// when there is a end and the next part is more than one number away, we cut a part | ||
for (const sub of subparts({start, end})) { | ||
merged.push(formatPart(sub, v)); | ||
} | ||
if (marker.end) { | ||
end[v] = BigInt(number); | ||
start = null; | ||
end = null; | ||
} else if (index === (numbers.length - 1)) { | ||
// cut the final part | ||
for (const sub of subparts({start, end})) { | ||
merged.push(formatPart(sub, v)); | ||
} | ||
} | ||
} | ||
return merged; | ||
} | ||
if (marker.start) depth += marker.start; | ||
if (marker.end) depth -= marker.end; | ||
export function merge(nets) { | ||
// sort to workaround https://github.com/silverwind/cidr-tools/issues/17 | ||
nets = uniq((Array.isArray(nets) ? nets : [nets]).sort(compare).map(parse)); | ||
const maps = mapNets(nets); | ||
if (marker.end && depth === 0 && ((numbers[index + 1] - numbers[index]) > 1)) { | ||
for (const sub of subparts({start: start[v], end: end[v]})) { | ||
merged[v].push(formatPart(sub, v)); | ||
} | ||
start[v] = null; | ||
end[v] = null; | ||
} else if (index === (numbers.length - 1)) { | ||
for (const sub of subparts({start: start[v], end: end[v]})) { | ||
merged[v].push(formatPart(sub, v)); | ||
} | ||
} | ||
} | ||
const merged = {4: [], 6: []}; | ||
for (const v of [4, 6]) { | ||
merged[v] = doMerge(maps[v], v); | ||
} | ||
@@ -292,0 +304,0 @@ |
{ | ||
"name": "cidr-tools", | ||
"version": "7.0.5", | ||
"version": "7.0.6", | ||
"author": "silverwind <me@silverwind.io>", | ||
@@ -26,8 +26,8 @@ "description": "Tools to work with IPv4 and IPv6 CIDR", | ||
"eslint-config-silverwind": "80.0.3", | ||
"tsd": "0.30.4", | ||
"tsd": "0.30.5", | ||
"updates": "15.1.2", | ||
"versions": "12.0.1", | ||
"vitest": "1.2.2", | ||
"vitest": "1.3.1", | ||
"vitest-config-silverwind": "5.1.1" | ||
} | ||
} |
@@ -80,5 +80,4 @@ # cidr-tools | ||
- `prefix` String: The network prefix, e.g. `/64`. | ||
- `start` BigInt: Start of the network. | ||
- `end` BigInt: Start of the network. | ||
- `single` Boolean: `true` when the network is a single IP. | ||
- `start` BigInt: Start number of the network. | ||
- `end` BigInt: End number of the network. | ||
@@ -85,0 +84,0 @@ ## Related |
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
16401
374
92