|
| 1 | +// Adapted from https://github.com/honojs/node-server/blob/1eb73c6d985665e75458ddd08c23bbc1dbdc7bcd/src/response.ts |
| 2 | +// deno-lint-ignore-file no-explicit-any |
| 3 | +// |
| 4 | +import type { OutgoingHttpHeaders } from "node:http"; |
| 5 | + |
| 6 | +interface InternalBody { |
| 7 | + source: string | Uint8Array | FormData | Blob | null; |
| 8 | + stream: ReadableStream; |
| 9 | + length: number | null; |
| 10 | +} |
| 11 | + |
| 12 | +const GlobalResponse = globalThis.Response; |
| 13 | + |
| 14 | +const responseCache = Symbol("responseCache"); |
| 15 | +const getResponseCache = Symbol("getResponseCache"); |
| 16 | +export const cacheKey = Symbol("cache"); |
| 17 | + |
| 18 | +export const buildOutgoingHttpHeaders = ( |
| 19 | + headers: Headers | HeadersInit | null | undefined, |
| 20 | +): OutgoingHttpHeaders => { |
| 21 | + const res: OutgoingHttpHeaders = {}; |
| 22 | + if (!(headers instanceof Headers)) { |
| 23 | + headers = new Headers(headers ?? undefined); |
| 24 | + } |
| 25 | + |
| 26 | + const cookies = []; |
| 27 | + for (const [k, v] of headers) { |
| 28 | + if (k === "set-cookie") { |
| 29 | + cookies.push(v); |
| 30 | + } else { |
| 31 | + res[k] = v; |
| 32 | + } |
| 33 | + } |
| 34 | + if (cookies.length > 0) { |
| 35 | + res["set-cookie"] = cookies; |
| 36 | + } |
| 37 | + res["content-type"] ??= "text/plain; charset=UTF-8"; |
| 38 | + |
| 39 | + return res; |
| 40 | +}; |
| 41 | + |
| 42 | +export class Response { |
| 43 | + #body?: BodyInit | null; |
| 44 | + #init?: ResponseInit; |
| 45 | + |
| 46 | + [getResponseCache](): typeof GlobalResponse { |
| 47 | + delete (this as any)[cacheKey]; |
| 48 | + return ((this as any)[responseCache] ||= new GlobalResponse( |
| 49 | + this.#body, |
| 50 | + this.#init, |
| 51 | + )); |
| 52 | + } |
| 53 | + |
| 54 | + constructor(body?: BodyInit | null, init?: ResponseInit) { |
| 55 | + this.#body = body; |
| 56 | + if (init instanceof Response) { |
| 57 | + const cachedGlobalResponse = (init as any)[responseCache]; |
| 58 | + if (cachedGlobalResponse) { |
| 59 | + this.#init = cachedGlobalResponse; |
| 60 | + // instantiate GlobalResponse cache and this object always returns value from global.Response |
| 61 | + this[getResponseCache](); |
| 62 | + return; |
| 63 | + } else { |
| 64 | + this.#init = init.#init; |
| 65 | + } |
| 66 | + } else { |
| 67 | + this.#init = init; |
| 68 | + } |
| 69 | + |
| 70 | + if ( |
| 71 | + typeof body === "string" || |
| 72 | + typeof (body as ReadableStream)?.getReader !== "undefined" |
| 73 | + ) { |
| 74 | + let headers = |
| 75 | + (init?.headers || { "content-type": "text/plain; charset=UTF-8" }) as |
| 76 | + | Record<string, string> |
| 77 | + | Headers |
| 78 | + | OutgoingHttpHeaders; |
| 79 | + if (headers instanceof Headers) { |
| 80 | + headers = buildOutgoingHttpHeaders(headers); |
| 81 | + } |
| 82 | + |
| 83 | + (this as any)[cacheKey] = [init?.status || 200, body, headers]; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +[ |
| 88 | + "body", |
| 89 | + "bodyUsed", |
| 90 | + "headers", |
| 91 | + "ok", |
| 92 | + "redirected", |
| 93 | + "status", |
| 94 | + "statusText", |
| 95 | + "trailers", |
| 96 | + "type", |
| 97 | + "url", |
| 98 | +].forEach((k) => { |
| 99 | + Object.defineProperty(Response.prototype, k, { |
| 100 | + get() { |
| 101 | + return this[getResponseCache]()[k]; |
| 102 | + }, |
| 103 | + }); |
| 104 | +}); |
| 105 | +["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => { |
| 106 | + Object.defineProperty(Response.prototype, k, { |
| 107 | + value: function () { |
| 108 | + return this[getResponseCache]()[k](); |
| 109 | + }, |
| 110 | + }); |
| 111 | +}); |
| 112 | +Object.setPrototypeOf(Response, GlobalResponse); |
| 113 | +Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype); |
0 commit comments