arnaucube
arnaucube/comunicationLeapPublic Self-hosted repository
comunicationLeap/node_modules/cookie/index.js
node_modules/cookie/index.jsRaw
 1
 2/// Serialize the a name value pair into a cookie string suitable for
 3/// http headers. An optional options object specified cookie parameters
 4///
 5/// serialize('foo', 'bar', { httpOnly: true })
 6///   => "foo=bar; httpOnly"
 7///
 8/// @param {String} name
 9/// @param {String} val
10/// @param {Object} options
11/// @return {String}
12var serialize = function(name, val, opt){
13    opt = opt || {};
14    var enc = opt.encode || encode;
15    var pairs = [name + '=' + enc(val)];
16
17    if (opt.maxAge) pairs.push('Max-Age=' + opt.maxAge);
18    if (opt.domain) pairs.push('Domain=' + opt.domain);
19    if (opt.path) pairs.push('Path=' + opt.path);
20    if (opt.expires) pairs.push('Expires=' + opt.expires.toUTCString());
21    if (opt.httpOnly) pairs.push('HttpOnly');
22    if (opt.secure) pairs.push('Secure');
23
24    return pairs.join('; ');
25};
26
27/// Parse the given cookie header string into an object
28/// The object has the various cookies as keys(names) => values
29/// @param {String} str
30/// @return {Object}
31var parse = function(str, opt) {
32    opt = opt || {};
33    var obj = {}
34    var pairs = str.split(/[;,] */);
35    var dec = opt.decode || decode;
36
37    pairs.forEach(function(pair) {
38        var eq_idx = pair.indexOf('=')
39
40        // skip things that don't look like key=value
41        if (eq_idx < 0) {
42            return;
43        }
44
45        var key = pair.substr(0, eq_idx).trim()
46        var val = pair.substr(++eq_idx, pair.length).trim();
47
48        // quoted values
49        if ('"' == val[0]) {
50            val = val.slice(1, -1);
51        }
52
53        // only assign once
54        if (undefined == obj[key]) {
55            try {
56                obj[key] = dec(val);
57            } catch (e) {
58                obj[key] = val;
59            }
60        }
61    });
62
63    return obj;
64};
65
66var encode = encodeURIComponent;
67var decode = decodeURIComponent;
68
69module.exports.serialize = serialize;
70module.exports.parse = parse;