MCPcopy Create free account
hub / github.com/fastify/fastify / constructor

Method constructor

lib/content-type.js:81–160  ·  view source on GitHub ↗
(headerValue)

Source from the content-addressed store, hash-verified

79 }
80
81 constructor (headerValue) {
82 if (headerValue == null || headerValue === '' || headerValue === 'undefined') {
83 return
84 }
85
86 let sepIdx = headerValue.indexOf(';')
87 if (sepIdx === -1) {
88 // The value is the simplest `type/subtype` variant.
89 sepIdx = headerValue.indexOf('/')
90 if (sepIdx === -1) {
91 // Got a string without the correct `type/subtype` format.
92 return
93 }
94
95 const type = headerValue.slice(0, sepIdx).trimStart().toLowerCase()
96 const subtype = headerValue.slice(sepIdx + 1).trimEnd().toLowerCase()
97
98 if (
99 typeNameReg.test(type) === true &&
100 subtypeNameReg.test(subtype) === true
101 ) {
102 this.#valid = true
103 this.#empty = false
104 this.#type = type
105 this.#subtype = subtype
106 }
107
108 return
109 }
110
111 // We have a `type/subtype; params=list...` header value.
112 const mediaType = headerValue.slice(0, sepIdx).toLowerCase()
113 const paramsList = headerValue.slice(sepIdx + 1).trim()
114
115 sepIdx = mediaType.indexOf('/')
116 if (sepIdx === -1) {
117 // We got an invalid string like `something; params=list...`.
118 return
119 }
120 const type = mediaType.slice(0, sepIdx).trimStart()
121 const subtype = mediaType.slice(sepIdx + 1).trimEnd()
122
123 if (
124 typeNameReg.test(type) === false ||
125 subtypeNameReg.test(subtype) === false
126 ) {
127 // Some portion of the media-type is using invalid characters. Therefore,
128 // the content-type header is invalid.
129 return
130 }
131 this.#type = type
132 this.#subtype = subtype
133 this.#valid = true
134 this.#empty = false
135
136 let matches = keyValuePairsReg.exec(paramsList)
137 while (matches) {
138 // https://httpwg.org/specs/rfc9110.html#parameter

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected