Skip to content
File

Blob: src/worker/util/request-context.ts

typescript26 lines
1import { sha256Hex } from "@/worker/auth/crypto";
2 
3export 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 
8export async function hashNullable(value: string | null | undefined): Promise<string | null> {
9 return value ? await sha256Hex(value) : null;
10}
11 
12export function pathPrefix(pathname: string): string {
13 const segment = pathname.split("/").filter(Boolean)[0];
14 return segment ? `/${segment}` : "/";
15}
16 
17export 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}