Home / Class/ ContentType Class — fastify Architecture

ContentType Class — fastify Architecture

Architecture documentation for the ContentType class in content-type.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  478e364f_b024_741a_f140_dbb777da4660["ContentType"]
  2d973b63_f7cb_5642_9d0b_82bc3068b57d["content-type.js"]
  478e364f_b024_741a_f140_dbb777da4660 -->|defined in| 2d973b63_f7cb_5642_9d0b_82bc3068b57d
  81bd4f47_8692_0d06_3d47_2ff0e1df269f["constructor()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| 81bd4f47_8692_0d06_3d47_2ff0e1df269f
  2f42aa02_dde4_4721_5279_761bb192b61c["Symbol()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| 2f42aa02_dde4_4721_5279_761bb192b61c
  7d245f71_7e26_202b_05cb_348230a48fcd["isEmpty()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| 7d245f71_7e26_202b_05cb_348230a48fcd
  f64973d7_123c_8869_ecda_61f069abc015["isValid()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| f64973d7_123c_8869_ecda_61f069abc015
  ff1c10e7_1182_35bf_44b4_66a816b6aae0["mediaType()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| ff1c10e7_1182_35bf_44b4_66a816b6aae0
  7f3054f2_aaec_8f14_f9f1_ecde8e79c975["type()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| 7f3054f2_aaec_8f14_f9f1_ecde8e79c975
  814595dc_2528_c066_5c6a_204a2b716a30["subtype()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| 814595dc_2528_c066_5c6a_204a2b716a30
  08483fa0_b1a4_fb59_f134_d7f1bc6627d0["parameters()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| 08483fa0_b1a4_fb59_f134_d7f1bc6627d0
  83f1f999_592b_41ff_4a73_042315c5432a["toString()"]
  478e364f_b024_741a_f140_dbb777da4660 -->|method| 83f1f999_592b_41ff_4a73_042315c5432a

Relationship Graph

Source Code

lib/content-type.js lines 36–150

class ContentType {
  #valid = false
  #empty = true
  #type = ''
  #subtype = ''
  #parameters = new Map()
  #string

  constructor (headerValue) {
    if (headerValue == null || headerValue === '' || headerValue === 'undefined') {
      return
    }

    let sepIdx = headerValue.indexOf(';')
    if (sepIdx === -1) {
      // The value is the simplest `type/subtype` variant.
      sepIdx = headerValue.indexOf('/')
      if (sepIdx === -1) {
        // Got a string without the correct `type/subtype` format.
        return
      }

      const type = headerValue.slice(0, sepIdx).trimStart().toLowerCase()
      const subtype = headerValue.slice(sepIdx + 1).trimEnd().toLowerCase()

      if (
        typeNameReg.test(type) === true &&
        subtypeNameReg.test(subtype) === true
      ) {
        this.#valid = true
        this.#empty = false
        this.#type = type
        this.#subtype = subtype
      }

      return
    }

    // We have a `type/subtype; params=list...` header value.
    const mediaType = headerValue.slice(0, sepIdx).toLowerCase()
    const paramsList = headerValue.slice(sepIdx + 1).trim()

    sepIdx = mediaType.indexOf('/')
    if (sepIdx === -1) {
      // We got an invalid string like `something; params=list...`.
      return
    }
    const type = mediaType.slice(0, sepIdx).trimStart()
    const subtype = mediaType.slice(sepIdx + 1).trimEnd()

    if (
      typeNameReg.test(type) === false ||
      subtypeNameReg.test(subtype) === false
    ) {
      // Some portion of the media-type is using invalid characters. Therefore,
      // the content-type header is invalid.
      return
    }
    this.#type = type
    this.#subtype = subtype
    this.#valid = true
    this.#empty = false

    let matches = keyValuePairsReg.exec(paramsList)
    while (matches) {
      const key = matches[1]
      const value = matches[2]
      if (value[0] === '"') {
        if (value.at(-1) !== '"') {
          this.#parameters.set(key, 'invalid quoted string')
          matches = keyValuePairsReg.exec(paramsList)
          continue
        }
        // We should probably verify the value matches a quoted string
        // (https://httpwg.org/specs/rfc9110.html#rule.quoted-string) value.
        // But we are not really doing much with the parameter values, so we
        // are omitting that at this time.
        this.#parameters.set(key, value.slice(1, value.length - 1))
      } else {
        this.#parameters.set(key, value)
      }

Defined In

Frequently Asked Questions

What is the ContentType class?
ContentType is a class in the fastify codebase, defined in lib/content-type.js.
Where is ContentType defined?
ContentType is defined in lib/content-type.js at line 36.

Analyze Your Own Codebase

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

Try Supermodel Free