MCPcopy
hub / github.com/vercel/next.js / writeHead

Method writeHead

packages/next/src/server/lib/mock-request.ts:311–359  ·  view source on GitHub ↗
(
    statusCode: number,
    statusMessage?:
      | string
      | OutgoingHttpHeaders
      | OutgoingHttpHeader[]
      | undefined,
    headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined
  )

Source from the content-addressed store, hash-verified

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)

Callers 15

sendErrorFunction · 0.80
index.jsFile · 0.80
handleRequestMethod · 0.80
handleGetRequestMethod · 0.80
replayEventsMethod · 0.80
handlePostRequestMethod · 0.80
handleDeleteRequestMethod · 0.80
validateSessionMethod · 0.80
sendMethod · 0.80
constructorMethod · 0.80

Calls 3

setHeaderMethod · 0.95
isArrayMethod · 0.80
entriesMethod · 0.45

Tested by 2

runAndCaptureOutputFunction · 0.64