Structured Headers

2.0.2 · active · verified Tue Apr 21

structured-headers is a JavaScript/TypeScript library providing a robust parser and serializer for HTTP Structured Field Values (RFC9651 and RFC8941). This specification standardizes complex HTTP header values, including lists, dictionaries, numbers, booleans, binary data (ByteSequences), timestamps, and Unicode strings (Display Strings). The current stable version is 2.0.2. New major versions are typically released to align with updates to the underlying RFCs, such as v2.0.0 which added support for RFC9651's new `Date` and `Display String` types. Patches address bug fixes and dependency updates. Key differentiators include its TypeScript foundation, shipping both ESM and CommonJS builds (though ESM is primary since v2), zero runtime dependencies, and an extensive test suite comprising 2805 unit tests largely sourced from the official HTTP Working Group. The library prioritizes strict RFC compliance, returning data structures that precisely mirror the specification, which can sometimes result in more complex types than developers might initially expect.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing various HTTP Structured Field Values: single items with parameters, lists including inner lists, and dictionaries with diverse member types.

import { parseItem, parseList, parseDictionary } from 'structured-headers';

// Parsing an 'Item' header
const itemHeader = '"Hello world"; a="5", @1686634251, :RE0gbWUgZm9yIGEgZnJlZSBjb29raWU=:';
console.log('Parsed Item:', parseItem('"Hello world"; a="5"'));
console.log('Parsed Date Item:', parseItem('@1686634251'));
console.log('Parsed Binary Item:', parseItem(':RE0gbWUgZm9yIGEgZnJlZSBjb29raWU=:'));

// Parsing a 'List' header
const listHeader = 'sometoken; param1; param2=hi, 42, (innerlistitem1 innerlistitem2)';
console.log('Parsed List:', JSON.stringify(parseList(listHeader), null, 2));

// Parsing a 'Dictionary' header
const dictHeader = 'fn="evert", ln="pot", coffee=?1, foo=(1 2 3)';
console.log('Parsed Dictionary:', JSON.stringify(Object.fromEntries(parseDictionary(dictHeader)), null, 2));

view raw JSON →