Home / Function/ onSendEnd() — fastify Function Reference

onSendEnd() — fastify Function Reference

Architecture documentation for the onSendEnd() function in reply.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  8460cb7c_51e5_baba_0f16_9ea8d190d139["onSendEnd()"]
  4bcd71dc_1ec2_5fe8_b8ff_4a371e392925["reply.js"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|defined in| 4bcd71dc_1ec2_5fe8_b8ff_4a371e392925
  0da2f4ee_9f19_0b3c_eba5_b3fc33ca1fea["onSendHook()"]
  0da2f4ee_9f19_0b3c_eba5_b3fc33ca1fea -->|calls| 8460cb7c_51e5_baba_0f16_9ea8d190d139
  76c4faa7_bbf0_a580_fab9_e5bbf7ee7506["wrapOnSendEnd()"]
  76c4faa7_bbf0_a580_fab9_e5bbf7ee7506 -->|calls| 8460cb7c_51e5_baba_0f16_9ea8d190d139
  c6d9a0f0_0f24_2129_fb32_dcc24b82bc25["header()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| c6d9a0f0_0f24_2129_fb32_dcc24b82bc25
  193bf1cb_b908_f458_21f8_567503b5bf8f["code()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| 193bf1cb_b908_f458_21f8_567503b5bf8f
  e0d1c987_ac40_1669_63e9_2bab4d6f95ff["safeWriteHead()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| e0d1c987_ac40_1669_63e9_2bab4d6f95ff
  944c1a61_ec49_25f7_bdf7_a5b89cd52706["sendTrailer()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| 944c1a61_ec49_25f7_bdf7_a5b89cd52706
  812490ea_e938_79ff_ced3_b0658978631e["removeHeader()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| 812490ea_e938_79ff_ced3_b0658978631e
  3d4cff63_882d_17c7_3219_d30292baf81d["sendStream()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| 3d4cff63_882d_17c7_3219_d30292baf81d
  e86de8a4_fcf2_726c_6613_1273524e94e2["sendWebStream()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| e86de8a4_fcf2_726c_6613_1273524e94e2
  style 8460cb7c_51e5_baba_0f16_9ea8d190d139 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/reply.js lines 563–671

function onSendEnd (reply, payload) {
  const res = reply.raw
  const req = reply.request

  // we check if we need to update the trailers header and set it
  if (reply[kReplyTrailers] !== null) {
    const trailerHeaders = Object.keys(reply[kReplyTrailers])
    let header = ''
    for (const trailerName of trailerHeaders) {
      if (typeof reply[kReplyTrailers][trailerName] !== 'function') continue
      header += ' '
      header += trailerName
    }
    // it must be chunked for trailer to work
    reply.header('Transfer-Encoding', 'chunked')
    reply.header('Trailer', header.trim())
  }

  // since Response contain status code, headers and body,
  // we need to update the status, add the headers and use it's body as payload
  // before continuing
  if (toString.call(payload) === '[object Response]') {
    // https://developer.mozilla.org/en-US/docs/Web/API/Response/status
    if (typeof payload.status === 'number') {
      reply.code(payload.status)
    }

    // https://developer.mozilla.org/en-US/docs/Web/API/Response/headers
    if (typeof payload.headers === 'object' && typeof payload.headers.forEach === 'function') {
      for (const [headerName, headerValue] of payload.headers) {
        reply.header(headerName, headerValue)
      }
    }

    // https://developer.mozilla.org/en-US/docs/Web/API/Response/body
    if (payload.body !== null) {
      if (payload.bodyUsed) {
        throw new FST_ERR_REP_RESPONSE_BODY_CONSUMED()
      }
    }
    // Keep going, body is either null or ReadableStream
    payload = payload.body
  }
  const statusCode = res.statusCode

  if (payload === undefined || payload === null) {
    // according to https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2
    // we cannot send a content-length for 304 and 204, and all status code
    // < 200
    // A sender MUST NOT send a Content-Length header field in any message
    // that contains a Transfer-Encoding header field.
    // For HEAD we don't overwrite the `content-length`
    if (statusCode >= 200 && statusCode !== 204 && statusCode !== 304 && req.method !== 'HEAD' && reply[kReplyTrailers] === null) {
      reply[kReplyHeaders]['content-length'] = '0'
    }

    safeWriteHead(reply, statusCode)
    sendTrailer(payload, res, reply)
    return
  }

  if ((statusCode >= 100 && statusCode < 200) || statusCode === 204) {
    // Responses without a content body must not send content-type
    // or content-length headers.
    // See https://www.rfc-editor.org/rfc/rfc9110.html#section-8.6.
    reply.removeHeader('content-type')
    reply.removeHeader('content-length')
    safeWriteHead(reply, statusCode)
    sendTrailer(undefined, res, reply)
    if (typeof payload.resume === 'function') {
      payload.on('error', noop)
      payload.resume()
    }
    return
  }

  // node:stream
  if (typeof payload.pipe === 'function') {
    sendStream(payload, res, reply)
    return
  }

Domain

Subdomains

Defined In

Frequently Asked Questions

What does onSendEnd() do?
onSendEnd() is a function in the fastify codebase, defined in lib/reply.js.
Where is onSendEnd() defined?
onSendEnd() is defined in lib/reply.js at line 563.
What does onSendEnd() call?
onSendEnd() calls 7 function(s): code, header, removeHeader, safeWriteHead, sendStream, sendTrailer, sendWebStream.
What calls onSendEnd()?
onSendEnd() is called by 2 function(s): onSendHook, wrapOnSendEnd.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free