| 31 | /** An internal oak abstraction for handling a Deno native request. Most users |
| 32 | * of oak do not need to worry about this abstraction. */ |
| 33 | export class NativeRequest implements ServerRequest { |
| 34 | #remoteAddr?: NetAddr; |
| 35 | // deno-lint-ignore no-explicit-any |
| 36 | #reject: (reason?: any) => void; |
| 37 | #request: Request; |
| 38 | #resolve: (value: Response) => void; |
| 39 | #resolved = false; |
| 40 | #response: Promise<Response>; |
| 41 | #upgradeWebSocket?: UpgradeWebSocketFn; |
| 42 | |
| 43 | constructor( |
| 44 | request: Request, |
| 45 | info: NativeRequestInfo, |
| 46 | ) { |
| 47 | this.#remoteAddr = info.remoteAddr; |
| 48 | // this allows for the value to be explicitly undefined in the options |
| 49 | this.#upgradeWebSocket = "upgradeWebSocket" in info |
| 50 | ? info.upgradeWebSocket |
| 51 | : maybeUpgradeWebSocket; |
| 52 | this.#request = request; |
| 53 | const { resolve, reject, promise } = createPromiseWithResolvers<Response>(); |
| 54 | this.#resolve = resolve; |
| 55 | this.#reject = reject; |
| 56 | this.#response = promise; |
| 57 | } |
| 58 | |
| 59 | get body(): ReadableStream<Uint8ArrayArrayBuffer> | null { |
| 60 | return this.#request.body; |
| 61 | } |
| 62 | |
| 63 | get headers(): Headers { |
| 64 | return this.#request.headers; |
| 65 | } |
| 66 | |
| 67 | get method(): string { |
| 68 | return this.#request.method; |
| 69 | } |
| 70 | |
| 71 | get remoteAddr(): string | undefined { |
| 72 | return this.#remoteAddr?.hostname; |
| 73 | } |
| 74 | |
| 75 | get request(): Request { |
| 76 | return this.#request; |
| 77 | } |
| 78 | |
| 79 | get response(): Promise<Response> { |
| 80 | return this.#response; |
| 81 | } |
| 82 | |
| 83 | get url(): string { |
| 84 | try { |
| 85 | const url = new URL(this.#request.url); |
| 86 | return this.#request.url.replace(url.origin, ""); |
| 87 | } catch { |
| 88 | // we don't care about errors, we just want to fall back |
| 89 | } |
| 90 | return this.#request.url; |
nothing calls this directly
no outgoing calls
no test coverage detected