Home / Function/ rawBody() — fastify Function Reference

rawBody() — fastify Function Reference

Architecture documentation for the rawBody() function in content-type-parser.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  4b3409ec_16d6_e0f9_062f_5a23bc15485a["rawBody()"]
  b47c11f3_fe3b_64dc_0f58_921e5921ec45["content-type-parser.js"]
  4b3409ec_16d6_e0f9_062f_5a23bc15485a -->|defined in| b47c11f3_fe3b_64dc_0f58_921e5921ec45
  2cc48675_3506_ea24_6a37_4a303bc97431["run()"]
  2cc48675_3506_ea24_6a37_4a303bc97431 -->|calls| 4b3409ec_16d6_e0f9_062f_5a23bc15485a
  style 4b3409ec_16d6_e0f9_062f_5a23bc15485a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/content-type-parser.js lines 233–304

function rawBody (request, reply, options, parser, done) {
  const asString = parser.asString === true
  const limit = options.limit === null ? parser.bodyLimit : options.limit
  const contentLength = Number(request.headers['content-length'])

  if (contentLength > limit) {
    done(new FST_ERR_CTP_BODY_TOO_LARGE(), undefined)
    return
  }

  let receivedLength = 0
  let body = asString ? '' : []
  const payload = request[kRequestPayloadStream] || request.raw

  if (asString) {
    payload.setEncoding('utf8')
  }

  payload.on('data', onData)
  payload.on('end', onEnd)
  payload.on('error', onEnd)
  payload.resume()

  function onData (chunk) {
    receivedLength += asString ? Buffer.byteLength(chunk) : chunk.length
    const { receivedEncodedLength = 0 } = payload
    // The resulting body length must not exceed bodyLimit (see "zip bomb").
    // The case when encoded length is larger than received length is rather theoretical,
    // unless the stream returned by preParsing hook is broken and reports wrong value.
    if (receivedLength > limit || receivedEncodedLength > limit) {
      payload.removeListener('data', onData)
      payload.removeListener('end', onEnd)
      payload.removeListener('error', onEnd)
      done(new FST_ERR_CTP_BODY_TOO_LARGE(), undefined)
      return
    }

    if (asString) {
      body += chunk
    } else {
      body.push(chunk)
    }
  }

  function onEnd (err) {
    payload.removeListener('data', onData)
    payload.removeListener('end', onEnd)
    payload.removeListener('error', onEnd)

    if (err != null) {
      if (!(typeof err.statusCode === 'number' && err.statusCode >= 400)) {
        err.statusCode = 400
      }
      done(err, undefined)
      return
    }

    if (!Number.isNaN(contentLength) && (payload.receivedEncodedLength || receivedLength) !== contentLength) {
      done(new FST_ERR_CTP_INVALID_CONTENT_LENGTH(), undefined)
      return
    }

    if (!asString) {
      body = Buffer.concat(body)
    }

    const result = parser.fn(request, body, done)
    if (result && typeof result.then === 'function') {
      result.then(body => { done(null, body) }, done)
    }
  }
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does rawBody() do?
rawBody() is a function in the fastify codebase, defined in lib/content-type-parser.js.
Where is rawBody() defined?
rawBody() is defined in lib/content-type-parser.js at line 233.
What calls rawBody()?
rawBody() is called by 1 function(s): run.

Analyze Your Own Codebase

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

Try Supermodel Free