File
Blob: src/worker/util/request-context.ts
| 1 | import { sha256Hex } from "@/worker/auth/crypto"; |
| 2 | |
| 3 | export function clientIp(request: Request): string | null { |
| 4 | const forwardedFor = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim(); |
| 5 | return request.headers.get("cf-connecting-ip") ?? forwardedFor ?? request.headers.get("x-real-ip") ?? null; |
| 6 | } |
| 7 | |
| 8 | export async function hashNullable(value: string | null | undefined): Promise<string | null> { |
| 9 | return value ? await sha256Hex(value) : null; |
| 10 | } |
| 11 | |
| 12 | export function pathPrefix(pathname: string): string { |
| 13 | const segment = pathname.split("/").filter(Boolean)[0]; |
| 14 | return segment ? `/${segment}` : "/"; |
| 15 | } |
| 16 | |
| 17 | export function destinationHost(request: Request): string | null { |
| 18 | const destination = request.headers.get("destination"); |
| 19 | if (!destination) return null; |
| 20 | try { |
| 21 | return new URL(destination).host; |
| 22 | } catch { |
| 23 | return null; |
| 24 | } |
| 25 | } |