Home / Function/ send() — fastify Function Reference

send() — fastify Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  86dc4e98_212d_2b7c_7128_76f05e7a8787["send()"]
  4bcd71dc_1ec2_5fe8_b8ff_4a371e392925["reply.js"]
  86dc4e98_212d_2b7c_7128_76f05e7a8787 -->|defined in| 4bcd71dc_1ec2_5fe8_b8ff_4a371e392925
  ec055405_4033_df3a_534f_a70521a62963["redirect()"]
  ec055405_4033_df3a_534f_a70521a62963 -->|calls| 86dc4e98_212d_2b7c_7128_76f05e7a8787
  944c1a61_ec49_25f7_bdf7_a5b89cd52706["sendTrailer()"]
  944c1a61_ec49_25f7_bdf7_a5b89cd52706 -->|calls| 86dc4e98_212d_2b7c_7128_76f05e7a8787
  5ebe00b9_f378_7908_1ade_5e7acbd75a54["notFound()"]
  5ebe00b9_f378_7908_1ade_5e7acbd75a54 -->|calls| 86dc4e98_212d_2b7c_7128_76f05e7a8787
  193596e2_0e27_a2b0_95bd_4f757f255c22["getHeader()"]
  86dc4e98_212d_2b7c_7128_76f05e7a8787 -->|calls| 193596e2_0e27_a2b0_95bd_4f757f255c22
  d1d39f22_2d9a_4ca3_4bbe_11322ac6d36b["onErrorHook()"]
  86dc4e98_212d_2b7c_7128_76f05e7a8787 -->|calls| d1d39f22_2d9a_4ca3_4bbe_11322ac6d36b
  0da2f4ee_9f19_0b3c_eba5_b3fc33ca1fea["onSendHook()"]
  86dc4e98_212d_2b7c_7128_76f05e7a8787 -->|calls| 0da2f4ee_9f19_0b3c_eba5_b3fc33ca1fea
  d9e0045e_2fd6_72e4_67eb_c04f9908f19e["preSerializationHook()"]
  86dc4e98_212d_2b7c_7128_76f05e7a8787 -->|calls| d9e0045e_2fd6_72e4_67eb_c04f9908f19e
  style 86dc4e98_212d_2b7c_7128_76f05e7a8787 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/reply.js lines 127–213

Reply.prototype.send = function (payload) {
  if (this[kReplyIsRunningOnErrorHook]) {
    throw new FST_ERR_SEND_INSIDE_ONERR()
  }

  if (this.sent === true) {
    this.log.warn({ err: new FST_ERR_REP_ALREADY_SENT(this.request.url, this.request.method) })
    return this
  }

  if (this[kReplyIsError] || payload instanceof Error) {
    this[kReplyIsError] = false
    onErrorHook(this, payload, onSendHook)
    return this
  }

  if (payload === undefined) {
    onSendHook(this, payload)
    return this
  }

  const contentType = this.getHeader('content-type')
  const hasContentType = contentType !== undefined

  if (payload !== null) {
    if (
      // node:stream
      typeof payload.pipe === 'function' ||
      // node:stream/web
      typeof payload.getReader === 'function' ||
      // Response
      toString.call(payload) === '[object Response]'
    ) {
      onSendHook(this, payload)
      return this
    }

    if (payload.buffer instanceof ArrayBuffer) {
      if (!hasContentType) {
        this[kReplyHeaders]['content-type'] = CONTENT_TYPE.OCTET
      }
      const payloadToSend = Buffer.isBuffer(payload)
        ? payload
        : Buffer.from(payload.buffer, payload.byteOffset, payload.byteLength)
      onSendHook(this, payloadToSend)
      return this
    }

    if (!hasContentType && typeof payload === 'string') {
      this[kReplyHeaders]['content-type'] = CONTENT_TYPE.PLAIN
      onSendHook(this, payload)
      return this
    }
  }

  if (this[kReplySerializer] !== null) {
    if (typeof payload !== 'string') {
      preSerializationHook(this, payload)
      return this
    }
    payload = this[kReplySerializer](payload)

    // The indexOf below also matches custom json mimetypes such as 'application/hal+json' or 'application/ld+json'
  } else if (!hasContentType || contentType.indexOf('json') !== -1) {
    if (!hasContentType) {
      this[kReplyHeaders]['content-type'] = CONTENT_TYPE.JSON
    } else if (contentType.indexOf('charset') === -1) {
      // If user doesn't set charset, we will set charset to utf-8
      const customContentType = contentType.trim()
      if (customContentType.endsWith(';')) {
        // custom content-type is ended with ';'
        this[kReplyHeaders]['content-type'] = `${customContentType} charset=utf-8`
      } else {
        this[kReplyHeaders]['content-type'] = `${customContentType}; charset=utf-8`
      }
    }

    if (typeof payload !== 'string') {
      preSerializationHook(this, payload)
      return this
    }

Domain

Subdomains

Defined In

Frequently Asked Questions

What does send() do?
send() is a function in the fastify codebase, defined in lib/reply.js.
Where is send() defined?
send() is defined in lib/reply.js at line 127.
What does send() call?
send() calls 4 function(s): getHeader, onErrorHook, onSendHook, preSerializationHook.
What calls send()?
send() is called by 3 function(s): notFound, redirect, sendTrailer.

Analyze Your Own Codebase

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

Try Supermodel Free