ip-utilties 1.1.1 → 1.3.0
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/package.json +3 -2
- package/src/ip.mjs +19 -7
- package/types/ip.d.mts +7 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ip-utilties",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"provenance": true
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"ipv4",
|
|
17
17
|
"ipv6",
|
|
18
18
|
"network",
|
|
19
|
-
"subnet"
|
|
19
|
+
"subnet",
|
|
20
|
+
"cidr"
|
|
20
21
|
],
|
|
21
22
|
"contributors": [
|
|
22
23
|
{
|
package/src/ip.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const ipv4 = {
|
|
2
|
+
name: "IPv4",
|
|
2
3
|
factory: Uint8Array,
|
|
3
4
|
normalize(address) {
|
|
4
5
|
return address;
|
|
@@ -12,6 +13,7 @@ const ipv4 = {
|
|
|
12
13
|
};
|
|
13
14
|
|
|
14
15
|
const ipv6 = {
|
|
16
|
+
name: "IPv6",
|
|
15
17
|
factory: Uint16Array,
|
|
16
18
|
normalize(address) {
|
|
17
19
|
const parts = address.split(":");
|
|
@@ -49,9 +51,7 @@ function _encode(family, address) {
|
|
|
49
51
|
const res = new family.factory(family.segments);
|
|
50
52
|
|
|
51
53
|
let i = 0;
|
|
52
|
-
for (const segment of family
|
|
53
|
-
.normalize(address)
|
|
54
|
-
.split(family.separator)) {
|
|
54
|
+
for (const segment of family.normalize(address).split(family.separator)) {
|
|
55
55
|
res[i++] = parseInt(segment, family.base);
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -140,6 +140,15 @@ export function isIPv6(address) {
|
|
|
140
140
|
return _is(ipv6, address);
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
/**
|
|
144
|
+
* IP address family for a given address.
|
|
145
|
+
* @param {string|Uint8Array|Uint16Array} address
|
|
146
|
+
* @return {string|undefined}
|
|
147
|
+
*/
|
|
148
|
+
export function familyIP(address) {
|
|
149
|
+
return _family(address)?.name;
|
|
150
|
+
}
|
|
151
|
+
|
|
143
152
|
function _family(address) {
|
|
144
153
|
return [ipv4, ipv6].find(d => _is(d, address));
|
|
145
154
|
}
|
|
@@ -151,8 +160,7 @@ function _is(family, address) {
|
|
|
151
160
|
|
|
152
161
|
case "object":
|
|
153
162
|
return (
|
|
154
|
-
address instanceof family.factory &&
|
|
155
|
-
address.length === family.segments
|
|
163
|
+
address instanceof family.factory && address.length === family.segments
|
|
156
164
|
);
|
|
157
165
|
}
|
|
158
166
|
|
|
@@ -192,8 +200,7 @@ export function prefixIP(address, length) {
|
|
|
192
200
|
|
|
193
201
|
function _prefix(family, address, length) {
|
|
194
202
|
return (
|
|
195
|
-
_asBigInt(family, address) &
|
|
196
|
-
(-1n << BigInt(family.bitLength - length))
|
|
203
|
+
_asBigInt(family, address) & (-1n << BigInt(family.bitLength - length))
|
|
197
204
|
);
|
|
198
205
|
}
|
|
199
206
|
|
|
@@ -207,6 +214,11 @@ export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
|
|
|
207
214
|
];
|
|
208
215
|
}
|
|
209
216
|
|
|
217
|
+
export function matchPrefixIP(prefix, length, address) {
|
|
218
|
+
const family = _family(address);
|
|
219
|
+
return _prefix(family, address, length) === _prefix(family, prefix, length);
|
|
220
|
+
}
|
|
221
|
+
|
|
210
222
|
export function normalizeCIDR(address) {
|
|
211
223
|
let [prefix, prefixLength] = address.split(/\//);
|
|
212
224
|
let longPrefix;
|
package/types/ip.d.mts
CHANGED
|
@@ -6,9 +6,16 @@ export function decodeIPv4(address: any, length: any): string;
|
|
|
6
6
|
export function decodeIP(address: any, length: any): string;
|
|
7
7
|
export function isIPv4(address: any): boolean;
|
|
8
8
|
export function isIPv6(address: any): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* IP address family for a given address.
|
|
11
|
+
* @param {string|Uint8Array|Uint16Array} address
|
|
12
|
+
* @return {string|undefined}
|
|
13
|
+
*/
|
|
14
|
+
export function familyIP(address: string | Uint8Array | Uint16Array): string | undefined;
|
|
9
15
|
export function asBigInt(address: any): any;
|
|
10
16
|
export function prefixIP(address: any, length: any): string;
|
|
11
17
|
export function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
|
|
18
|
+
export function matchPrefixIP(prefix: any, length: any, address: any): boolean;
|
|
12
19
|
export function normalizeCIDR(address: any): {
|
|
13
20
|
longPrefix?: undefined;
|
|
14
21
|
prefix?: undefined;
|