Gathering detailed insights and metrics for structured-headers
Gathering detailed insights and metrics for structured-headers
Gathering detailed insights and metrics for structured-headers
Gathering detailed insights and metrics for structured-headers
Parser for draft-ietf-httpbis-header-structure, structured headers for HTTP
npm install structured-headers
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
30 Stars
199 Commits
5 Forks
5 Watching
17 Branches
6 Contributors
Updated on 22 Oct 2024
TypeScript (61.88%)
JavaScript (35.76%)
Makefile (2.36%)
Cumulative downloads
Total Downloads
Last day
-5.8%
130,542
Compared to previous day
Last week
-1.1%
745,736
Compared to previous week
Last month
9.8%
3,143,443
Compared to previous month
Last year
68.1%
26,825,533
Compared to previous year
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.
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.0
and 1
. As a result we're
skipping the tests that require a serialiation output of 1.0
.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!
Using npm:
npm install structured-headers
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
:
1import { parseItem } from 'structured-headers'; 2 3console.log( 4 parseItem(header) 5);
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:
1// The raw value 2type BareItem = number | string | Token | ArrayBuffer | boolean | Date | DisplayString; 3 4// The return type of parseItem 5type Item = [ 6 BareItem, 7 Map<string, BareItem> 8];
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:
1import { parseList } from 'structured-headers'; 2 3console.log( 4 parseList(header) 5);
parseList
returns an array with each member. The return type is:
1type InnerList = [Item[], Parameters]; 2type List = (InnerList|Item)[];
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:
1import { parseDictionary } from 'structured-headers'; 2 3console.log( 4 parseDictionary(header) 5);
The return type for parseDictionary
is a Map
.
1type Dictionary = Map<string, Item|InnerList>;
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.
1import { 2 serializeDictionary, 3 serializeList, 4 serializeItem 5} from 'structured-headers'; 6 7// Returns "foo", "bar" 8serializeList([ 9 ['foo', new Map()], 10 ['bar', new Map()], 11]); 12 13// Returns a=1, b=?0 14sh.serializeDictionary({ 15 a: 1, 16 b: false, 17}); 18 19// Returns 42 20serializeItem(42); 21 2248,65,6C,6C,6F,2C,20,57,6F,72,6C,64,21. 23 2472, 101, 108, 108, 25 111, 32, 119, 111, 26 114, 108, 100 27 28 29// Returns 5.5 30serializeItem(5.5); 31 32// Returns "hello world" 33serializeItem("hello world"); 34 35// Returns %"Frysl%C3%A2n" 36serializeItem("Fryslân"); 37 38// Returns ?1 39serializeItem(true); 40 41// Returns a base-64 representation like: *aGVsbG8=* 42serializeItem( 43 new UInt8Array( 44 [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] 45 ).buffer 46); 47 48// Returns a unix timestamp 49serializeItem(new Date()); 50 51// Parameters to items can be passed as the second argument 52// Returns "hello", q=5 53serializeItem( 54 "hello", 55 new Map(['q', 5]) 56);
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.
No vulnerabilities found.
No security vulnerabilities found.
expo-structured-headers
Expo module implementation of a parser based on https://httpwg.org/specs/rfc8941.html
@ungap/structured-clone
A structuredClone polyfill
@smithy/middleware-stack
Provides a means for composing multiple middleware functions into a single handler
@cxres/structured-headers
Implementation of Structured Field Values for HTTP (RFC9651, RFC8941)