Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / express-session / session / session.js
1
2 /*!
3  * Connect - session - Session
4  * Copyright(c) 2010 Sencha Inc.
5  * Copyright(c) 2011 TJ Holowaychuk
6  * MIT Licensed
7  */
8
9 /**
10  * Expose Session.
11  */
12
13 module.exports = Session;
14
15 /**
16  * Create a new `Session` with the given request and `data`.
17  *
18  * @param {IncomingRequest} req
19  * @param {Object} data
20  * @api private
21  */
22
23 function Session(req, data) {
24   Object.defineProperty(this, 'req', { value: req });
25   Object.defineProperty(this, 'id', { value: req.sessionID });
26
27   if (typeof data === 'object' && data !== null) {
28     // merge data into this, ignoring prototype properties
29     for (var prop in data) {
30       if (!(prop in this)) {
31         this[prop] = data[prop]
32       }
33     }
34   }
35 }
36
37 /**
38  * Update reset `.cookie.maxAge` to prevent
39  * the cookie from expiring when the
40  * session is still active.
41  *
42  * @return {Session} for chaining
43  * @api public
44  */
45
46 Session.prototype.touch = function(){
47   return this.resetMaxAge();
48 };
49
50 /**
51  * Reset `.maxAge` to `.originalMaxAge`.
52  *
53  * @return {Session} for chaining
54  * @api public
55  */
56
57 Session.prototype.resetMaxAge = function(){
58   this.cookie.maxAge = this.cookie.originalMaxAge;
59   return this;
60 };
61
62 /**
63  * Save the session data with optional callback `fn(err)`.
64  *
65  * @param {Function} fn
66  * @return {Session} for chaining
67  * @api public
68  */
69
70 Session.prototype.save = function(fn){
71   this.req.sessionStore.set(this.id, this, fn || function(){});
72   return this;
73 };
74
75 /**
76  * Re-loads the session data _without_ altering
77  * the maxAge properties. Invokes the callback `fn(err)`,
78  * after which time if no exception has occurred the
79  * `req.session` property will be a new `Session` object,
80  * although representing the same session.
81  *
82  * @param {Function} fn
83  * @return {Session} for chaining
84  * @api public
85  */
86
87 Session.prototype.reload = function(fn){
88   var req = this.req
89     , store = this.req.sessionStore;
90   store.get(this.id, function(err, sess){
91     if (err) return fn(err);
92     if (!sess) return fn(new Error('failed to load session'));
93     store.createSession(req, sess);
94     fn();
95   });
96   return this;
97 };
98
99 /**
100  * Destroy `this` session.
101  *
102  * @param {Function} fn
103  * @return {Session} for chaining
104  * @api public
105  */
106
107 Session.prototype.destroy = function(fn){
108   delete this.req.session;
109   this.req.sessionStore.destroy(this.id, fn);
110   return this;
111 };
112
113 /**
114  * Regenerate this request's session.
115  *
116  * @param {Function} fn
117  * @return {Session} for chaining
118  * @api public
119  */
120
121 Session.prototype.regenerate = function(fn){
122   this.req.sessionStore.regenerate(this.req, fn);
123   return this;
124 };