Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

structured-headers

evert4.9mMIT2.0.0TypeScript support: included

Implementation of Structured Field Values for HTTP (RFC9651, RFC8941)

http, structured-header, structured-fields, structured fields, RFC9651, RFC8941, headers

readme

Structured Headers parser for Javascript

This library implements a parser and serializer for the Structured Field Values for HTTP specification. (RFC9651, RFC8941).

This specification defines a standard serialization for complex HTTP header values, including lists (arrays), dictionaries (maps) and also numbers, booleans, binary data, timestamps and Unicode strings.

The library is written in Typescript, and the examples in this document are too, but plain Javascript is also fully supported. It ships with ESM and CommonJS builds and has 0 dependencies.

Compatibility

This package has 2805 unittests, the vast majority are supplied from the official HTTP WG test suite.

However, there are 2 differences in the serializer:

  1. Javascript can't differentiate between 1.0 and 1. As a result we're skipping the tests that require a serialiation output of 1.0.
  2. Javascript rounds slightly different from the spec. The tests suggest that 0.0025 should round to the nearest event number (0.002), but Javascript rounds to 0.003.

No fix is planned for #1, because there's no reasonably way to fix this without wrapping every number in a custom class, and this will negatively impact the developer experience. We do intend to fix #2 in the future with a custom rounding algorithm.

This library emits and expects the exact data structures as they are suggested by the RFC. The result of this is that the returned types can be a bit complex.

In the future we intend to loosen the required types for the serializer, and add new helper functions that give you simpler structures if you don't need certain features for a header (such as Parameters).

Let us know what you would like to see here!

Installation

Using npm:

npm install structured-headers

API

Parsing an item

The following are examples of item headers:

Parsed as string

# Parsed an ASCII string
Header: "foo"

# A simple string, called a 'Token' in the spec
Header: foo

# A Unicode string, called a 'Display String' in the spec. They use
# percent encoding, but encode a different set of characters than
# URLs.
Header %"Frysl%C3%A2n"

# Parsed as number
Header: 5
Header: -10
Header: 5.01415

# Parsed into boolean
Header: ?1
Header: ?0

# Binaries are base64 encoded
Header: :RE0gbWUgZm9yIGEgZnJlZSBjb29raWU=:

# Items can have parameters
Header: "Hello world"; a="5"

# Parsed into a Date object
Header: @1686634251

To parse these header values, use the parseItem:

import { parseItem } from 'structured-headers';

console.log(
  parseItem(header)
);

parseItem returns a tuple (array with 2 items), the first item is the value, the second is a Map object with parameters.

The type is roughly:

// The raw value
type BareItem = number | string | Token | ByteSequence | boolean | Date | DisplayString;

// The return type of parseItem
type Item = [
  BareItem,
  Map<string, BareItem>
];

Parsing a list

A list is an array of items. Some examples:

# A simple list
Header: 5, "foo", bar, ?1

# Each element can have parameters
Header: sometoken; param1; param2=hi, 42

# A list can also contain lists itself. These are called 'inner lists' and
# use parenthesis
Header: sometoken, (innerlistitem1 innerlistitem2), (anotherlist)

To parse these:

import { parseList } from 'structured-headers';

console.log(
  parseList(header)
);

parseList returns an array with each member. The return type is:

type InnerList = [Item[], Parameters];
type List = (InnerList|Item)[];

Parsing a dictionary

A dictionary is a key->value object. Examples:

# A simple dictionary
Header: fn="evert", ln="pot", coffee=?1

# Each item may have parameters too
Header: foo=123; q=1, bar=123, q=0.5

# A dictionary value may be an inner list again
Header: foo=(1 2 3)

To parse dictionaries:

import { parseDictionary } from 'structured-headers';

console.log(
  parseDictionary(header)
);

The return type for parseDictionary is a Map.

type Dictionary = Map<string, Item|InnerList>;

Serializing

The serialiser functions work the exact same way, but in opposite direction. They all return strings.

Currently the serializes expect the exact type that the parsers return, but the intention is to loosen the types for serialization, so it's a bit more ergnomic to call. Want this? Let me know by opening an issue.

import {
  serializeDictionary,
  serializeList,
  serializeItem
} from 'structured-headers';

// Returns "foo", "bar"
serializeList([
  ['foo', new Map()],
  ['bar', new Map()],
]);

