Skip to content

Commit a61a1ad

Browse files
committed
no base
1 parent 6f97b5a commit a61a1ad

File tree

1 file changed

+51
-60
lines changed

1 file changed

+51
-60
lines changed

ip/index.ts

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function isIpv6Cidr(cidr: unknown): cidr is Ipv6Cidr {
5656
function isTrustedProxy(
5757
ip: string,
5858
segments: ReadonlyArray<number>,
59-
proxies?: ReadonlyArray<string | CidrBase> | null | undefined,
59+
proxies?: ReadonlyArray<string | Cidr> | null | undefined,
6060
) {
6161
if (Array.isArray(proxies) && proxies.length > 0) {
6262
return proxies.some((proxy) => {
@@ -76,92 +76,83 @@ function isTrustedProxy(
7676
return false;
7777
}
7878

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:
11992

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.
12395

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;
126112
}
127113

128-
return true;
114+
if (ip[part] >> shift !== cidr.parts[part] >> shift) {
115+
return false;
116+
}
117+
118+
cidrBits -= cidr.partSize;
119+
part += 1;
129120
}
121+
122+
return true;
130123
}
131124

132-
class Ipv4Cidr extends CidrBase {
125+
class Ipv4Cidr {
133126
type = "v4" as const;
134127
partSize = 8 as const;
135128
parts: Readonly<Ipv4Tuple>;
136129
bits: number;
137130

138131
constructor(parts: Ipv4Tuple, bits: number) {
139-
super();
140132
this.bits = bits;
141133
this.parts = parts;
142134
Object.freeze(this);
143135
}
144136

145-
contains(ip: Ipv4Tuple): boolean {
146-
return super.contains(ip);
137+
contains(ip: Array<number>): boolean {
138+
return cidrContains(this, ip);
147139
}
148140
}
149141

150-
class Ipv6Cidr extends CidrBase {
142+
class Ipv6Cidr {
151143
type = "v6" as const;
152144
partSize = 16 as const;
153145
parts: Readonly<Ipv6Tuple>;
154146
bits: number;
155147

156148
constructor(parts: Ipv6Tuple, bits: number) {
157-
super();
158149
this.bits = bits;
159150
this.parts = parts;
160151
Object.freeze(this);
161152
}
162153

163-
contains(ip: Ipv6Tuple): boolean {
164-
return super.contains(ip);
154+
contains(ip: Array<number>): boolean {
155+
return cidrContains(this, ip);
165156
}
166157
}
167158

@@ -203,7 +194,7 @@ function isCidr(address: string): address is `${string}/${string}` {
203194

204195
// Converts a string that looks like a Cidr address into the corresponding class
205196
// while ignoring non-Cidr IP addresses.
206-
export function parseProxy(proxy: string): string | CidrBase {
197+
export function parseProxy(proxy: string): string | Cidr {
207198
if (isCidr(proxy)) {
208199
return parseCidr(proxy);
209200
} else {
@@ -471,7 +462,7 @@ const IPV4_BROADCAST = u32FromBytes([255, 255, 255, 255]);
471462

472463
function isGlobalIpv4(
473464
s: unknown,
474-
proxies: ReadonlyArray<string | CidrBase> | null | undefined,
465+
proxies: ReadonlyArray<string | Cidr> | null | undefined,
475466
): s is string {
476467
if (typeof s !== "string") {
477468
return false;
@@ -570,7 +561,7 @@ function isGlobalIpv4(
570561

571562
function isGlobalIpv6(
572563
s: unknown,
573-
proxies: ReadonlyArray<string | CidrBase> | null | undefined,
564+
proxies: ReadonlyArray<string | Cidr> | null | undefined,
574565
): s is string {
575566
if (typeof s !== "string") {
576567
return false;
@@ -721,7 +712,7 @@ function isGlobalIpv6(
721712

722713
function isGlobalIp(
723714
s: unknown,
724-
proxies: ReadonlyArray<string | CidrBase> | null | undefined,
715+
proxies: ReadonlyArray<string | Cidr> | null | undefined,
725716
): s is string {
726717
if (isGlobalIpv4(s, proxies)) {
727718
return true;
@@ -772,7 +763,7 @@ export type Platform = "cloudflare" | "fly-io" | "vercel" | "render";
772763

773764
export interface Options {
774765
platform?: Platform | null | undefined;
775-
proxies?: ReadonlyArray<string | CidrBase> | null | undefined;
766+
proxies?: ReadonlyArray<string | Cidr> | null | undefined;
776767
}
777768

778769
function isHeaders(val: HeaderLike["headers"]): val is Headers {

0 commit comments

Comments
 (0)