source: trip-planner-front/node_modules/cookie/index.js@ eed0bf8

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

initial commit

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*!
2 * cookie
3 * Copyright(c) 2012-2014 Roman Shtylman
4 * Copyright(c) 2015 Douglas Christopher Wilson
5 * MIT Licensed
6 */
7
8'use strict';
9
10/**
11 * Module exports.
12 * @public
13 */
14
15exports.parse = parse;
16exports.serialize = serialize;
17
18/**
19 * Module variables.
20 * @private
21 */
22
23var decode = decodeURIComponent;
24var encode = encodeURIComponent;
25var pairSplitRegExp = /; */;
26
27/**
28 * RegExp to match field-content in RFC 7230 sec 3.2
29 *
30 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
31 * field-vchar = VCHAR / obs-text
32 * obs-text = %x80-FF
33 */
34
35var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
36
37/**
38 * Parse a cookie header.
39 *
40 * Parse the given cookie header string into an object
41 * The object has the various cookies as keys(names) => values
42 *
43 * @param {string} str
44 * @param {object} [options]
45 * @return {object}
46 * @public
47 */
48
49function parse(str, options) {
50 if (typeof str !== 'string') {
51 throw new TypeError('argument str must be a string');
52 }
53
54 var obj = {}
55 var opt = options || {};
56 var pairs = str.split(pairSplitRegExp);
57 var dec = opt.decode || decode;
58
59 for (var i = 0; i < pairs.length; i++) {
60 var pair = pairs[i];
61 var eq_idx = pair.indexOf('=');
62
63 // skip things that don't look like key=value
64 if (eq_idx < 0) {
65 continue;
66 }
67
68 var key = pair.substr(0, eq_idx).trim()
69 var val = pair.substr(++eq_idx, pair.length).trim();
70
71 // quoted values
72 if ('"' == val[0]) {
73 val = val.slice(1, -1);
74 }
75
76 // only assign once
77 if (undefined == obj[key]) {
78 obj[key] = tryDecode(val, dec);
79 }
80 }
81
82 return obj;
83}
84
85/**
86 * Serialize data into a cookie header.
87 *
88 * Serialize the a name value pair into a cookie string suitable for
89 * http headers. An optional options object specified cookie parameters.
90 *
91 * serialize('foo', 'bar', { httpOnly: true })
92 * => "foo=bar; httpOnly"
93 *
94 * @param {string} name
95 * @param {string} val
96 * @param {object} [options]
97 * @return {string}
98 * @public
99 */
100
101function serialize(name, val, options) {
102 var opt = options || {};
103 var enc = opt.encode || encode;
104
105 if (typeof enc !== 'function') {
106 throw new TypeError('option encode is invalid');
107 }
108
109 if (!fieldContentRegExp.test(name)) {
110 throw new TypeError('argument name is invalid');
111 }
112
113 var value = enc(val);
114
115 if (value && !fieldContentRegExp.test(value)) {
116 throw new TypeError('argument val is invalid');
117 }
118
119 var str = name + '=' + value;
120
121 if (null != opt.maxAge) {
122 var maxAge = opt.maxAge - 0;
123
124 if (isNaN(maxAge) || !isFinite(maxAge)) {
125 throw new TypeError('option maxAge is invalid')
126 }
127
128 str += '; Max-Age=' + Math.floor(maxAge);
129 }
130
131 if (opt.domain) {
132 if (!fieldContentRegExp.test(opt.domain)) {
133 throw new TypeError('option domain is invalid');
134 }
135
136 str += '; Domain=' + opt.domain;
137 }
138
139 if (opt.path) {
140 if (!fieldContentRegExp.test(opt.path)) {
141 throw new TypeError('option path is invalid');
142 }
143
144 str += '; Path=' + opt.path;
145 }
146
147 if (opt.expires) {
148 if (typeof opt.expires.toUTCString !== 'function') {
149 throw new TypeError('option expires is invalid');
150 }
151
152 str += '; Expires=' + opt.expires.toUTCString();
153 }
154
155 if (opt.httpOnly) {
156 str += '; HttpOnly';
157 }
158
159 if (opt.secure) {
160 str += '; Secure';
161 }
162
163 if (opt.sameSite) {
164 var sameSite = typeof opt.sameSite === 'string'
165 ? opt.sameSite.toLowerCase() : opt.sameSite;
166
167 switch (sameSite) {
168 case true:
169 str += '; SameSite=Strict';
170 break;
171 case 'lax':
172 str += '; SameSite=Lax';
173 break;
174 case 'strict':
175 str += '; SameSite=Strict';
176 break;
177 case 'none':
178 str += '; SameSite=None';
179 break;
180 default:
181 throw new TypeError('option sameSite is invalid');
182 }
183 }
184
185 return str;
186}
187
188/**
189 * Try decoding a string using a decoding function.
190 *
191 * @param {string} str
192 * @param {function} decode
193 * @private
194 */
195
196function tryDecode(str, decode) {
197 try {
198 return decode(str);
199 } catch (e) {
200 return str;
201 }
202}
Note: See TracBrowser for help on using the repository browser.