import type { NetAddr, ServerRequest, UpgradeWebSocketFn, UpgradeWebSocketOptions,} from "./types.ts";import { createPromiseWithResolvers } from "./util.ts";
export const DomResponse: typeof Response = (globalThis as any).Response ?? class MockResponse {};
const maybeUpgradeWebSocket: UpgradeWebSocketFn | undefined = "upgradeWebSocket" in Deno ? (Deno as any).upgradeWebSocket.bind(Deno) : undefined;
export function isNativeRequest(r: ServerRequest): r is NativeRequest { return r instanceof NativeRequest;}
export interface NativeRequestInfo { remoteAddr?: NetAddr; upgradeWebSocket?: UpgradeWebSocketFn;}
export class NativeRequest implements ServerRequest { #remoteAddr?: NetAddr; #reject!: (reason?: any) => void; #request: Request; #resolve!: (value: Response) => void; #resolved = false; #response: Promise<Response>; #upgradeWebSocket?: UpgradeWebSocketFn;
constructor( request: Request, info: NativeRequestInfo, ) { this.#remoteAddr = info.remoteAddr; this.#upgradeWebSocket = "upgradeWebSocket" in info ? info.upgradeWebSocket : maybeUpgradeWebSocket; this.#request = request; const { resolve, reject, promise } = createPromiseWithResolvers<Response>(); this.#resolve = resolve; this.#reject = reject; this.#response = promise; }
get body(): ReadableStream<Uint8Array> | null { return this.#request.body as any; }
get headers(): Headers { return this.#request.headers; }
get method(): string { return this.#request.method; }
get remoteAddr(): string | undefined { return this.#remoteAddr?.hostname; }
get request(): Request { return this.#request; }
get response(): Promise<Response> { return this.#response; }
get url(): string { try { const url = new URL(this.#request.url); return this.#request.url.replace(url.origin, ""); } catch { } return this.#request.url; }
get rawUrl(): string { return this.#request.url; }
error(reason?: any): void { if (this.#resolved) { throw new Error("Request already responded to."); } this.#reject(reason); this.#resolved = true; }
getBody(): ReadableStream<Uint8Array> | null { return this.#request.body; }
respond(response: Response): void { if (this.#resolved) { throw new Error("Request already responded to."); } this.#resolved = true; this.#resolve(response); }
upgrade(options?: UpgradeWebSocketOptions): WebSocket { if (this.#resolved) { throw new Error("Request already responded to."); } if (!this.#upgradeWebSocket) { throw new TypeError("Upgrading web sockets not supported."); } const { response, socket } = this.#upgradeWebSocket( this.#request, options, ); this.#resolve(response); this.#resolved = true; return socket; }}