ip-utilties 1.1.0 → 1.2.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 +1 -1
- package/src/ip.mjs +58 -47
- package/types/ip.d.mts +6 -0
package/package.json
CHANGED
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(":");
|
|
@@ -31,7 +33,7 @@ const ipv6 = {
|
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
export function encodeIP(address) {
|
|
34
|
-
const d =
|
|
36
|
+
const d = _family(address);
|
|
35
37
|
return d && _encode(d, address);
|
|
36
38
|
}
|
|
37
39
|
|
|
@@ -43,43 +45,43 @@ export function encodeIPv4(address) {
|
|
|
43
45
|
return _encode(ipv4, address);
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
function _encode(
|
|
48
|
+
function _encode(family, address) {
|
|
47
49
|
switch (typeof address) {
|
|
48
50
|
case "string":
|
|
49
|
-
const res = new
|
|
51
|
+
const res = new family.factory(family.segments);
|
|
50
52
|
|
|
51
53
|
let i = 0;
|
|
52
|
-
for (const segment of
|
|
54
|
+
for (const segment of family
|
|
53
55
|
.normalize(address)
|
|
54
|
-
.split(
|
|
55
|
-
res[i++] = parseInt(segment,
|
|
56
|
+
.split(family.separator)) {
|
|
57
|
+
res[i++] = parseInt(segment, family.base);
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
return res;
|
|
59
61
|
|
|
60
62
|
case "bigint":
|
|
61
|
-
return _encodeBigInt(
|
|
63
|
+
return _encodeBigInt(family, address);
|
|
62
64
|
|
|
63
65
|
case "object":
|
|
64
66
|
if (
|
|
65
|
-
address instanceof
|
|
66
|
-
address.length ===
|
|
67
|
+
address instanceof family.factory &&
|
|
68
|
+
address.length === family.segments
|
|
67
69
|
) {
|
|
68
70
|
return address;
|
|
69
71
|
}
|
|
70
72
|
}
|
|
71
73
|
}
|
|
72
74
|
|
|
73
|
-
function _decode(
|
|
75
|
+
function _decode(family, address, length) {
|
|
74
76
|
switch (typeof address) {
|
|
75
77
|
case "string":
|
|
76
78
|
if (length === undefined) {
|
|
77
79
|
return address;
|
|
78
80
|
}
|
|
79
|
-
address = _encode(
|
|
81
|
+
address = _encode(family, address);
|
|
80
82
|
break;
|
|
81
83
|
case "bigint":
|
|
82
|
-
address = _encodeBigInt(
|
|
84
|
+
address = _encodeBigInt(family, address);
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
let result = "";
|
|
@@ -88,7 +90,7 @@ function _decode(definition, address, length) {
|
|
|
88
90
|
let last = address?.length;
|
|
89
91
|
|
|
90
92
|
if (length !== undefined) {
|
|
91
|
-
length /=
|
|
93
|
+
length /= family.segmentLength;
|
|
92
94
|
|
|
93
95
|
if (length < last) {
|
|
94
96
|
last = length;
|
|
@@ -98,22 +100,22 @@ function _decode(definition, address, length) {
|
|
|
98
100
|
for (; j < last; j++) {
|
|
99
101
|
word = address[j];
|
|
100
102
|
|
|
101
|
-
if (word !== 0 || !
|
|
103
|
+
if (word !== 0 || !family.compressor || compressed > 0) {
|
|
102
104
|
break;
|
|
103
105
|
}
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
if (j > i + 1) {
|
|
107
109
|
compressed++;
|
|
108
|
-
result +=
|
|
110
|
+
result += family.compressor;
|
|
109
111
|
} else {
|
|
110
112
|
if (result.length > 0) {
|
|
111
|
-
result +=
|
|
113
|
+
result += family.separator;
|
|
112
114
|
}
|
|
113
115
|
}
|
|
114
116
|
|
|
115
117
|
if (j < last) {
|
|
116
|
-
result += word.toString(
|
|
118
|
+
result += word.toString(family.base);
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
121
|
|
|
@@ -140,19 +142,28 @@ export function isIPv6(address) {
|
|
|
140
142
|
return _is(ipv6, address);
|
|
141
143
|
}
|
|
142
144
|
|
|
143
|
-
|
|
145
|
+
/**
|
|
146
|
+
* IP address family for a given address.
|
|
147
|
+
* @param {string|Uint8Array|Uint16Array} address
|
|
148
|
+
* @return {string|undefined}
|
|
149
|
+
*/
|
|
150
|
+
export function familyIP(address) {
|
|
151
|
+
return _family(address)?.name
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function _family(address) {
|
|
144
155
|
return [ipv4, ipv6].find(d => _is(d, address));
|
|
145
156
|
}
|
|
146
157
|
|
|
147
|
-
function _is(
|
|
158
|
+
function _is(family, address) {
|
|
148
159
|
switch (typeof address) {
|
|
149
160
|
case "string":
|
|
150
|
-
return address.indexOf(
|
|
161
|
+
return address.indexOf(family.separator) >= 0;
|
|
151
162
|
|
|
152
163
|
case "object":
|
|
153
164
|
return (
|
|
154
|
-
address instanceof
|
|
155
|
-
address.length ===
|
|
165
|
+
address instanceof family.factory &&
|
|
166
|
+
address.length === family.segments
|
|
156
167
|
);
|
|
157
168
|
}
|
|
158
169
|
|
|
@@ -160,50 +171,50 @@ function _is(definition, address) {
|
|
|
160
171
|
}
|
|
161
172
|
|
|
162
173
|
export function asBigInt(address) {
|
|
163
|
-
return _asBigInt(
|
|
174
|
+
return _asBigInt(_family(address), address);
|
|
164
175
|
}
|
|
165
176
|
|
|
166
|
-
function _asBigInt(
|
|
177
|
+
function _asBigInt(family, address) {
|
|
167
178
|
if (typeof address === "bigint") {
|
|
168
179
|
return address;
|
|
169
180
|
}
|
|
170
181
|
|
|
171
|
-
return _encode(
|
|
172
|
-
(a, c) => (a << BigInt(
|
|
182
|
+
return _encode(family, address).reduce(
|
|
183
|
+
(a, c) => (a << BigInt(family.segmentLength)) + BigInt(c),
|
|
173
184
|
0n
|
|
174
185
|
);
|
|
175
186
|
}
|
|
176
187
|
|
|
177
|
-
function _encodeBigInt(
|
|
188
|
+
function _encodeBigInt(family, address) {
|
|
178
189
|
const segments = [];
|
|
179
190
|
|
|
180
|
-
for (let i = 0; i <
|
|
181
|
-
segments.push(Number(address &
|
|
182
|
-
address >>= BigInt(
|
|
191
|
+
for (let i = 0; i < family.segments; i++) {
|
|
192
|
+
segments.push(Number(address & family.segmentMask));
|
|
193
|
+
address >>= BigInt(family.segmentLength);
|
|
183
194
|
}
|
|
184
195
|
|
|
185
|
-
return new
|
|
196
|
+
return new family.factory(segments.reverse());
|
|
186
197
|
}
|
|
187
198
|
|
|
188
199
|
export function prefixIP(address, length) {
|
|
189
|
-
const
|
|
190
|
-
return _decode(
|
|
200
|
+
const family = _family(address);
|
|
201
|
+
return _decode(family, _prefix(family, address, length));
|
|
191
202
|
}
|
|
192
203
|
|
|
193
|
-
function _prefix(
|
|
204
|
+
function _prefix(family, address, length) {
|
|
194
205
|
return (
|
|
195
|
-
_asBigInt(
|
|
196
|
-
(-1n << BigInt(
|
|
206
|
+
_asBigInt(family, address) &
|
|
207
|
+
(-1n << BigInt(family.bitLength - length))
|
|
197
208
|
);
|
|
198
209
|
}
|
|
199
210
|
|
|
200
211
|
export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
|
|
201
|
-
const
|
|
202
|
-
const from = _prefix(
|
|
203
|
-
const to = from | ((1n << BigInt(
|
|
212
|
+
const family = _family(address);
|
|
213
|
+
const from = _prefix(family, address, prefix);
|
|
214
|
+
const to = from | ((1n << BigInt(family.bitLength - prefix)) - 1n);
|
|
204
215
|
return [
|
|
205
|
-
_encode(
|
|
206
|
-
_encode(
|
|
216
|
+
_encode(family, from + BigInt(lowerAdd)),
|
|
217
|
+
_encode(family, to - BigInt(upperReduce))
|
|
207
218
|
];
|
|
208
219
|
}
|
|
209
220
|
|
|
@@ -218,22 +229,22 @@ export function normalizeCIDR(address) {
|
|
|
218
229
|
} else {
|
|
219
230
|
prefixLength = parseInt(prefixLength);
|
|
220
231
|
|
|
221
|
-
const
|
|
232
|
+
const family = isIPv6(prefix) ? ipv6 : ipv4;
|
|
222
233
|
let n;
|
|
223
234
|
|
|
224
235
|
if (prefixLength) {
|
|
225
|
-
n = _prefix(
|
|
236
|
+
n = _prefix(family, prefix, prefixLength);
|
|
226
237
|
} else {
|
|
227
|
-
n = _encode(
|
|
238
|
+
n = _encode(family, prefix);
|
|
228
239
|
|
|
229
240
|
if (isLocalhost(n)) {
|
|
230
|
-
prefixLength =
|
|
241
|
+
prefixLength = family === ipv6 ? 128 : 8;
|
|
231
242
|
} else {
|
|
232
243
|
return {};
|
|
233
244
|
}
|
|
234
245
|
}
|
|
235
|
-
prefix = _decode(
|
|
236
|
-
longPrefix = _decode(
|
|
246
|
+
prefix = _decode(family, n, prefixLength);
|
|
247
|
+
longPrefix = _decode(family, n);
|
|
237
248
|
}
|
|
238
249
|
|
|
239
250
|
return {
|
package/types/ip.d.mts
CHANGED
|
@@ -6,6 +6,12 @@ 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[];
|