MCPcopy Create free account
hub / github.com/nock/nock / headersInputToRawArray

Function headersInputToRawArray

lib/common.js:157–185  ·  view source on GitHub ↗

* Converts the various accepted formats of headers into a flat array representing "raw headers". * * Nock allows headers to be provided as a raw array, a plain object, or a Map. * * While all the header names are expected to be strings, the values are left intact as they can * be functions, str

(headers)

Source from the content-addressed store, hash-verified

155 * https://nodejs.org/api/http.html#http_message_rawheaders
156 */
157function headersInputToRawArray(headers) {
158 if (headers === undefined) {
159 return []
160 }
161
162 if (Array.isArray(headers)) {
163 // If the input is an array, assume it's already in the raw format and simply return a copy
164 // but throw an error if there aren't an even number of items in the array
165 if (headers.length % 2) {
166 throw new Error(
167 `Raw headers must be provided as an array with an even number of items. [fieldName, value, ...]`,
168 )
169 }
170 return [...headers]
171 }
172
173 // [].concat(...) is used instead of Array.flat until v11 is the minimum Node version
174 if (util.types.isMap(headers)) {
175 return [].concat(...Array.from(headers, ([k, v]) => [k.toString(), v]))
176 }
177
178 if (isPlainObject(headers)) {
179 return [].concat(...Object.entries(headers))
180 }
181
182 throw new Error(
183 `Headers must be provided as an array of raw values, a Map, or a plain Object. ${headers}`,
184 )
185}
186
187/**
188 * Converts an array of raw headers to an object, using the same rules as Nodes `http.IncomingMessage.headers`.

Callers

nothing calls this directly

Calls 1

isPlainObjectFunction · 0.85

Tested by

no test coverage detected