4 // Allowed token characters:
6 // '!', '#', '$', '%', '&', ''', '*', '+', '-',
7 // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
9 // tokenChars[32] === 0 // ' '
10 // tokenChars[33] === 1 // '!'
11 // tokenChars[34] === 0 // '"'
16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
17 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
18 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
19 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
20 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
21 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
22 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
23 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
27 * Adds an offer to the map of extension offers or a parameter to the map of
30 * @param {Object} dest The map of extension offers or parameters
31 * @param {String} name The extension or parameter name
32 * @param {(Object|Boolean|String)} elem The extension parameters or the
36 function push(dest, name, elem) {
37 if (dest[name] === undefined) dest[name] = [elem];
38 else dest[name].push(elem);
42 * Parses the `Sec-WebSocket-Extensions` header into an object.
44 * @param {String} header The field value of the header
45 * @return {Object} The parsed object
48 function parse(header) {
49 const offers = Object.create(null);
51 if (header === undefined || header === '') return offers;
53 let params = Object.create(null);
54 let mustUnescape = false;
55 let isEscaping = false;
63 for (; i < header.length; i++) {
64 const code = header.charCodeAt(i);
66 if (extensionName === undefined) {
67 if (end === -1 && tokenChars[code] === 1) {
68 if (start === -1) start = i;
69 } else if (code === 0x20 /* ' ' */ || code === 0x09 /* '\t' */) {
70 if (end === -1 && start !== -1) end = i;
71 } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
73 throw new SyntaxError(`Unexpected character at index ${i}`);
76 if (end === -1) end = i;
77 const name = header.slice(start, end);
79 push(offers, name, params);
80 params = Object.create(null);
87 throw new SyntaxError(`Unexpected character at index ${i}`);
89 } else if (paramName === undefined) {
90 if (end === -1 && tokenChars[code] === 1) {
91 if (start === -1) start = i;
92 } else if (code === 0x20 || code === 0x09) {
93 if (end === -1 && start !== -1) end = i;
94 } else if (code === 0x3b || code === 0x2c) {
96 throw new SyntaxError(`Unexpected character at index ${i}`);
99 if (end === -1) end = i;
100 push(params, header.slice(start, end), true);
102 push(offers, extensionName, params);
103 params = Object.create(null);
104 extensionName = undefined;
108 } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
109 paramName = header.slice(start, i);
112 throw new SyntaxError(`Unexpected character at index ${i}`);
116 // The value of a quoted-string after unescaping must conform to the
117 // token ABNF, so only token characters are valid.
118 // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
121 if (tokenChars[code] !== 1) {
122 throw new SyntaxError(`Unexpected character at index ${i}`);
124 if (start === -1) start = i;
125 else if (!mustUnescape) mustUnescape = true;
127 } else if (inQuotes) {
128 if (tokenChars[code] === 1) {
129 if (start === -1) start = i;
130 } else if (code === 0x22 /* '"' */ && start !== -1) {
133 } else if (code === 0x5c /* '\' */) {
136 throw new SyntaxError(`Unexpected character at index ${i}`);
138 } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
140 } else if (end === -1 && tokenChars[code] === 1) {
141 if (start === -1) start = i;
142 } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
143 if (end === -1) end = i;
144 } else if (code === 0x3b || code === 0x2c) {
146 throw new SyntaxError(`Unexpected character at index ${i}`);
149 if (end === -1) end = i;
150 let value = header.slice(start, end);
152 value = value.replace(/\\/g, '');
153 mustUnescape = false;
155 push(params, paramName, value);
157 push(offers, extensionName, params);
158 params = Object.create(null);
159 extensionName = undefined;
162 paramName = undefined;
165 throw new SyntaxError(`Unexpected character at index ${i}`);
170 if (start === -1 || inQuotes) {
171 throw new SyntaxError('Unexpected end of input');
174 if (end === -1) end = i;
175 const token = header.slice(start, end);
176 if (extensionName === undefined) {
177 push(offers, token, params);
179 if (paramName === undefined) {
180 push(params, token, true);
181 } else if (mustUnescape) {
182 push(params, paramName, token.replace(/\\/g, ''));
184 push(params, paramName, token);
186 push(offers, extensionName, params);
193 * Builds the `Sec-WebSocket-Extensions` header field value.
195 * @param {Object} extensions The map of extensions and parameters to format
196 * @return {String} A string representing the given object
199 function format(extensions) {
200 return Object.keys(extensions)
201 .map((extension) => {
202 let configurations = extensions[extension];
203 if (!Array.isArray(configurations)) configurations = [configurations];
204 return configurations
208 Object.keys(params).map((k) => {
209 let values = params[k];
210 if (!Array.isArray(values)) values = [values];
212 .map((v) => (v === true ? k : `${k}=${v}`))
223 module.exports = { format, parse };