raspberryGPIOhtmlserver/node_modules/connect/lib/middleware/session/cookie.js
node_modules/connect/lib/middleware/session/cookie.jsRaw
1
2/*!
3 * Connect - session - Cookie
4 * Copyright(c) 2010 Sencha Inc.
5 * Copyright(c) 2011 TJ Holowaychuk
6 * MIT Licensed
7 */
8
9/**
10 * Module dependencies.
11 */
12
13var utils = require('../../utils')
14 , cookie = require('cookie');
15
16/**
17 * Initialize a new `Cookie` with the given `options`.
18 *
19 * @param {IncomingMessage} req
20 * @param {Object} options
21 * @api private
22 */
23
24var Cookie = module.exports = function Cookie(options) {
25 this.path = '/';
26 this.maxAge = null;
27 this.httpOnly = true;
28 if (options) utils.merge(this, options);
29 this.originalMaxAge = undefined == this.originalMaxAge
30 ? this.maxAge
31 : this.originalMaxAge;
32};
33
34/*!
35 * Prototype.
36 */
37
38Cookie.prototype = {
39
40 /**
41 * Set expires `date`.
42 *
43 * @param {Date} date
44 * @api public
45 */
46
47 set expires(date) {
48 this._expires = date;
49 this.originalMaxAge = this.maxAge;
50 },
51
52 /**
53 * Get expires `date`.
54 *
55 * @return {Date}
56 * @api public
57 */
58
59 get expires() {
60 return this._expires;
61 },
62
63 /**
64 * Set expires via max-age in `ms`.
65 *
66 * @param {Number} ms
67 * @api public
68 */
69
70 set maxAge(ms) {
71 this.expires = 'number' == typeof ms
72 ? new Date(Date.now() + ms)
73 : ms;
74 },
75
76 /**
77 * Get expires max-age in `ms`.
78 *
79 * @return {Number}
80 * @api public
81 */
82
83 get maxAge() {
84 return this.expires instanceof Date
85 ? this.expires.valueOf() - Date.now()
86 : this.expires;
87 },
88
89 /**
90 * Return cookie data object.
91 *
92 * @return {Object}
93 * @api private
94 */
95
96 get data() {
97 return {
98 originalMaxAge: this.originalMaxAge
99 , expires: this._expires
100 , secure: this.secure
101 , httpOnly: this.httpOnly
102 , domain: this.domain
103 , path: this.path
104 }
105 },
106
107 /**
108 * Check if the cookie has a reasonably large max-age.
109 *
110 * @return {Boolean}
111 * @api private
112 */
113
114 get hasLongExpires() {
115 var week = 604800000;
116 return this.maxAge > (4 * week);
117 },
118
119 /**
120 * Return a serialized cookie string.
121 *
122 * @return {String}
123 * @api public
124 */
125
126 serialize: function(name, val){
127 return cookie.serialize(name, val, this.data);
128 },
129
130 /**
131 * Return JSON representation of this cookie.
132 *
133 * @return {Object}
134 * @api private
135 */
136
137 toJSON: function(){
138 return this.data;
139 }
140};