| 309 | headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined |
| 310 | ): this |
| 311 | public writeHead( |
| 312 | statusCode: number, |
| 313 | statusMessage?: |
| 314 | | string |
| 315 | | OutgoingHttpHeaders |
| 316 | | OutgoingHttpHeader[] |
| 317 | | undefined, |
| 318 | headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined |
| 319 | ): this { |
| 320 | if (!headers && typeof statusMessage !== 'string') { |
| 321 | headers = statusMessage |
| 322 | } else if (typeof statusMessage === 'string' && statusMessage.length > 0) { |
| 323 | this.statusMessage = statusMessage |
| 324 | } |
| 325 | |
| 326 | if (headers) { |
| 327 | // When headers have been set with response.setHeader(), they will be |
| 328 | // merged with any headers passed to response.writeHead(), with the |
| 329 | // headers passed to response.writeHead() given precedence. |
| 330 | // |
| 331 | // https://nodejs.org/api/http.html#responsewriteheadstatuscode-statusmessage-headers |
| 332 | // |
| 333 | // For this reason, we need to only call `set` to ensure that this will |
| 334 | // overwrite any existing headers. |
| 335 | if (Array.isArray(headers)) { |
| 336 | // headers may be an Array where the keys and values are in the same list. |
| 337 | // It is not a list of tuples. So, the even-numbered offsets are key |
| 338 | // values, and the odd-numbered offsets are the associated values. The |
| 339 | // array is in the same format as request.rawHeaders. |
| 340 | for (let i = 0; i < headers.length; i += 2) { |
| 341 | // The header key is always a string according to the spec. |
| 342 | this.setHeader(headers[i] as string, headers[i + 1]) |
| 343 | } |
| 344 | } else { |
| 345 | for (const [key, value] of Object.entries(headers)) { |
| 346 | // Skip undefined values |
| 347 | if (typeof value === 'undefined') continue |
| 348 | |
| 349 | this.setHeader(key, value) |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | this.statusCode = statusCode |
| 355 | this.headersSent = true |
| 356 | this.headPromiseResolve?.() |
| 357 | |
| 358 | return this |
| 359 | } |
| 360 | |
| 361 | public hasHeader(name: string): boolean { |
| 362 | return this.headers.has(name) |