@@ -56,7 +56,7 @@ function isIpv6Cidr(cidr: unknown): cidr is Ipv6Cidr {
56
56
function isTrustedProxy (
57
57
ip : string ,
58
58
segments : ReadonlyArray < number > ,
59
- proxies ?: ReadonlyArray < string | CidrBase > | null | undefined ,
59
+ proxies ?: ReadonlyArray < string | Cidr > | null | undefined ,
60
60
) {
61
61
if ( Array . isArray ( proxies ) && proxies . length > 0 ) {
62
62
return proxies . some ( ( proxy ) => {
@@ -76,92 +76,83 @@ function isTrustedProxy(
76
76
return false ;
77
77
}
78
78
79
- abstract class CidrBase {
80
- abstract type : "v4" | "v6" ;
81
- abstract partSize : 8 | 16 ;
82
- abstract parts : readonly number [ ] ;
83
- abstract bits : number ;
84
-
85
- // Based on CIDR matching implementation in `ipaddr.js`
86
- // Source code:
87
- // https://github.com/whitequark/ipaddr.js/blob/08c2cd41e2cb3400683cbd503f60421bfdf66921/lib/ipaddr.js#L107-L130
88
- //
89
- // Licensed: The MIT License (MIT)
90
- // Copyright (C) 2011-2017 whitequark <[email protected] >
91
- //
92
- // Permission is hereby granted, free of charge, to any person obtaining a copy
93
- // of this software and associated documentation files (the "Software"), to deal
94
- // in the Software without restriction, including without limitation the rights
95
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
96
- // copies of the Software, and to permit persons to whom the Software is
97
- // furnished to do so, subject to the following conditions:
98
-
99
- // The above copyright notice and this permission notice shall be included in
100
- // all copies or substantial portions of the Software.
101
-
102
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
103
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
104
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
105
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
106
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
107
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
108
- // THE SOFTWARE.
109
- contains ( ip : number [ ] ) : boolean {
110
- let part = 0 ;
111
- let shift ;
112
- let cidrBits = this . bits ;
113
-
114
- while ( cidrBits > 0 ) {
115
- shift = this . partSize - cidrBits ;
116
- if ( shift < 0 ) {
117
- shift = 0 ;
118
- }
79
+ // Based on CIDR matching implementation in `ipaddr.js`
80
+ // Source code:
81
+ // https://github.com/whitequark/ipaddr.js/blob/08c2cd41e2cb3400683cbd503f60421bfdf66921/lib/ipaddr.js#L107-L130
82
+ //
83
+ // Licensed: The MIT License (MIT)
84
+ // Copyright (C) 2011-2017 whitequark <[email protected] >
85
+ //
86
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
87
+ // of this software and associated documentation files (the "Software"), to deal
88
+ // in the Software without restriction, including without limitation the rights
89
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
90
+ // copies of the Software, and to permit persons to whom the Software is
91
+ // furnished to do so, subject to the following conditions:
119
92
120
- if ( ip [ part ] >> shift !== this . parts [ part ] >> shift ) {
121
- return false ;
122
- }
93
+ // The above copyright notice and this permission notice shall be included in
94
+ // all copies or substantial portions of the Software.
123
95
124
- cidrBits -= this . partSize ;
125
- part += 1 ;
96
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
98
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
99
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
100
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
101
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
102
+ // THE SOFTWARE.
103
+ function cidrContains ( cidr : Cidr , ip : number [ ] ) : boolean {
104
+ let part = 0 ;
105
+ let shift ;
106
+ let cidrBits = cidr . bits ;
107
+
108
+ while ( cidrBits > 0 ) {
109
+ shift = cidr . partSize - cidrBits ;
110
+ if ( shift < 0 ) {
111
+ shift = 0 ;
126
112
}
127
113
128
- return true ;
114
+ if ( ip [ part ] >> shift !== cidr . parts [ part ] >> shift ) {
115
+ return false ;
116
+ }
117
+
118
+ cidrBits -= cidr . partSize ;
119
+ part += 1 ;
129
120
}
121
+
122
+ return true ;
130
123
}
131
124
132
- class Ipv4Cidr extends CidrBase {
125
+ class Ipv4Cidr {
133
126
type = "v4" as const ;
134
127
partSize = 8 as const ;
135
128
parts : Readonly < Ipv4Tuple > ;
136
129
bits : number ;
137
130
138
131
constructor ( parts : Ipv4Tuple , bits : number ) {
139
- super ( ) ;
140
132
this . bits = bits ;
141
133
this . parts = parts ;
142
134
Object . freeze ( this ) ;
143
135
}
144
136
145
- contains ( ip : Ipv4Tuple ) : boolean {
146
- return super . contains ( ip ) ;
137
+ contains ( ip : Array < number > ) : boolean {
138
+ return cidrContains ( this , ip ) ;
147
139
}
148
140
}
149
141
150
- class Ipv6Cidr extends CidrBase {
142
+ class Ipv6Cidr {
151
143
type = "v6" as const ;
152
144
partSize = 16 as const ;
153
145
parts : Readonly < Ipv6Tuple > ;
154
146
bits : number ;
155
147
156
148
constructor ( parts : Ipv6Tuple , bits : number ) {
157
- super ( ) ;
158
149
this . bits = bits ;
159
150
this . parts = parts ;
160
151
Object . freeze ( this ) ;
161
152
}
162
153
163
- contains ( ip : Ipv6Tuple ) : boolean {
164
- return super . contains ( ip ) ;
154
+ contains ( ip : Array < number > ) : boolean {
155
+ return cidrContains ( this , ip ) ;
165
156
}
166
157
}
167
158
@@ -203,7 +194,7 @@ function isCidr(address: string): address is `${string}/${string}` {
203
194
204
195
// Converts a string that looks like a Cidr address into the corresponding class
205
196
// while ignoring non-Cidr IP addresses.
206
- export function parseProxy ( proxy : string ) : string | CidrBase {
197
+ export function parseProxy ( proxy : string ) : string | Cidr {
207
198
if ( isCidr ( proxy ) ) {
208
199
return parseCidr ( proxy ) ;
209
200
} else {
@@ -471,7 +462,7 @@ const IPV4_BROADCAST = u32FromBytes([255, 255, 255, 255]);
471
462
472
463
function isGlobalIpv4 (
473
464
s : unknown ,
474
- proxies : ReadonlyArray < string | CidrBase > | null | undefined ,
465
+ proxies : ReadonlyArray < string | Cidr > | null | undefined ,
475
466
) : s is string {
476
467
if ( typeof s !== "string" ) {
477
468
return false ;
@@ -570,7 +561,7 @@ function isGlobalIpv4(
570
561
571
562
function isGlobalIpv6 (
572
563
s : unknown ,
573
- proxies : ReadonlyArray < string | CidrBase > | null | undefined ,
564
+ proxies : ReadonlyArray < string | Cidr > | null | undefined ,
574
565
) : s is string {
575
566
if ( typeof s !== "string" ) {
576
567
return false ;
@@ -721,7 +712,7 @@ function isGlobalIpv6(
721
712
722
713
function isGlobalIp (
723
714
s : unknown ,
724
- proxies : ReadonlyArray < string | CidrBase > | null | undefined ,
715
+ proxies : ReadonlyArray < string | Cidr > | null | undefined ,
725
716
) : s is string {
726
717
if ( isGlobalIpv4 ( s , proxies ) ) {
727
718
return true ;
@@ -772,7 +763,7 @@ export type Platform = "cloudflare" | "fly-io" | "vercel" | "render";
772
763
773
764
export interface Options {
774
765
platform ?: Platform | null | undefined ;
775
- proxies ?: ReadonlyArray < string | CidrBase > | null | undefined ;
766
+ proxies ?: ReadonlyArray < string | Cidr > | null | undefined ;
776
767
}
777
768
778
769
function isHeaders ( val : HeaderLike [ "headers" ] ) : val is Headers {
0 commit comments