source: trip-planner-front/node_modules/websocket-extensions/lib/parser.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.9 KB
Line 
1'use strict';
2
3var TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/,
4 NOTOKEN = /([^!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z])/g,
5 QUOTED = /"((?:\\[\x00-\x7f]|[^\x00-\x08\x0a-\x1f\x7f"\\])*)"/,
6 PARAM = new RegExp(TOKEN.source + '(?:=(?:' + TOKEN.source + '|' + QUOTED.source + '))?'),
7 EXT = new RegExp(TOKEN.source + '(?: *; *' + PARAM.source + ')*', 'g'),
8 EXT_LIST = new RegExp('^' + EXT.source + '(?: *, *' + EXT.source + ')*$'),
9 NUMBER = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/;
10
11var hasOwnProperty = Object.prototype.hasOwnProperty;
12
13var Parser = {
14 parseHeader: function(header) {
15 var offers = new Offers();
16 if (header === '' || header === undefined) return offers;
17
18 if (!EXT_LIST.test(header))
19 throw new SyntaxError('Invalid Sec-WebSocket-Extensions header: ' + header);
20
21 var values = header.match(EXT);
22
23 values.forEach(function(value) {
24 var params = value.match(new RegExp(PARAM.source, 'g')),
25 name = params.shift(),
26 offer = {};
27
28 params.forEach(function(param) {
29 var args = param.match(PARAM), key = args[1], data;
30
31 if (args[2] !== undefined) {
32 data = args[2];
33 } else if (args[3] !== undefined) {
34 data = args[3].replace(/\\/g, '');
35 } else {
36 data = true;
37 }
38 if (NUMBER.test(data)) data = parseFloat(data);
39
40 if (hasOwnProperty.call(offer, key)) {
41 offer[key] = [].concat(offer[key]);
42 offer[key].push(data);
43 } else {
44 offer[key] = data;
45 }
46 }, this);
47 offers.push(name, offer);
48 }, this);
49
50 return offers;
51 },
52
53 serializeParams: function(name, params) {
54 var values = [];
55
56 var print = function(key, value) {
57 if (value instanceof Array) {
58 value.forEach(function(v) { print(key, v) });
59 } else if (value === true) {
60 values.push(key);
61 } else if (typeof value === 'number') {
62 values.push(key + '=' + value);
63 } else if (NOTOKEN.test(value)) {
64 values.push(key + '="' + value.replace(/"/g, '\\"') + '"');
65 } else {
66 values.push(key + '=' + value);
67 }
68 };
69
70 for (var key in params) print(key, params[key]);
71
72 return [name].concat(values).join('; ');
73 }
74};
75
76var Offers = function() {
77 this._byName = {};
78 this._inOrder = [];
79};
80
81Offers.prototype.push = function(name, params) {
82 if (!hasOwnProperty.call(this._byName, name))
83 this._byName[name] = [];
84
85 this._byName[name].push(params);
86 this._inOrder.push({ name: name, params: params });
87};
88
89Offers.prototype.eachOffer = function(callback, context) {
90 var list = this._inOrder;
91 for (var i = 0, n = list.length; i < n; i++)
92 callback.call(context, list[i].name, list[i].params);
93};
94
95Offers.prototype.byName = function(name) {
96 return this._byName[name] || [];
97};
98
99Offers.prototype.toArray = function() {
100 return this._inOrder.slice();
101};
102
103module.exports = Parser;
Note: See TracBrowser for help on using the repository browser.