Home / Function/ constructor() — fastify Function Reference

constructor() — fastify Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  81bd4f47_8692_0d06_3d47_2ff0e1df269f["constructor()"]
  478e364f_b024_741a_f140_dbb777da4660["ContentType"]
  81bd4f47_8692_0d06_3d47_2ff0e1df269f -->|defined in| 478e364f_b024_741a_f140_dbb777da4660
  style 81bd4f47_8692_0d06_3d47_2ff0e1df269f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/content-type.js lines 44–119

  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)
      }
      matches = keyValuePairsReg.exec(paramsList)
    }
  }

Domain

Subdomains

Defined In

Frequently Asked Questions

What does constructor() do?
constructor() is a function in the fastify codebase, defined in lib/content-type.js.
Where is constructor() defined?
constructor() is defined in lib/content-type.js at line 44.

Analyze Your Own Codebase

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

Try Supermodel Free