cidr-tools 7.0.3 → 7.0.5
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.
- package/README.md +3 -3
- package/index.js +14 -22
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -71,15 +71,15 @@ Returns a string or array (depending on input) with a normalized representation.
|
|
|
71
71
|
|
|
72
72
|
- `network` *String*: A CIDR or IP address.
|
|
73
73
|
|
|
74
|
-
Returns a `parsed` Object which is used internally by this module. It can be used to test whether the passed network is IPv4 or IPv6 or work with the BigInts directly.
|
|
74
|
+
Returns a `parsed` Object which is used internally by this module. It can be used to test whether the passed network is IPv4 or IPv6 or to work with the BigInts directly.
|
|
75
75
|
|
|
76
76
|
`parsed`: `Object`
|
|
77
77
|
- `cidr` String: The CIDR of the network.
|
|
78
|
-
- `version` Number: Either `4` or `6`.
|
|
78
|
+
- `version` Number: IP protocol version. Either `4` or `6`.
|
|
79
79
|
- `prefix` String: The network prefix, e.g. `/64`.
|
|
80
80
|
- `start` BigInt: Start of the network.
|
|
81
81
|
- `end` BigInt: Start of the network.
|
|
82
|
-
- `single` Boolean: `true` when is a single IP.
|
|
82
|
+
- `single` Boolean: `true` when the network is a single IP.
|
|
83
83
|
|
|
84
84
|
## Related
|
|
85
85
|
|
package/index.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import {parseIp, stringifyIp, normalizeIp, ipVersion} from "ip-bigint";
|
|
2
2
|
|
|
3
|
-
const bits = {
|
|
4
|
-
4: 32,
|
|
5
|
-
6: 128,
|
|
6
|
-
};
|
|
7
|
-
|
|
3
|
+
const bits = {4: 32, 6: 128};
|
|
8
4
|
const uniq = arr => Array.from(new Set(arr));
|
|
9
5
|
const cidrVersion = cidr => cidr.includes("/") ? ipVersion(cidr) : 0;
|
|
10
6
|
|
|
@@ -41,7 +37,6 @@ export function normalize(cidr, {compress = true, hexify = false} = {}) {
|
|
|
41
37
|
export function parse(str) {
|
|
42
38
|
const cidrVer = cidrVersion(str);
|
|
43
39
|
const parsed = Object.create(null);
|
|
44
|
-
parsed.single = false;
|
|
45
40
|
|
|
46
41
|
if (cidrVer) {
|
|
47
42
|
parsed.cidr = str;
|
|
@@ -51,7 +46,6 @@ export function parse(str) {
|
|
|
51
46
|
if (version) {
|
|
52
47
|
parsed.cidr = `${str}/${bits[version]}`;
|
|
53
48
|
parsed.version = version;
|
|
54
|
-
parsed.single = true;
|
|
55
49
|
} else {
|
|
56
50
|
throw new Error(`Network is not a CIDR or IP: ${str}`);
|
|
57
51
|
}
|
|
@@ -59,6 +53,7 @@ export function parse(str) {
|
|
|
59
53
|
|
|
60
54
|
const [ip, prefix] = parsed.cidr.split("/");
|
|
61
55
|
parsed.prefix = prefix;
|
|
56
|
+
parsed.single = prefix === String(bits[parsed.version]);
|
|
62
57
|
const {number, version} = parseIp(ip);
|
|
63
58
|
const numBits = bits[version];
|
|
64
59
|
const ipBits = number.toString(2).padStart(numBits, "0");
|
|
@@ -223,32 +218,29 @@ function diff(a, b) {
|
|
|
223
218
|
return a - b;
|
|
224
219
|
}
|
|
225
220
|
|
|
226
|
-
function formatPart(part,
|
|
227
|
-
const ip = normalize(stringifyIp({
|
|
228
|
-
number: BigInt(part.start.toString()),
|
|
229
|
-
version: v,
|
|
230
|
-
}));
|
|
221
|
+
function formatPart(part, version) {
|
|
222
|
+
const ip = normalize(stringifyIp({number: BigInt(part.start.toString()), version}));
|
|
231
223
|
const zeroes = diff(part.end, part.start).toString(2);
|
|
232
|
-
const prefix = bits[
|
|
224
|
+
const prefix = bits[version] - (zeroes.match(/0/g) || []).length;
|
|
233
225
|
return `${ip}/${prefix}`;
|
|
234
226
|
}
|
|
235
227
|
|
|
236
228
|
function mapNets(nets) {
|
|
237
229
|
const maps = {4: {}, 6: {}};
|
|
238
|
-
for (const {start, end, version
|
|
239
|
-
if (!maps[
|
|
240
|
-
if (!maps[
|
|
230
|
+
for (const {start, end, version} of nets) {
|
|
231
|
+
if (!maps[version][start]) maps[version][start] = {};
|
|
232
|
+
if (!maps[version][end]) maps[version][end] = {};
|
|
241
233
|
|
|
242
|
-
if (maps[
|
|
243
|
-
maps[
|
|
234
|
+
if (maps[version][start].start) {
|
|
235
|
+
maps[version][start].start += 1;
|
|
244
236
|
} else {
|
|
245
|
-
maps[
|
|
237
|
+
maps[version][start].start = 1;
|
|
246
238
|
}
|
|
247
239
|
|
|
248
|
-
if (maps[
|
|
249
|
-
maps[
|
|
240
|
+
if (maps[version][end].end) {
|
|
241
|
+
maps[version][end].end += 1;
|
|
250
242
|
} else {
|
|
251
|
-
maps[
|
|
243
|
+
maps[version][end].end = 1;
|
|
252
244
|
}
|
|
253
245
|
}
|
|
254
246
|
return maps;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cidr-tools",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.5",
|
|
4
4
|
"author": "silverwind <me@silverwind.io>",
|
|
5
5
|
"description": "Tools to work with IPv4 and IPv6 CIDR",
|
|
6
6
|
"repository": "silverwind/cidr-tools",
|
|
@@ -18,15 +18,15 @@
|
|
|
18
18
|
"./index.d.ts"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"ip-bigint": "8.0.
|
|
21
|
+
"ip-bigint": "^8.0.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"eslint": "8.
|
|
25
|
-
"eslint-config-silverwind": "
|
|
26
|
-
"tsd": "0.
|
|
27
|
-
"updates": "15.
|
|
28
|
-
"versions": "12.0.
|
|
29
|
-
"vitest": "
|
|
30
|
-
"vitest-config-silverwind": "
|
|
24
|
+
"eslint": "8.56.0",
|
|
25
|
+
"eslint-config-silverwind": "80.0.3",
|
|
26
|
+
"tsd": "0.30.4",
|
|
27
|
+
"updates": "15.1.2",
|
|
28
|
+
"versions": "12.0.1",
|
|
29
|
+
"vitest": "1.2.2",
|
|
30
|
+
"vitest-config-silverwind": "5.1.1"
|
|
31
31
|
}
|
|
32
32
|
}
|