// Returns a=1, b=?0
sh.serializeDictionary({
  a: 1,
  b: false,
});

// Returns 42
serializeItem(42);

// Returns 5.5
serializeItem(5.5);

// Returns "hello world"
serializeItem("hello world");

// Returns %"Frysl%C3%A2n"
serializeItem("Fryslân");

// Returns ?1
serializeItem(true);

// Returns a base-64 representation like: *aGVsbG8=*
serializeItem(new ByteSequence('aGVsbG8='));

// Returns a unix timestamp
serializeItem(new Date());

// Parameters to items can be passed as the second argument
// Returns "hello", q=5
serializeItem(
  "hello",
  new Map(['q', 5])
);

Browser support

There is a minified version of this library in the browser/ directory. This minified file will expose a global variable called 'structuredHeader' which contains the rest of the api.

changelog

ChangeLog

2.0.0 (2024-10-02)

The "Structured Field Values" was updated in RFC9651. This new specification added the 'Date' and 'Display String' field types. The former encodes unix timestamp, the latter a Unicode string.

Perfect time to update this package as well! This new major release supports the new standard.

  • 66: We now convert from/to ArrayBuffer instead of a custom ByteSequence

    object. This is a breaking change.
  • Add support for Date and DisplayString from RFC9651.
  • Switched to ESM, but we're still bundling a CommonJS build.
  • No longer shipping a minified build.
  • Dropped Chai and now using node:assert.
  • Dropped Mocha and now using node:test.

2.0.0-alpha.1 (2024-02-23)

  • Fixed exports value in package.json. (@CxRes)

2.0.0-alpha.0 (2024-01-29)

  • Support for a new Date and Display String types, from the new draft draft-ietf-httpbis-sfbis.
  • Simplified serializing Dictionaries and Items. The existing signatures still work, but the new API is a bit more natural and doesn't require wrapping everything in arrays and Maps.
  • Now requires Node 18.
  • Converted to ESM.
  • No longer providing a Webpack build. Most frontend applications already do their own bundling. Please let us know if you need this, so we can redo this with modern tools.

1.0.1 (2023-08-03)

  • This library emitted TypeError or a plain Error in a few places in the parser, where it should have been ParseError this is corrected everywhere now.

1.0.0 (2023-06-13)

  • This is mainly a re-release of 0.5.0. The package is stable and dependencies have been updated.
  • Dropped support for Node 12. The minimum Node version is now 14.

0.5.0 (2022-09-13)

  • All the serializer functions are now exported. (@adrianhopebailie)
  • Added an isByteSequence helper function (@adrianhopebailie)
  • Bring all dependencies up to date.

0.4.1 (2021-06-09)

  • Corrected the 'main' property in package.json.

0.4.0 (2021-05-15)

  • Fully up to date with RFC8941.
  • This is a complete rewrite, all APIs have changed and return the structures that are recommended by the actual RFC document.
  • Passing almost all tests from the HTTP WG test suite. See the readme for the exceptions.

0.3.0 (2019-10-03)

  • Fully up to date with draft-ietf-httpbis-header-structure-13.
  • Parameterized Lists and List of Lists are gone, their feautures are merged into List and Dictionaries.
  • Both lists and dictionaries now require an object such as {value: x, parameters: y}. This is a breaking change, but was required to support parameters correctly everywhere.
  • Stricter float parsing.

0.2.0 (2019-04-27)

  • Fully up to date with draft-ietf-httpbis-header-structure-10.
  • True and False are now encoded as ?1 and ?0.
  • Added serializing support.
  • Integers with more than 15 digits now error as per the new draft.
  • Updated all dependencies.

0.1.0 (2018-12-06)

  • Fully up to date with draft-ietf-httpbis-header-structure-09.
  • Package renamed to 'structured-headers'.
  • Conversion to typescript.
  • The parseBinary function is renamed to parseByteSequence, to match the rename in draft-ietf-httpbis-header-structure-08.
  • Support for Booleans.
  • The parseIdentifier function is renamed to parseToken, to match the rename in draft-ietf-httpbis-header-structure-09.
  • Renamed parseParameterizedList to parseParamList. It's shorter.

0.0.2 (2018-03-27)

  • Added minified webpacked version.
  • Added readme.
  • Fixed a small bug in identifier parsing.
  • 100% unittest coverage.

0.0.1 (2018-03-26)