diff options
author | Marvin Hagemeister <marvin@deno.com> | 2024-07-09 17:46:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-09 17:46:10 +0200 |
commit | 07613a6bf26d9112d47fda9e502425395bd78105 (patch) | |
tree | 5ad911d495cfdbff1f6f9cfe63865571ab7e1fa5 /ext/node/polyfills/http.ts | |
parent | 0425dabb84b41d2b7d3204068ce8a1f4a397bce6 (diff) |
fix(node/http): support all `.writeHead()` signatures (#24469)
Implement the missing `.writeHead()` signatures from Node's
`ServerResponse` class that we didn't support.
Fixes https://github.com/denoland/deno/issues/24468
Diffstat (limited to 'ext/node/polyfills/http.ts')
-rw-r--r-- | ext/node/polyfills/http.ts | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index 534bad908..3059da3a6 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -3,7 +3,7 @@ // TODO(petamoriken): enable prefer-primordials for node polyfills // deno-lint-ignore-file prefer-primordials -import { core } from "ext:core/mod.js"; +import { core, primordials } from "ext:core/mod.js"; import { op_fetch_response_upgrade, op_fetch_send, @@ -68,6 +68,7 @@ import { resourceForReadableStream } from "ext:deno_web/06_streams.js"; import { TcpConn } from "ext:deno_net/01_net.js"; const { internalRidSymbol } = core; +const { ArrayIsArray } = primordials; enum STATUS_CODES { /** RFC 7231, 6.2.1 */ @@ -1458,20 +1459,65 @@ export class ServerResponse extends NodeWritable { getHeaderNames() { return Object.keys(this.#headers); } - getHeaders() { + getHeaders(): Record<string, string | number | string[]> { + // @ts-ignore Ignore null __proto__ return { __proto__: null, ...this.#headers }; } hasHeader(name: string) { return Object.hasOwn(this.#headers, name); } - writeHead(status: number, headers: Record<string, string> = {}) { + writeHead( + status: number, + statusMessage?: string, + headers?: + | Record<string, string | number | string[]> + | Array<[string, string]>, + ): this; + writeHead( + status: number, + headers?: + | Record<string, string | number | string[]> + | Array<[string, string]>, + ): this; + writeHead( + status: number, + statusMessageOrHeaders?: + | string + | Record<string, string | number | string[]> + | Array<[string, string]>, + maybeHeaders?: + | Record<string, string | number | string[]> + | Array<[string, string]>, + ): this { this.statusCode = status; - for (const k in headers) { - if (Object.hasOwn(headers, k)) { - this.setHeader(k, headers[k]); + + let headers = null; + if (typeof statusMessageOrHeaders === "string") { + this.statusMessage = statusMessageOrHeaders; + if (maybeHeaders !== undefined) { + headers = maybeHeaders; } + } else if (statusMessageOrHeaders !== undefined) { + headers = statusMessageOrHeaders; } + + if (headers !== null) { + if (ArrayIsArray(headers)) { + headers = headers as Array<[string, string]>; + for (let i = 0; i < headers.length; i++) { + this.appendHeader(headers[i][0], headers[i][1]); + } + } else { + headers = headers as Record<string, string>; + for (const k in headers) { + if (Object.hasOwn(headers, k)) { + this.setHeader(k, headers[k]); + } + } + } + } + return this; } |