]> git.cworth.org Git - obsolete/notmuch-web/blob - node_modules/express/node_modules/connect/lib/middleware/session/memory.js
Install the "express" node module via npm
[obsolete/notmuch-web] / node_modules / express / node_modules / connect / lib / middleware / session / memory.js
1
2 /*!
3  * Connect - session - MemoryStore
4  * Copyright(c) 2010 Sencha Inc.
5  * Copyright(c) 2011 TJ Holowaychuk
6  * MIT Licensed
7  */
8
9 /**
10  * Module dependencies.
11  */
12
13 var Store = require('./store')
14   , utils = require('../../utils')
15   , Session = require('./session');
16
17 /**
18  * Initialize a new `MemoryStore`.
19  *
20  * @api public
21  */
22
23 var MemoryStore = module.exports = function MemoryStore() {
24   this.sessions = {};
25 };
26
27 /**
28  * Inherit from `Store.prototype`.
29  */
30
31 MemoryStore.prototype.__proto__ = Store.prototype;
32
33 /**
34  * Attempt to fetch session by the given `sid`.
35  *
36  * @param {String} sid
37  * @param {Function} fn
38  * @api public
39  */
40
41 MemoryStore.prototype.get = function(sid, fn){
42   var self = this;
43   process.nextTick(function(){
44     var expires
45       , sess = self.sessions[sid];
46     if (sess) {
47       sess = JSON.parse(sess);
48       expires = 'string' == typeof sess.cookie.expires
49         ? new Date(sess.cookie.expires)
50         : sess.cookie.expires;
51       if (!expires || new Date < expires) {
52         fn(null, sess);
53       } else {
54         self.destroy(sid, fn);
55       }
56     } else {
57       fn();
58     }
59   });
60 };
61
62 /**
63  * Commit the given `sess` object associated with the given `sid`.
64  *
65  * @param {String} sid
66  * @param {Session} sess
67  * @param {Function} fn
68  * @api public
69  */
70
71 MemoryStore.prototype.set = function(sid, sess, fn){
72   var self = this;
73   process.nextTick(function(){
74     self.sessions[sid] = JSON.stringify(sess);
75     fn && fn();
76   });
77 };
78
79 /**
80  * Destroy the session associated with the given `sid`.
81  *
82  * @param {String} sid
83  * @api public
84  */
85
86 MemoryStore.prototype.destroy = function(sid, fn){
87   var self = this;
88   process.nextTick(function(){
89     delete self.sessions[sid];
90     fn && fn();
91   });
92 };
93
94 /**
95  * Invoke the given callback `fn` with all active sessions.
96  *
97  * @param {Function} fn
98  * @api public
99  */
100
101 MemoryStore.prototype.all = function(fn){
102   var arr = []
103     , keys = Object.keys(this.sessions);
104   for (var i = 0, len = keys.length; i < len; ++i) {
105     arr.push(this.sessions[keys[i]]);
106   }
107   fn(null, arr);
108 };
109
110 /**
111  * Clear all sessions.
112  *
113  * @param {Function} fn
114  * @api public
115  */
116
117 MemoryStore.prototype.clear = function(fn){
118   this.sessions = {};
119   fn && fn();
120 };
121
122 /**
123  * Fetch number of sessions.
124  *
125  * @param {Function} fn
126  * @api public
127  */
128
129 MemoryStore.prototype.length = function(fn){
130   fn(null, Object.keys(this.sessions).length);
131 };