Home / Function/ sendTrailer() — fastify Function Reference

sendTrailer() — fastify Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  944c1a61_ec49_25f7_bdf7_a5b89cd52706["sendTrailer()"]
  4bcd71dc_1ec2_5fe8_b8ff_4a371e392925["reply.js"]
  944c1a61_ec49_25f7_bdf7_a5b89cd52706 -->|defined in| 4bcd71dc_1ec2_5fe8_b8ff_4a371e392925
  8460cb7c_51e5_baba_0f16_9ea8d190d139["onSendEnd()"]
  8460cb7c_51e5_baba_0f16_9ea8d190d139 -->|calls| 944c1a61_ec49_25f7_bdf7_a5b89cd52706
  e86de8a4_fcf2_726c_6613_1273524e94e2["sendWebStream()"]
  e86de8a4_fcf2_726c_6613_1273524e94e2 -->|calls| 944c1a61_ec49_25f7_bdf7_a5b89cd52706
  13e2d38d_ce6d_5fec_32a3_0640002b60a9["sendStreamTrailer()"]
  13e2d38d_ce6d_5fec_32a3_0640002b60a9 -->|calls| 944c1a61_ec49_25f7_bdf7_a5b89cd52706
  86dc4e98_212d_2b7c_7128_76f05e7a8787["send()"]
  944c1a61_ec49_25f7_bdf7_a5b89cd52706 -->|calls| 86dc4e98_212d_2b7c_7128_76f05e7a8787
  f26bdb28_b830_3aec_800b_087055150c6c["then()"]
  944c1a61_ec49_25f7_bdf7_a5b89cd52706 -->|calls| f26bdb28_b830_3aec_800b_087055150c6c
  style 944c1a61_ec49_25f7_bdf7_a5b89cd52706 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/reply.js lines 811–863

function sendTrailer (payload, res, reply) {
  if (reply[kReplyTrailers] === null) {
    // when no trailer, we close the stream
    res.end(null, null, null) // avoid ArgumentsAdaptorTrampoline from V8
    return
  }
  const trailerHeaders = Object.keys(reply[kReplyTrailers])
  const trailers = {}
  let handled = 0
  let skipped = true
  function send () {
    // add trailers when all handler handled
    /* istanbul ignore else */
    if (handled === 0) {
      res.addTrailers(trailers)
      // we need to properly close the stream
      // after trailers sent
      res.end(null, null, null) // avoid ArgumentsAdaptorTrampoline from V8
    }
  }

  for (const trailerName of trailerHeaders) {
    if (typeof reply[kReplyTrailers][trailerName] !== 'function') continue
    skipped = false
    handled--

    function cb (err, value) {
      // TODO: we may protect multiple callback calls
      //       or mixing async-await with callback
      handled++

      // we can safely ignore error for trailer
      // since it does affect the client
      // we log in here only for debug usage
      if (err) reply.log.debug(err)
      else trailers[trailerName] = value

      // we push the check to the end of event
      // loop, so the registration continue to
      // process.
      process.nextTick(send)
    }

    const result = reply[kReplyTrailers][trailerName](reply, payload, cb)
    if (typeof result === 'object' && typeof result.then === 'function') {
      result.then((v) => cb(null, v), cb)
    }
  }

  // when all trailers are skipped
  // we need to close the stream
  if (skipped) res.end(null, null, null) // avoid ArgumentsAdaptorTrampoline from V8
}

Domain

Subdomains

Defined In

Calls

Frequently Asked Questions

What does sendTrailer() do?
sendTrailer() is a function in the fastify codebase, defined in lib/reply.js.
Where is sendTrailer() defined?
sendTrailer() is defined in lib/reply.js at line 811.
What does sendTrailer() call?
sendTrailer() calls 2 function(s): send, then.
What calls sendTrailer()?
sendTrailer() is called by 3 function(s): onSendEnd, sendStreamTrailer, sendWebStream.

Analyze Your Own Codebase

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

Try Supermodel Free