@hugmanrique/ws-extensions
v0.0.2
Published
Sec-WebSocket-Extensions header parser and serializer
Downloads
26
Maintainers
Readme
:cyclone: ws-extensions
Sec-WebSocket-Extensions
header parser and serializer utils. This library aims to allow any WebSocket server implementation to parse the extensions supplied by the client decoupling the logic of parsing and checking the header value conforms to the ABNF specified in Section 9.1 of the WebSocket Protocol RFC.
Getting Started
Install ws-extensions using npm
:
npm install --save @hugmanrique/ws-extensions
Or via yarn
:
yarn add @hugmanrique/ws-extensions
The minimum supported Node version is v8.10.0
.
Let's get started by parsing a header:
import { parse } from '@hugmanrique/ws-extensions';
const header = 'a; b=1';
// [ { name: 'a', params: { b: 1 } } ]
parse(header);
Multiple complex offers support
ws-extensions
supports duplicate offers or params:
const header = 'a; b=1, c, b; d, c; e="hey, you"; e, a; b';
/*
[ { name: 'a', params: { b: 1 } },
{ name: 'c', params: {} },
{ name: 'b', params: { d: true } },
{ name: 'c', params: { e: ['hey, you', true] } },
{ name: 'a', params: { b: true } } ]
*/
parse(header);
Methods
parse(header)
Parses the Sec-WebSocket-Extensions
header into a JavaScript object.
- Returns an empty object if the header is empty.
- Throws a
SyntaxError
if the header is invalid. - Unescapes data (numbers and flags get converted to JS primitives)
- Supports duplicate offers
- Supports duplicate params (converts the param value to an Array)
serialize(name, params)
Serializes the params of an offer.