Merge branch 'async-session' into integration
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents 7606f1b, 655ad77 · Browse files · 6 files·+123 added−60 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+123 added−60 removed
- tests:+35 added−8 removed
- other:+88 added−52 removed
Provenance
- Commit
e0ef616- Tree
953f03b- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
7606f1b- Parent 2
655ad77
6 files changed+123 added−60 removed
modified:examples/chat/app.js+15 added-9 removed
19 unmodified lines202122232425262728242526272829303132293031323334353637383940414240 unmodified lines19 unmodified linesget('/', function(){ this.pass('/chat')})
get('/chat', function(){ var self= this; //TODO: The current memory session store only persists sessions // on response completion *AFTER* this method completes so // the length will be out by 1 for each new session created.get('/chat', function(){ this.render('chat.haml.html', { locals: { title: 'Chat', messages: messages, name: this.session.name, usersOnline: Session.store.length() } }) Session.store.length(function(error, length){ self.render('chat.haml.html', { locals: { title: 'Chat', messages: messages, name: self.session.name, usersOnline: length } }) });})
post('/chat', function(){ this.session.name = this.param('name')40 unmodified linesmodified:lib/express/plugins/cache.js+14 added-12 removed
82 unmodified lines83 * Set the given _key_ to _val_.84 *85 * @param {string} key86 * @param {string} val87- * @param {function} fn87+ * @param {function} callback88 * @return {string}89 * @api public90 */91 92- set: function(key, val, fn) {92+ set: function(key, val, callback) {93 exports.Store.prototype.set.apply(this, arguments)94 this.data[key] = new Cache(key, val)95- if (fn instanceof Function) fn(val)95+ if (callback) callback(val)96 },97 98 /**99 * Get data found matching the given _key_.8 unmodified lines108 * 'page:users': '<html>...</html>',109 * ... }110 *111 * @param {string} key112- * @param {function} fn112+ * @param {function} callback113 * @return {mixed}114 * @api public115 */116 117- get: function(key, fn) {117+ get: function(key, callback) {118 exports.Store.prototype.get.apply(this, arguments)119 if (key.indexOf('*') === -1)120- return fn(this.data[key] instanceof Cache120+ return callback(this.data[key] instanceof Cache121 ? this.data[key].val122 : null)123 var regexp = this.normalize(key) 124- fn(this.data.reduce(function(vals, cache){124+ callback(this.data.reduce(function(vals, cache){125 if (regexp.test(cache.key))126 vals[cache.key] = cache.val127 return vals128 }, {}))7 unmodified lines136 * cache.clear('page:front', function(){})137 * cache.clear('page:*', function(){})138 *139 * @param {string} key140- * @param {function} fn140+ * @param {function} callback141 * @api public142 */143 144- clear: function(key, fn) {144+ clear: function(key, callback) {145 if (key.indexOf('*') === -1)146 delete this.data[key]147 else {148 var regexp = this.normalize(key)1 unmodified line150 if (this.data.hasOwnProperty(key))151 if (regexp.test(key))152 delete this.data[key]153 }154- fn()154+ callback()155 },156 157 /**158 * Reap caches older than _ms_.1 unmodified line160 * @param {int} ms161 * @api private162 */163 164- reap: function(ms) {164+ reap: function(ms, callback) {165 var self = this,166 threshold = Number(new Date(Number(new Date) - ms))167 this.data.each(function(cache){168 if (cache.created < threshold)169 self.clear(cache.key, function(){})170 })171+ if (callback) callback()172 },173 174 /**175 * Convert the given key matching _pattern_ 38 unmodified lines214 215 /**216 * Start reaper.217 *218+ * @param {function} callback219 * @api private220 */221 220- startReaper: function() {222+ startReaper: function(callbac) {223 setInterval(function(self){224 self.store.reap(self.lifetime || (1).day)225 }, this.reapInterval || this.reapEvery || (1).hour, this)226 }2 unmodified linesmodified:lib/express/plugins/session.js+57 added-25 removed
7 unmodified lines8 var utils = require('express/utils')9 10 // --- Session11 12-var Session = new Class({12+exports.Base = new Class({13 14 /**15 * Initialize session _sid_.16 */49 unmodified lines66 },67 68 /**69 * Fetch session with the given _sid_ or 70- * a new Session is returned.70+ * a new Session is created.71 *72- * @param {int} sid73- * @return {Session}72+ * @param {number} sid73+ * @param {function} callback74 * @api private75 */76 77- fetch: function(sid) {78- return this.store[sid] || new Session(sid)77+ fetch: function(sid, callback) {78+ if (sid && this.store[sid])79+ callback(null, this.store[sid])80+ else81+ this.generate(callback)82 },83 84 /**85 * Commit _session_ data.86 *87 * @param {Session} session88+ * @param {function} callback89 * @api private90 */91 88- commit: function(session) {89- return this.store[session.id] = session92+ commit: function(session, callback) {93+ this.store[session.id] = session 94+ if (callback) callback(null, session)95 },96 97 /**98 * Clear all sessions.99 *100+ * @param {function} callback101 * @api public102 */103 98- clear: function() {104+ clear: function(callback) {105 this.store = {}106+ if (callback) callback()107 },108 109 /**110 * Destroy session using the given _sid_.111 *112 * @param {int} sid113+ * @param {function} callback114 * @api public115 */116 109- destroy: function(sid) {117+ destroy: function(sid, callback) {118 delete this.store[sid]119+ if (callback) callback(sid)120 },121 122 /**114- * Return the number of sessions currently stored.123+ * Pass the number of sessions currently stored.124 *116- * @return {int}125+ * @param {function} callback126 * @api public127 */128 120- length: function() {121- return this.store.values.length129+ length: function(callback) {130+ callback(null, this.store.values.length)131 },132 133 /**134 * Reap sessions older than _ms_.135 *136 * @param {int} ms137+ * @param {function} callback138 * @api private139 */140 131- reap: function(ms) {141+ reap: function(ms, callback) {142 var threshold = Number(new Date(Number(new Date) - ms))143 this.store.each(function(session, sid){144 if (session.lastAccess < threshold)145 this.destroy(sid)146 }, this)147+ if (callback) callback()148+ },149+150+ /**151+ * Creates and passes a shiny new session.152+ *153+ * @param {function} callback154+ * @api public155+ */156+ 157+ generate: function(callback) {158+ callback(null, new exports.Base(utils.uid()))159 }160 })161 162 // --- Session26 unmodified lines189 * @api private190 */191 192 startReaper: function() {171- setInterval(function(self){172- self.store.reap(self.lifetime || (1).day)193+ setTimeout(function(self) {194+ self.store.reap(self.lifetime || (1).day) 195 }, this.reapInterval || this.reapEvery || (1).hour, this)196 }197 },198 4 unmodified lines203 /**204 * Create session id when not found; delegate to store.205 */206 185- request: function(event) {186- var sid187- if (!(sid = event.request.cookie('sid')))188- event.request.cookie('sid', sid = utils.uid(), set('session cookie'))189- event.request.session = exports.Session.store.fetch(sid)190- event.request.session.touch()207+ request: function(event, next) {208+ var sid= event.request.cookie('sid');209+ exports.Session.store.fetch(sid, function(error, session) {210+ if( error ) next(error);211+ else {212+ // If an invalid session id was passed then the id can change213+ if( session.id != sid ) 214+ event.request.cookie('sid',session.id, set('session cookie'))215+ 216+ event.request.session= session;217+ event.request.session.touch();218+ next(); 219+ }220+ }); 221+ return true;222 },223 224 /**225 * Delegate to store, allowing it to save sessions changes.226 */227 197- response: function(event) {198- exports.Session.store.commit(event.request.session)228+ response: function(event, next) {229+ exports.Session.store.commit(event.request.session, next) 230+ return true;231 }232 }233 })modified:lib/express/request.js+2 added-6 removed
240 unmodified lines241 ;(function next(e) {242 if (e || complete === total)243 callback(e)244 else {245- try {246- if (self.plugins.at(complete++).trigger(new Event(name, data), next) !== true)247- next()245+ if (self.plugins.at(complete++).trigger(new Event(name, data), next) !== true)246+ next()248- } catch(e) {249- next(e)250- }247 }248 })()249 return this250 },30 unmodified linesmodified:spec/spec.plugins.flash.js+2 added
4 unmodified lines56789101112131464 unmodified lines4 unmodified lines use(require('express/plugins/cookie').Cookie) use(require('express/plugins/session').Session) use(require('express/plugins/flash').Flash) Session.store.clear() var sess = new Base(123) Session.store.commit(sess, function(){}) end describe 'Flash' describe '#flash()'64 unmodified linesmodified:spec/spec.plugins.session.js+33 added-8 removed
2 unmodified lines3 before_each4 reset()5 use(require('express/plugins/cookie').Cookie)6 use(Session = require('express/plugins/session').Session)7+ Base = require('express/plugins/session').Base8 Session.store.clear()9 end10 11 describe 'Session'2 unmodified lines14 get('/login', function(){ return '' })15 get('/login').headers['set-cookie'].should.match(/^sid=(\w+);/)16 end17 end17- 18- describe 'when sid cookie is present'18+19+ describe 'when existing sid cookie is present'20 it 'should not set sid'21+ Session.store.commit(new Base(123))22 get('/login', function(){ return '' })23 get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'set-cookie'24 end25 end24- 26+ 27+ describe 'when unknown sid cookie is present'28+ it 'should set new sid'29+ get('/login', function(){ return '' })30+ var headers= get('/login', { headers: { cookie: 'sid=123' }}).headers31+ 32+ headers.should.have_property 'set-cookie'33+ headers['set-cookie'].should.not.be '123'34+ end35+ end 36+37 describe 'session Store.Memory'38 before_each27- memory = new (require('express/plugins/session').Store.Memory)39+ Session.store= memory = new (require('express/plugins/session').Store.Memory)40 end41 42 it 'should persist'43 post('/login', function(){1 unmodified line45 })46 get('/login', function(){47 return this.session.name48 })49+ memory.commit(new Base(123))50 var headers = { headers: { cookie: 'sid=123' }}51 post('/login', headers)52 get('/login', headers).status.should.eql 20053 get('/login', headers).body.should.eql 'tj'7 unmodified lines61 62 describe '#fetch()'63 describe 'when the session does not exist'64 it 'should return a new Session'65+ var result66+ memory.fetch('1', function(error,session){67+ result= session68+ })52- memory.fetch('1').should.have_property 'lastAccess'69+ result.should.have_property 'lastAccess'70 end71 end72 73 describe 'when the session does exist'74 it 'should return the previous session'58- memory.commit({ id: '1', same: true })59- memory.fetch('1').should.have_property 'same', true75+ var result76+ memory.commit(new Base('1'))77+ memory.fetch('1', function(error, session){78+ result = session79+ })80+ result.id.should.eql '1'81 end82 end83 end84 8 unmodified lines93 end94 95 describe '#length()'96 it 'should return the number of session'97+ var length98 memory.commit({ id: '1' })99 memory.commit({ id: '2' })78- memory.length().should.eql 2100+ memory.length(function(error, len) {101+ length = len102+ })103+ length.should.eql 2104 end105 end106 107 describe '#destroy()'26 unmodified lines