2 * Connect - session - Store
3 * Copyright(c) 2010 Sencha Inc.
4 * Copyright(c) 2011 TJ Holowaychuk
11 * Module dependencies.
15 var Cookie = require('./cookie')
16 var EventEmitter = require('events').EventEmitter
17 var Session = require('./session')
18 var util = require('util')
25 module.exports = Store
28 * Abstract base class for session stores.
33 EventEmitter.call(this)
37 * Inherit from EventEmitter.
40 util.inherits(Store, EventEmitter)
43 * Re-generate the given requests's session.
45 * @param {IncomingRequest} req
46 * @return {Function} fn
50 Store.prototype.regenerate = function(req, fn){
52 this.destroy(req.sessionID, function(err){
59 * Load a `Session` instance via the given `sid`
60 * and invoke the callback `fn(err, sess)`.
63 * @param {Function} fn
67 Store.prototype.load = function(sid, fn){
69 this.get(sid, function(err, sess){
70 if (err) return fn(err);
71 if (!sess) return fn();
72 var req = { sessionID: sid, sessionStore: self };
73 fn(null, self.createSession(req, sess))
78 * Create session from JSON `sess` data.
80 * @param {IncomingRequest} req
81 * @param {Object} sess
86 Store.prototype.createSession = function(req, sess){
87 var expires = sess.cookie.expires
88 var originalMaxAge = sess.cookie.originalMaxAge
90 sess.cookie = new Cookie(sess.cookie);
92 if (typeof expires === 'string') {
93 // convert expires to a Date object
94 sess.cookie.expires = new Date(expires)
97 // keep originalMaxAge intact
98 sess.cookie.originalMaxAge = originalMaxAge
100 req.session = new Session(req, sess);