| 48 | }; |
| 49 | |
| 50 | export class NodeRequest implements ServerRequest { |
| 51 | #request: IncomingMessage; |
| 52 | #response: ServerResponse; |
| 53 | #responded = false; |
| 54 | |
| 55 | get remoteAddr(): string | undefined { |
| 56 | const addr = this.#request.socket.address(); |
| 57 | // deno-lint-ignore no-explicit-any |
| 58 | return addr && (addr as any)?.address; |
| 59 | } |
| 60 | |
| 61 | get headers(): Headers { |
| 62 | return new Headers(this.#request.headers as Record<string, string>); |
| 63 | } |
| 64 | |
| 65 | get method(): string { |
| 66 | return this.#request.method ?? "GET"; |
| 67 | } |
| 68 | |
| 69 | get url(): string { |
| 70 | return this.#request.url ?? ""; |
| 71 | } |
| 72 | |
| 73 | constructor( |
| 74 | request: IncomingMessage, |
| 75 | response: ServerResponse, |
| 76 | ) { |
| 77 | this.#request = request; |
| 78 | this.#response = response; |
| 79 | } |
| 80 | |
| 81 | // deno-lint-ignore no-explicit-any |
| 82 | error(reason?: any) { |
| 83 | if (this.#responded) { |
| 84 | throw new Error("Request already responded to."); |
| 85 | } |
| 86 | let error; |
| 87 | if (reason) { |
| 88 | error = reason instanceof Error ? reason : new Error(String(reason)); |
| 89 | } |
| 90 | this.#response.destroy(error); |
| 91 | this.#responded = true; |
| 92 | } |
| 93 | |
| 94 | getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null { |
| 95 | let body: ReadableStream<Uint8ArrayArrayBuffer> | null; |
| 96 | if (this.method === "GET" || this.method === "HEAD") { |
| 97 | body = null; |
| 98 | } else { |
| 99 | body = new ReadableStream<Uint8ArrayArrayBuffer>({ |
| 100 | start: (controller) => { |
| 101 | this.#request.on("data", (chunk) => { |
| 102 | controller.enqueue(chunk as Uint8ArrayArrayBuffer); |
| 103 | }); |
| 104 | this.#request.on("error", (err: Error) => { |
| 105 | controller.error(err); |
| 106 | }); |
| 107 | this.#request.on("end", () => { |
nothing calls this directly
no outgoing calls
no test coverage detected