3 * Copyright(c) 2012-2014 Roman Shtylman
4 * Copyright(c) 2015 Douglas Christopher Wilson
15 exports.parse = parse;
16 exports.serialize = serialize;
23 var decode = decodeURIComponent;
24 var encode = encodeURIComponent;
25 var pairSplitRegExp = /; */;
28 * RegExp to match field-content in RFC 7230 sec 3.2
30 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
31 * field-vchar = VCHAR / obs-text
35 var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
38 * Parse a cookie header.
40 * Parse the given cookie header string into an object
41 * The object has the various cookies as keys(names) => values
44 * @param {object} [options]
49 function parse(str, options) {
50 if (typeof str !== 'string') {
51 throw new TypeError('argument str must be a string');
55 var opt = options || {};
56 var pairs = str.split(pairSplitRegExp);
57 var dec = opt.decode || decode;
59 for (var i = 0; i < pairs.length; i++) {
61 var eq_idx = pair.indexOf('=');
63 // skip things that don't look like key=value
68 var key = pair.substr(0, eq_idx).trim()
69 var val = pair.substr(++eq_idx, pair.length).trim();
73 val = val.slice(1, -1);
77 if (undefined == obj[key]) {
78 obj[key] = tryDecode(val, dec);
86 * Serialize data into a cookie header.
88 * Serialize the a name value pair into a cookie string suitable for
89 * http headers. An optional options object specified cookie parameters.
91 * serialize('foo', 'bar', { httpOnly: true })
92 * => "foo=bar; httpOnly"
94 * @param {string} name
96 * @param {object} [options]
101 function serialize(name, val, options) {
102 var opt = options || {};
103 var enc = opt.encode || encode;
105 if (typeof enc !== 'function') {
106 throw new TypeError('option encode is invalid');
109 if (!fieldContentRegExp.test(name)) {
110 throw new TypeError('argument name is invalid');
113 var value = enc(val);
115 if (value && !fieldContentRegExp.test(value)) {
116 throw new TypeError('argument val is invalid');
119 var str = name + '=' + value;
121 if (null != opt.maxAge) {
122 var maxAge = opt.maxAge - 0;
123 if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
124 str += '; Max-Age=' + Math.floor(maxAge);
128 if (!fieldContentRegExp.test(opt.domain)) {
129 throw new TypeError('option domain is invalid');
132 str += '; Domain=' + opt.domain;
136 if (!fieldContentRegExp.test(opt.path)) {
137 throw new TypeError('option path is invalid');
140 str += '; Path=' + opt.path;
144 if (typeof opt.expires.toUTCString !== 'function') {
145 throw new TypeError('option expires is invalid');
148 str += '; Expires=' + opt.expires.toUTCString();
160 var sameSite = typeof opt.sameSite === 'string'
161 ? opt.sameSite.toLowerCase() : opt.sameSite;
165 str += '; SameSite=Strict';
168 str += '; SameSite=Lax';
171 str += '; SameSite=Strict';
174 throw new TypeError('option sameSite is invalid');
182 * Try decoding a string using a decoding function.
184 * @param {string} str
185 * @param {function} decode
189 function tryDecode(str, decode) {