A small set of utility helpers exposed for third-party implementations of the
Dispatcher API. They cover the header-handling primitives undici uses
internally, so that custom dispatchers and handlers can normalize header names
and parse raw header lists exactly the way undici does.
import { util } from 'undici'
const { parseHeaders, headerNameToString } = utilparseHeaders
History
Header names supplied as a Buffer are accepted and normalized.
parseHeaders(headers, obj?): Record<Array><Record>Receives a flat list of raw header name/value pairs and returns them as an
object keyed by lowercased header name. Header names are normalized with
headerNameToString(), and Buffer values are decoded as latin1. When a
header name appears more than once, its values are collected into an array.
import { util } from 'undici'
const raw = ['Content-Type', 'text/plain', 'Set-Cookie', 'a=1', 'Set-Cookie', 'b=2']
console.log(util.parseHeaders(raw))
// { 'content-type': 'text/plain', 'set-cookie': [ 'a=1', 'b=2' ] }headerNameToString(value): string<string>Returns the lowercased form of a header name. The value may be provided as a
<string> or as a <Buffer>, in which case it is decoded as latin1. Common
header names are resolved through an internal lookup table for performance.
import { util } from 'undici'
console.log(util.headerNameToString('Content-Type'))
// 'content-type'
console.log(util.headerNameToString(Buffer.from('X-Custom-Header')))
// 'x-custom-header'