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