raspberryGPIOhtmlserver/node_modules/connect/lib/middleware/session/session.js
node_modules/connect/lib/middleware/session/session.jsRaw
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 * Module dependencies.
11 */
12
13var utils = require('../../utils');
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
23var Session = module.exports = function Session(req, data) {
24 Object.defineProperty(this, 'req', { value: req });
25 Object.defineProperty(this, 'id', { value: req.sessionID });
26 if ('object' == typeof data) utils.merge(this, data);
27};
28
29/**
30 * Update reset `.cookie.maxAge` to prevent
31 * the cookie from expiring when the
32 * session is still active.
33 *
34 * @return {Session} for chaining
35 * @api public
36 */
37
38Session.prototype.touch = function(){
39 return this.resetMaxAge();
40};
41
42/**
43 * Reset `.maxAge` to `.originalMaxAge`.
44 *
45 * @return {Session} for chaining
46 * @api public
47 */
48
49Session.prototype.resetMaxAge = function(){
50 this.cookie.maxAge = this.cookie.originalMaxAge;
51 return this;
52};
53
54/**
55 * Save the session data with optional callback `fn(err)`.
56 *
57 * @param {Function} fn
58 * @return {Session} for chaining
59 * @api public
60 */
61
62Session.prototype.save = function(fn){
63 this.req.sessionStore.set(this.id, this, fn || function(){});
64 return this;
65};
66
67/**
68 * Re-loads the session data _without_ altering
69 * the maxAge properties. Invokes the callback `fn(err)`,
70 * after which time if no exception has occurred the
71 * `req.session` property will be a new `Session` object,
72 * although representing the same session.
73 *
74 * @param {Function} fn
75 * @return {Session} for chaining
76 * @api public
77 */
78
79Session.prototype.reload = function(fn){
80 var req = this.req
81 , store = this.req.sessionStore;
82 store.get(this.id, function(err, sess){
83 if (err) return fn(err);
84 if (!sess) return fn(new Error('failed to load session'));
85 store.createSession(req, sess);
86 fn();
87 });
88 return this;
89};
90
91/**
92 * Destroy `this` session.
93 *
94 * @param {Function} fn
95 * @return {Session} for chaining
96 * @api public
97 */
98
99Session.prototype.destroy = function(fn){
100 delete this.req.session;
101 this.req.sessionStore.destroy(this.id, fn);
102 return this;
103};
104
105/**
106 * Regenerate this request's session.
107 *
108 * @param {Function} fn
109 * @return {Session} for chaining
110 * @api public
111 */
112
113Session.prototype.regenerate = function(fn){
114 this.req.sessionStore.regenerate(this.req, fn);
115 return this;
116};