142
St
startsInvasion / files
startsInvasion / node_modules / connect / lib / middleware / session / session.js
2.7 KB
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 , Cookie = require('./cookie'); 15 16/** 17 * Create a new `Session` with the given request and `data`. 18 * 19 * @param {IncomingRequest} req 20 * @param {Object} data 21 * @api private 22 */ 23 24var Session = module.exports = function Session(req, data) { 25 Object.defineProperty(this, 'req', { value: req }); 26 Object.defineProperty(this, 'id', { value: req.sessionID }); 27 if ('object' == typeof data) { 28 utils.merge(this, data); 29 } else { 30 this.lastAccess = Date.now(); 31 } 32}; 33 34/** 35 * Update `.lastAccess` timestamp, 36 * and reset `.cookie.maxAge` to prevent 37 * the cookie from expiring when the 38 * session is still active. 39 * 40 * @return {Session} for chaining 41 * @api public 42 */ 43 44Session.prototype.touch = function(){ 45 return this 46 .resetLastAccess() 47 .resetMaxAge(); 48}; 49 50/** 51 * Update `.lastAccess` timestamp. 52 * 53 * @return {Session} for chaining 54 * @api public 55 */ 56 57Session.prototype.resetLastAccess = function(){ 58 this.lastAccess = Date.now(); 59 return this; 60}; 61 62/** 63 * Reset `.maxAge` to `.originalMaxAge`. 64 * 65 * @return {Session} for chaining 66 * @api public 67 */ 68 69Session.prototype.resetMaxAge = function(){ 70 this.cookie.maxAge = this.cookie.originalMaxAge; 71 return this; 72}; 73 74/** 75 * Save the session data with optional callback `fn(err)`. 76 * 77 * @param {Function} fn 78 * @return {Session} for chaining 79 * @api public 80 */ 81 82Session.prototype.save = function(fn){ 83 this.req.sessionStore.set(this.id, this, fn || function(){}); 84 return this; 85}; 86 87/** 88 * Re-loads the session data _without_ altering 89 * the maxAge or lastAccess properties. Invokes the 90 * callback `fn(err)`, after which time if no exception 91 * has occurred the `req.session` property will be 92 * a new `Session` object, although representing the 93 * same session. 94 * 95 * @param {Function} fn 96 * @return {Session} for chaining 97 * @api public 98 */ 99 100Session.prototype.reload = function(fn){ 101 var req = this.req 102 , store = this.req.sessionStore; 103 store.get(this.id, function(err, sess){ 104 if (err) return fn(err); 105 if (!sess) return fn(new Error('failed to load session')); 106 store.createSession(req, sess); 107 fn(); 108 }); 109 return this; 110}; 111 112/** 113 * Destroy `this` session. 114 * 115 * @param {Function} fn 116 * @return {Session} for chaining 117 * @api public 118 */ 119 120Session.prototype.destroy = function(fn){ 121 delete this.req.session; 122 this.req.sessionStore.destroy(this.id, fn); 123 return this; 124}; 125 126/** 127 * Regenerate this request's session. 128 * 129 * @param {Function} fn 130 * @return {Session} for chaining 131 * @api public 132 */ 133 134Session.prototype.regenerate = function(fn){ 135 this.req.sessionStore.regenerate(this.req, fn); 136 return this; 137};