@se-oss/ip-address 1.1.1 → 1.1.2
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/dist/index.cjs +70 -49
- package/dist/index.js +70 -49
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -59,7 +59,15 @@ var IPV6_COMPRESSED_7 = `(?:${IPV6_SEGMENT}:){1,2}(?::${IPV6_SEGMENT}){1,5}`;
|
|
|
59
59
|
var IPV6_COMPRESSED_8 = `${IPV6_SEGMENT}:(?::${IPV6_SEGMENT}){1,6}`;
|
|
60
60
|
var IPV6_COMPRESSED_9 = `::(?:${IPV6_SEGMENT}:){0,5}${IPV6_SEGMENT}`;
|
|
61
61
|
var IPV6_COMPRESSED_10 = "::";
|
|
62
|
-
var IPV6_IPV4_MAPPED =
|
|
62
|
+
var IPV6_IPV4_MAPPED = [
|
|
63
|
+
`(?:${IPV6_SEGMENT}:){6}`,
|
|
64
|
+
`::(?:${IPV6_SEGMENT}:){0,5}`,
|
|
65
|
+
`(?:${IPV6_SEGMENT}:){1}:(?:${IPV6_SEGMENT}:){0,4}`,
|
|
66
|
+
`(?:${IPV6_SEGMENT}:){2}:(?:${IPV6_SEGMENT}:){0,3}`,
|
|
67
|
+
`(?:${IPV6_SEGMENT}:){3}:(?:${IPV6_SEGMENT}:){0,2}`,
|
|
68
|
+
`(?:${IPV6_SEGMENT}:){4}:(?:${IPV6_SEGMENT}:){0,1}`,
|
|
69
|
+
`(?:${IPV6_SEGMENT}:){5}:`
|
|
70
|
+
].map((prefix) => `${prefix}${IPV4_ADDRESS_PATTERN}`).join("|");
|
|
63
71
|
var IPV6_COMBINED = [
|
|
64
72
|
IPV6_STANDARD,
|
|
65
73
|
IPV6_COMPRESSED_1,
|
|
@@ -86,19 +94,19 @@ var InvalidIPAddressError = class extends Error {
|
|
|
86
94
|
|
|
87
95
|
// src/utils.ts
|
|
88
96
|
function fastIpVersion(probablyIp) {
|
|
89
|
-
if (probablyIp.includes(".")) {
|
|
90
|
-
return 4;
|
|
91
|
-
}
|
|
92
97
|
if (probablyIp.includes(":")) {
|
|
93
98
|
return 6;
|
|
94
99
|
}
|
|
100
|
+
if (probablyIp.includes(".")) {
|
|
101
|
+
return 4;
|
|
102
|
+
}
|
|
95
103
|
throw new InvalidIPAddressError(`Invalid IP address: ${probablyIp}`);
|
|
96
104
|
}
|
|
97
105
|
function ipv4ToBigInt(ip) {
|
|
98
|
-
|
|
99
|
-
if (parts.length !== 4 || parts.some((part) => !/^\d+$/.test(part) || Number(part) > 255)) {
|
|
106
|
+
if (!IPV4_ADDRESS_REGEX.test(ip)) {
|
|
100
107
|
throw new InvalidIPAddressError(`Invalid IPv4 address: ${ip}`);
|
|
101
108
|
}
|
|
109
|
+
const parts = ip.split(".");
|
|
102
110
|
return parts.reduce((acc, octet) => acc << 8n | BigInt(parseInt(octet, 10)), 0n);
|
|
103
111
|
}
|
|
104
112
|
function expandIPv6Shorthand(parts) {
|
|
@@ -110,26 +118,28 @@ function expandIPv6Shorthand(parts) {
|
|
|
110
118
|
return parts;
|
|
111
119
|
}
|
|
112
120
|
function ipv6ToBigInt(ip) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
let parts = ip.split(":");
|
|
123
|
-
parts = expandIPv6Shorthand(parts);
|
|
124
|
-
if (parts.length !== 8 || parts.some((part) => !/^[0-9a-fA-F]*$/.test(part) || part.length > 4)) {
|
|
121
|
+
const percentIndex = ip.indexOf("%");
|
|
122
|
+
if (percentIndex !== -1) {
|
|
123
|
+
if (percentIndex === 0 || percentIndex === ip.length - 1) {
|
|
124
|
+
throw new InvalidIPAddressError(`Invalid IPv6 address: ${ip}`);
|
|
125
|
+
}
|
|
126
|
+
ip = ip.slice(0, percentIndex);
|
|
127
|
+
}
|
|
128
|
+
if (!IPV6_VALIDATION_REGEX.test(ip)) {
|
|
125
129
|
throw new InvalidIPAddressError(`Invalid IPv6 address: ${ip}`);
|
|
126
130
|
}
|
|
131
|
+
if (ip.includes(".")) {
|
|
132
|
+
const lastColon = ip.lastIndexOf(":");
|
|
133
|
+
const prefix = ip.slice(0, lastColon + 1);
|
|
134
|
+
const ipv4BigInt = ipv4ToBigInt(ip.slice(lastColon + 1));
|
|
135
|
+
const high = (ipv4BigInt >> 16n & 0xffffn).toString(16);
|
|
136
|
+
const low = (ipv4BigInt & 0xffffn).toString(16);
|
|
137
|
+
ip = `${prefix}${high}:${low}`;
|
|
138
|
+
}
|
|
139
|
+
const parts = expandIPv6Shorthand(ip.split(":"));
|
|
127
140
|
let number = 0n;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
for (const n of r) {
|
|
131
|
-
number += n * 2n ** exp;
|
|
132
|
-
exp += 16n;
|
|
141
|
+
for (const part of parts) {
|
|
142
|
+
number = number << 16n | (part === "" ? 0n : BigInt(Number.parseInt(part, 16)));
|
|
133
143
|
}
|
|
134
144
|
return number;
|
|
135
145
|
}
|
|
@@ -144,7 +154,7 @@ function bigIntToIp(number, version, compress = true) {
|
|
|
144
154
|
if (version === 4) {
|
|
145
155
|
return `${number >> 24n & 0xffn}.${number >> 16n & 0xffn}.${number >> 8n & 0xffn}.${number & 0xffn}`;
|
|
146
156
|
}
|
|
147
|
-
if (number >> 32n === 0xffffn) {
|
|
157
|
+
if (compress && number >> 32n === 0xffffn) {
|
|
148
158
|
const ipv4Part = number & 0xffffffffn;
|
|
149
159
|
const ipv4String = bigIntToIp(ipv4Part, 4);
|
|
150
160
|
return `::ffff:${ipv4String}`;
|
|
@@ -160,27 +170,33 @@ function bigIntToIp(number, version, compress = true) {
|
|
|
160
170
|
return to16.map((part) => part.padStart(4, "0")).join(":");
|
|
161
171
|
}
|
|
162
172
|
function compressIPv6(parts) {
|
|
163
|
-
let
|
|
164
|
-
let
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
let bestStart = -1;
|
|
174
|
+
let bestLen = 0;
|
|
175
|
+
let curStart = -1;
|
|
176
|
+
let curLen = 0;
|
|
177
|
+
for (let i = 0; i < parts.length; i++) {
|
|
178
|
+
if (parts[i] === "0") {
|
|
179
|
+
if (curStart === -1) {
|
|
180
|
+
curStart = i;
|
|
181
|
+
curLen = 1;
|
|
182
|
+
} else {
|
|
183
|
+
curLen++;
|
|
184
|
+
}
|
|
185
|
+
if (curLen > bestLen) {
|
|
186
|
+
bestStart = curStart;
|
|
187
|
+
bestLen = curLen;
|
|
173
188
|
}
|
|
174
|
-
|
|
189
|
+
} else {
|
|
190
|
+
curStart = -1;
|
|
191
|
+
curLen = 0;
|
|
175
192
|
}
|
|
176
193
|
}
|
|
177
|
-
if (
|
|
178
|
-
|
|
194
|
+
if (bestLen < 2) {
|
|
195
|
+
return parts.join(":");
|
|
179
196
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
return parts.filter(Boolean).join(":").replace(/:{2,}/, "::");
|
|
197
|
+
const before = parts.slice(0, bestStart).join(":");
|
|
198
|
+
const after = parts.slice(bestStart + bestLen).join(":");
|
|
199
|
+
return `${before}::${after}`;
|
|
184
200
|
}
|
|
185
201
|
|
|
186
202
|
// src/ipv6.ts
|
|
@@ -720,24 +736,29 @@ var CIDR = class {
|
|
|
720
736
|
*/
|
|
721
737
|
constructor(cidr) {
|
|
722
738
|
const parts = cidr.split("/");
|
|
739
|
+
if (parts.length > 2) {
|
|
740
|
+
throw new InvalidIPAddressError(`Invalid CIDR: ${cidr}`);
|
|
741
|
+
}
|
|
723
742
|
const addressPart = parts[0];
|
|
724
743
|
let prefixPart = parts[1];
|
|
725
744
|
let address;
|
|
726
745
|
let version;
|
|
727
|
-
if (addressPart.includes("
|
|
746
|
+
if (addressPart.includes(":")) {
|
|
747
|
+
address = new IPv6(addressPart);
|
|
748
|
+
version = 6;
|
|
749
|
+
} else if (addressPart.includes(".")) {
|
|
728
750
|
address = new IPv4(addressPart);
|
|
729
751
|
version = 4;
|
|
730
752
|
} else {
|
|
731
|
-
|
|
732
|
-
version = 6;
|
|
753
|
+
throw new InvalidIPAddressError(`Invalid IP address: ${addressPart}`);
|
|
733
754
|
}
|
|
734
755
|
if (prefixPart === void 0) {
|
|
735
756
|
prefixPart = String(BITS[version]);
|
|
736
757
|
}
|
|
737
|
-
|
|
738
|
-
if (isNaN(prefix)) {
|
|
758
|
+
if (!/^\d+$/.test(prefixPart)) {
|
|
739
759
|
throw new InvalidIPAddressError(`Invalid prefix: ${prefixPart}`);
|
|
740
760
|
}
|
|
761
|
+
const prefix = Number(prefixPart);
|
|
741
762
|
if (prefix < 0 || prefix > BITS[version]) {
|
|
742
763
|
throw new InvalidIPAddressError(`Prefix out of range for IPv${version}: ${prefix}`);
|
|
743
764
|
}
|
|
@@ -864,12 +885,12 @@ var CIDR = class {
|
|
|
864
885
|
|
|
865
886
|
// src/index.ts
|
|
866
887
|
function parseIP(address) {
|
|
867
|
-
if (address.includes(".")) {
|
|
868
|
-
return new IPv4(address);
|
|
869
|
-
}
|
|
870
888
|
if (address.includes(":")) {
|
|
871
889
|
return new IPv6(address);
|
|
872
890
|
}
|
|
891
|
+
if (address.includes(".")) {
|
|
892
|
+
return new IPv4(address);
|
|
893
|
+
}
|
|
873
894
|
throw new InvalidIPAddressError(`Invalid IP address: ${address}`);
|
|
874
895
|
}
|
|
875
896
|
function parseCIDR(cidr) {
|
package/dist/index.js
CHANGED
|
@@ -24,7 +24,15 @@ var IPV6_COMPRESSED_7 = `(?:${IPV6_SEGMENT}:){1,2}(?::${IPV6_SEGMENT}){1,5}`;
|
|
|
24
24
|
var IPV6_COMPRESSED_8 = `${IPV6_SEGMENT}:(?::${IPV6_SEGMENT}){1,6}`;
|
|
25
25
|
var IPV6_COMPRESSED_9 = `::(?:${IPV6_SEGMENT}:){0,5}${IPV6_SEGMENT}`;
|
|
26
26
|
var IPV6_COMPRESSED_10 = "::";
|
|
27
|
-
var IPV6_IPV4_MAPPED =
|
|
27
|
+
var IPV6_IPV4_MAPPED = [
|
|
28
|
+
`(?:${IPV6_SEGMENT}:){6}`,
|
|
29
|
+
`::(?:${IPV6_SEGMENT}:){0,5}`,
|
|
30
|
+
`(?:${IPV6_SEGMENT}:){1}:(?:${IPV6_SEGMENT}:){0,4}`,
|
|
31
|
+
`(?:${IPV6_SEGMENT}:){2}:(?:${IPV6_SEGMENT}:){0,3}`,
|
|
32
|
+
`(?:${IPV6_SEGMENT}:){3}:(?:${IPV6_SEGMENT}:){0,2}`,
|
|
33
|
+
`(?:${IPV6_SEGMENT}:){4}:(?:${IPV6_SEGMENT}:){0,1}`,
|
|
34
|
+
`(?:${IPV6_SEGMENT}:){5}:`
|
|
35
|
+
].map((prefix) => `${prefix}${IPV4_ADDRESS_PATTERN}`).join("|");
|
|
28
36
|
var IPV6_COMBINED = [
|
|
29
37
|
IPV6_STANDARD,
|
|
30
38
|
IPV6_COMPRESSED_1,
|
|
@@ -51,19 +59,19 @@ var InvalidIPAddressError = class extends Error {
|
|
|
51
59
|
|
|
52
60
|
// src/utils.ts
|
|
53
61
|
function fastIpVersion(probablyIp) {
|
|
54
|
-
if (probablyIp.includes(".")) {
|
|
55
|
-
return 4;
|
|
56
|
-
}
|
|
57
62
|
if (probablyIp.includes(":")) {
|
|
58
63
|
return 6;
|
|
59
64
|
}
|
|
65
|
+
if (probablyIp.includes(".")) {
|
|
66
|
+
return 4;
|
|
67
|
+
}
|
|
60
68
|
throw new InvalidIPAddressError(`Invalid IP address: ${probablyIp}`);
|
|
61
69
|
}
|
|
62
70
|
function ipv4ToBigInt(ip) {
|
|
63
|
-
|
|
64
|
-
if (parts.length !== 4 || parts.some((part) => !/^\d+$/.test(part) || Number(part) > 255)) {
|
|
71
|
+
if (!IPV4_ADDRESS_REGEX.test(ip)) {
|
|
65
72
|
throw new InvalidIPAddressError(`Invalid IPv4 address: ${ip}`);
|
|
66
73
|
}
|
|
74
|
+
const parts = ip.split(".");
|
|
67
75
|
return parts.reduce((acc, octet) => acc << 8n | BigInt(parseInt(octet, 10)), 0n);
|
|
68
76
|
}
|
|
69
77
|
function expandIPv6Shorthand(parts) {
|
|
@@ -75,26 +83,28 @@ function expandIPv6Shorthand(parts) {
|
|
|
75
83
|
return parts;
|
|
76
84
|
}
|
|
77
85
|
function ipv6ToBigInt(ip) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
let parts = ip.split(":");
|
|
88
|
-
parts = expandIPv6Shorthand(parts);
|
|
89
|
-
if (parts.length !== 8 || parts.some((part) => !/^[0-9a-fA-F]*$/.test(part) || part.length > 4)) {
|
|
86
|
+
const percentIndex = ip.indexOf("%");
|
|
87
|
+
if (percentIndex !== -1) {
|
|
88
|
+
if (percentIndex === 0 || percentIndex === ip.length - 1) {
|
|
89
|
+
throw new InvalidIPAddressError(`Invalid IPv6 address: ${ip}`);
|
|
90
|
+
}
|
|
91
|
+
ip = ip.slice(0, percentIndex);
|
|
92
|
+
}
|
|
93
|
+
if (!IPV6_VALIDATION_REGEX.test(ip)) {
|
|
90
94
|
throw new InvalidIPAddressError(`Invalid IPv6 address: ${ip}`);
|
|
91
95
|
}
|
|
96
|
+
if (ip.includes(".")) {
|
|
97
|
+
const lastColon = ip.lastIndexOf(":");
|
|
98
|
+
const prefix = ip.slice(0, lastColon + 1);
|
|
99
|
+
const ipv4BigInt = ipv4ToBigInt(ip.slice(lastColon + 1));
|
|
100
|
+
const high = (ipv4BigInt >> 16n & 0xffffn).toString(16);
|
|
101
|
+
const low = (ipv4BigInt & 0xffffn).toString(16);
|
|
102
|
+
ip = `${prefix}${high}:${low}`;
|
|
103
|
+
}
|
|
104
|
+
const parts = expandIPv6Shorthand(ip.split(":"));
|
|
92
105
|
let number = 0n;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
for (const n of r) {
|
|
96
|
-
number += n * 2n ** exp;
|
|
97
|
-
exp += 16n;
|
|
106
|
+
for (const part of parts) {
|
|
107
|
+
number = number << 16n | (part === "" ? 0n : BigInt(Number.parseInt(part, 16)));
|
|
98
108
|
}
|
|
99
109
|
return number;
|
|
100
110
|
}
|
|
@@ -109,7 +119,7 @@ function bigIntToIp(number, version, compress = true) {
|
|
|
109
119
|
if (version === 4) {
|
|
110
120
|
return `${number >> 24n & 0xffn}.${number >> 16n & 0xffn}.${number >> 8n & 0xffn}.${number & 0xffn}`;
|
|
111
121
|
}
|
|
112
|
-
if (number >> 32n === 0xffffn) {
|
|
122
|
+
if (compress && number >> 32n === 0xffffn) {
|
|
113
123
|
const ipv4Part = number & 0xffffffffn;
|
|
114
124
|
const ipv4String = bigIntToIp(ipv4Part, 4);
|
|
115
125
|
return `::ffff:${ipv4String}`;
|
|
@@ -125,27 +135,33 @@ function bigIntToIp(number, version, compress = true) {
|
|
|
125
135
|
return to16.map((part) => part.padStart(4, "0")).join(":");
|
|
126
136
|
}
|
|
127
137
|
function compressIPv6(parts) {
|
|
128
|
-
let
|
|
129
|
-
let
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
let bestStart = -1;
|
|
139
|
+
let bestLen = 0;
|
|
140
|
+
let curStart = -1;
|
|
141
|
+
let curLen = 0;
|
|
142
|
+
for (let i = 0; i < parts.length; i++) {
|
|
143
|
+
if (parts[i] === "0") {
|
|
144
|
+
if (curStart === -1) {
|
|
145
|
+
curStart = i;
|
|
146
|
+
curLen = 1;
|
|
147
|
+
} else {
|
|
148
|
+
curLen++;
|
|
149
|
+
}
|
|
150
|
+
if (curLen > bestLen) {
|
|
151
|
+
bestStart = curStart;
|
|
152
|
+
bestLen = curLen;
|
|
138
153
|
}
|
|
139
|
-
|
|
154
|
+
} else {
|
|
155
|
+
curStart = -1;
|
|
156
|
+
curLen = 0;
|
|
140
157
|
}
|
|
141
158
|
}
|
|
142
|
-
if (
|
|
143
|
-
|
|
159
|
+
if (bestLen < 2) {
|
|
160
|
+
return parts.join(":");
|
|
144
161
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
return parts.filter(Boolean).join(":").replace(/:{2,}/, "::");
|
|
162
|
+
const before = parts.slice(0, bestStart).join(":");
|
|
163
|
+
const after = parts.slice(bestStart + bestLen).join(":");
|
|
164
|
+
return `${before}::${after}`;
|
|
149
165
|
}
|
|
150
166
|
|
|
151
167
|
// src/ipv6.ts
|
|
@@ -685,24 +701,29 @@ var CIDR = class {
|
|
|
685
701
|
*/
|
|
686
702
|
constructor(cidr) {
|
|
687
703
|
const parts = cidr.split("/");
|
|
704
|
+
if (parts.length > 2) {
|
|
705
|
+
throw new InvalidIPAddressError(`Invalid CIDR: ${cidr}`);
|
|
706
|
+
}
|
|
688
707
|
const addressPart = parts[0];
|
|
689
708
|
let prefixPart = parts[1];
|
|
690
709
|
let address;
|
|
691
710
|
let version;
|
|
692
|
-
if (addressPart.includes("
|
|
711
|
+
if (addressPart.includes(":")) {
|
|
712
|
+
address = new IPv6(addressPart);
|
|
713
|
+
version = 6;
|
|
714
|
+
} else if (addressPart.includes(".")) {
|
|
693
715
|
address = new IPv4(addressPart);
|
|
694
716
|
version = 4;
|
|
695
717
|
} else {
|
|
696
|
-
|
|
697
|
-
version = 6;
|
|
718
|
+
throw new InvalidIPAddressError(`Invalid IP address: ${addressPart}`);
|
|
698
719
|
}
|
|
699
720
|
if (prefixPart === void 0) {
|
|
700
721
|
prefixPart = String(BITS[version]);
|
|
701
722
|
}
|
|
702
|
-
|
|
703
|
-
if (isNaN(prefix)) {
|
|
723
|
+
if (!/^\d+$/.test(prefixPart)) {
|
|
704
724
|
throw new InvalidIPAddressError(`Invalid prefix: ${prefixPart}`);
|
|
705
725
|
}
|
|
726
|
+
const prefix = Number(prefixPart);
|
|
706
727
|
if (prefix < 0 || prefix > BITS[version]) {
|
|
707
728
|
throw new InvalidIPAddressError(`Prefix out of range for IPv${version}: ${prefix}`);
|
|
708
729
|
}
|
|
@@ -829,12 +850,12 @@ var CIDR = class {
|
|
|
829
850
|
|
|
830
851
|
// src/index.ts
|
|
831
852
|
function parseIP(address) {
|
|
832
|
-
if (address.includes(".")) {
|
|
833
|
-
return new IPv4(address);
|
|
834
|
-
}
|
|
835
853
|
if (address.includes(":")) {
|
|
836
854
|
return new IPv6(address);
|
|
837
855
|
}
|
|
856
|
+
if (address.includes(".")) {
|
|
857
|
+
return new IPv4(address);
|
|
858
|
+
}
|
|
838
859
|
throw new InvalidIPAddressError(`Invalid IP address: ${address}`);
|
|
839
860
|
}
|
|
840
861
|
function parseCIDR(cidr) {
|
package/package.json
CHANGED