Skip to content

Commit 1a34915

Browse files
author
v1rtl
committed
use ipaddrjs deno lib instead of esm.sh
1 parent 39f2c9b commit 1a34915

File tree

6 files changed

+60
-49
lines changed

6 files changed

+60
-49
lines changed

deno.lock

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/// <reference types="./ipaddr.d.ts" />
1+
export type { RequestWithConnection } from 'https://deno.land/x/[email protected]/mod.ts'
2+
export { forwarded } from 'https://deno.land/x/[email protected]/mod.ts'
23
export {
4+
IPv4,
5+
IPv6,
36
isValid,
47
parse,
5-
} from "https://esm.sh/[email protected]/lib/ipaddr.js?exports=isValid,parse";
6-
export type { IPv4, IPv6 } from "./ipaddr.d.ts";
7-
export type { RequestWithConnection } from "https://deno.land/x/[email protected]/mod.ts";
8-
export { forwarded } from "https://deno.land/x/[email protected]/mod.ts";
8+
} from 'https://deno.land/x/[email protected]/mod.ts'

example.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Server } from 'https://deno.land/std@0.192.0/http/server.ts'
1+
import { Server } from 'https://deno.land/std@0.197.0/http/server.ts'
22

33
import { proxyaddr, RequestWithConnection } from './mod.ts'
44

ipaddr.d.ts

-20
This file was deleted.

mod.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { forwarded, IPv4, IPv6, isValid, parse } from './deps.ts'
2-
import type { RequestWithConnection } from './deps.ts'
1+
import { forwarded, isValid, parse } from './deps.ts'
2+
import type { IPv4, IPv6, RequestWithConnection } from './deps.ts'
33
export { RequestWithConnection }
44

55
type Trust = ((addr: string, i?: number) => boolean) | string[] | string
@@ -98,19 +98,18 @@ export function parseIPNotation(note: string) {
9898

9999
if (!isValid(str)) throw new TypeError('invalid IP address: ' + str)
100100

101-
let ip = parse(str)
101+
let ip = parse(str) as IPv4 | IPv6
102102

103103
if (pos === -1 && ip.kind() === 'ipv6') {
104-
ip = ip as typeof IPv6
104+
ip = ip as IPv6
105105

106106
if (ip.isIPv4MappedAddress()) ip = ip.toIPv4Address()
107107
}
108108

109109
const max = ip.kind() === 'ipv6' ? 128 : 32
110110

111-
let range: string | number | null = pos !== -1
112-
? note.substring(pos + 1, note.length)
113-
: null
111+
let range: string | number | null =
112+
pos !== -1 ? note.substring(pos + 1, note.length) : null
114113

115114
if (range === null) range = max
116115
else if (DIGIT_REGEXP.test(range)) range = parseInt(range, 10)
@@ -170,13 +169,15 @@ function trustMulti(subnets: (IPv4 | IPv6)[][]) {
170169
}
171170

172171
if (!ipconv) {
173-
ipconv = subnetkind === 'ipv4'
174-
? ip.toIPv4Address()
175-
: ip.toIPv4MappedAddress()
172+
ipconv =
173+
subnetkind === 'ipv4'
174+
? (ip as IPv6).toIPv4Address()
175+
: (ip as IPv6).toIPv4MappedAddress()
176176
}
177177

178178
trusted = ipconv
179179
}
180+
// @ts-ignore types
180181
if (trusted.match(subnetip, subnetrange)) return true
181182
}
182183
return false
@@ -192,17 +193,26 @@ function trustSingle(subnet: (IPv4 | IPv6)[]) {
192193
const subnetkind = subnetip.kind()
193194
const subnetisipv4 = subnetkind === 'ipv4'
194195
const subnetrange = subnet[1]
196+
195197
return function trust(addr: string) {
196198
if (!isValid(addr)) return false
199+
197200
let ip = parse(addr)
198201
const kind = ip.kind()
202+
199203
if (kind !== subnetkind) {
200204
if (subnetisipv4 && !(ip as IPv6).isIPv4MappedAddress()) {
205+
// Incompatible IP addresses
201206
return false
202207
}
203208

204-
ip = subnetisipv4 ? ip.toIPv4Address() : ip.toIPv4MappedAddress()
209+
// Convert IP to match subnet IP kind
210+
ip = subnetisipv4
211+
? (ip as IPv6).toIPv4Address()
212+
: (ip as IPv6).toIPv4MappedAddress()
205213
}
214+
215+
// @ts-ignore types
206216
return ip.match(subnetip, subnetrange)
207217
}
208218
}

mod_test.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import {
55
run,
66
} from 'https://deno.land/x/[email protected]/mod.ts'
77
import { all, RequestWithConnection } from './mod.ts'
8-
import { ConnInfo } from 'https://deno.land/std@0.192.0/http/server.ts'
8+
import { ConnInfo } from 'https://deno.land/std@0.197.0/http/server.ts'
99

1010
const createReq = (
1111
hostname: string,
12-
headers?: Record<string, string>,
13-
): RequestWithConnection => ({
14-
conn: {
15-
remoteAddr: {
16-
hostname,
17-
port: 8081,
18-
transport: 'tcp',
19-
},
20-
} as ConnInfo,
21-
headers: new Headers(headers || {}),
22-
} as unknown as RequestWithConnection)
12+
headers?: Record<string, string>
13+
): RequestWithConnection =>
14+
({
15+
conn: {
16+
remoteAddr: {
17+
hostname,
18+
port: 8081,
19+
transport: 'tcp',
20+
},
21+
} as ConnInfo,
22+
headers: new Headers(headers || {}),
23+
} as unknown as RequestWithConnection)
2324

2425
describe('all(req, trust)', () => {
2526
it('with no headers should return socket address', () => {

0 commit comments

Comments
 (0)