Squashed commit of the changes I've put in place to support callback
Before adoption: this commit came in with the history the project adopted, so no landing records it.
methods on the session object. This code changes the behaviour of the session handling in a fairly key way: If an unknown session id is seen then a *new* session is created with a *new* session id. The previous behaviour was to create a new session with the existing id.
authored by ciaranj · committed by ciaranj · parent ce82a91 · Browse files · 5 files·+115 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+115 added−38 removed
- tests:+42 added−8 removed
- other:+73 added−30 removed
Provenance
- Commit
212dc88- Tree
9d0cd20- Author
ciaranj <ciaranj@gmail.com>- Committer
ciaranj <ciaranj@gmail.com>- Parent
ce82a91
5 files changed+115 added−38 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/session.js+57 added-21 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 */56 unmodified lines73 * @return {Session}74 * @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]) callback( null, this.store[sid] );79+ else this.newSession(callback);80 },81 82 /**83 * Commit _session_ data.1 unmodified line85 * @param {Session} session86 * @api private87 */88 88- commit: function(session) {89- return this.store[session.id] = session89+ commit: function(session, callback) {90+ this.store[session.id] = session 91+ if( callback ) callback( null , session );92 },93 94 /**95 * Clear all sessions.96 *97 * @api public98 */99 98- clear: function() {100+ clear: function(callback) {101 this.store = {}102+ if( callback ) callback();103 },104 105 /**106 * Destroy session using the given _sid_.1 unmodified line108 * @param {int} sid109 * @api public110 */111 109- destroy: function(sid) {112+ destroy: function(sid, callback) {113 delete this.store[sid]114+ if( callback ) callback();115 },116 117 /**118 * Return the number of sessions currently stored.1 unmodified line120 * @return {int}121 * @api public122 */123 120- length: function() {121- return this.store.values.length124+ length: function(callback) {125+ callback(null, this.store.values.length);126 },127 128 /**129 * Reap sessions older than _ms_.1 unmodified line131 * @param {int} ms132 * @api private133 */134 131- reap: function(ms) {135+ reap: function(ms, callback) {136 var threshold = Number(new Date(Number(new Date) - ms))137 this.store.each(function(session, sid){138 if (session.lastAccess < threshold)139 this.destroy(sid)140 }, this)141+ if( callback ) callback(); 142+ },143+144+ /**145+ * Retrieves a shiny new session146+ *147+ * @param {function} callback148+ * @api private149+ */150+ newSession: function(callback) {151+ callback(null, new exports.Base(utils.uid()));152 }153 })154 155 // --- Session26 unmodified lines182 * @api private183 */184 185 startReaper: function() {171- setInterval(function(self){172- self.store.reap(self.lifetime || (1).day)173- }, this.reapInterval || this.reapEvery || (1).hour, this)186+ var self = this;187+ (function _reap() {188+ setTimeout(function() {189+ try {190+ self.store.reap(self.lifetime || (1).day, _reap) 191+ }192+ catch(e) { 193+ puts("Error: Problem during reap: " + e)194+ // as long as _reap never throws an exception we'll be ok. 195+ // (and the delegated reap method doesn't throw an exception after executing the callback)196+ _reap(); 197+ }198+ }, self.reapInterval || self.reapEvery || (1).hour, this)199+ })();200 }201 },202 203 // --- Events3 unmodified lines207 /**208 * Create session id when not found; delegate to store.209 */210 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()211+ request: function(event, next) {212+ var sid= event.request.cookie('sid');213+ exports.Session.store.fetch(sid, function(error, session) {214+ if( error ) next(error);215+ else {216+ // If an invalid session id was passed then the id can change217+ if( session.id != sid ) 218+ event.request.cookie('sid',session.id, set('session cookie'))219+ 220+ event.request.session= session;221+ event.request.session.touch();222+ next(); 223+ }224+ }); 225+ return true;226 },227 228 /**229 * Delegate to store, allowing it to save sessions changes.230 */231 197- response: function(event) {198- exports.Session.store.commit(event.request.session)232+ response: function(event, next) {233+ exports.Session.store.commit(event.request.session, next) 234+ return true;235 }236 }237 })modified:lib/express/request.js+1 added
245 unmodified lines246 try {247 if (self.plugins.at(complete++).trigger(new Event(name, data), next) !== true)248 next()249 } catch(e) {250+ puts("Error: ("+name+") - " + e );251 next(e)252 }253 }254 })()48 unmodified linesmodified:spec/spec.plugins.flash.js+3 added
4 unmodified lines5678910111213141564 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); sess.same= true; Session.store.commit(sess, function(){}) end describe 'Flash' describe '#flash()'64 unmodified linesmodified:spec/spec.plugins.session.js+39 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+ var sess= new Base(123);22+ sess.same= true;23+ Session.store.commit(sess)24 get('/login', function(){ return '' })25 get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'set-cookie'26 end27 end24- 28+ 29+ describe 'when unknown sid cookie is present'30+ it 'should set new sid'31+ get('/login', function(){ return '' })32+ var headers= get('/login', { headers: { cookie: 'sid=123' }}).headers33+ 34+ headers.should.have_property 'set-cookie'35+ headers['set-cookie'].should.not.be '123'36+ end37+ end 38+39 describe 'session Store.Memory'40 before_each27- memory = new (require('express/plugins/session').Store.Memory)41+ Session.store= memory = new (require('express/plugins/session').Store.Memory)42 end43 44 it 'should persist'45 post('/login', function(){1 unmodified line47 })48 get('/login', function(){49 return this.session.name50 })51+ var sess= new Base(123);52+ sess.same= true;53+ memory.commit(sess)54 var headers = { headers: { cookie: 'sid=123' }}55 post('/login', headers)56 get('/login', headers).status.should.eql 20057 get('/login', headers).body.should.eql 'tj'7 unmodified lines65 66 describe '#fetch()'67 describe 'when the session does not exist'68 it 'should return a new Session'69+ var result70+ memory.fetch('1', function(error,session){71+ result= session72+ })52- memory.fetch('1').should.have_property 'lastAccess'73+ result.should.have_property 'lastAccess'74 end75 end76 77 describe 'when the session does exist'78 it 'should return the previous session'58- memory.commit({ id: '1', same: true })59- memory.fetch('1').should.have_property 'same', true79+ var result80+ var sess= new Base(1);81+ sess.same= true;82+ memory.commit(sess)83+ memory.fetch('1', function(error, session){84+ result= session85+ })86+ result.should.have_property 'same', true87 end88 end89 end90 8 unmodified lines99 end100 101 describe '#length()'102 it 'should return the number of session'103+ var len104 memory.commit({ id: '1' })105 memory.commit({ id: '2' })78- memory.length().should.eql 2106+ memory.length(function(error, l) {107+ len= l108+ })109+ len.should.eql 2110 end111 end112 113 describe '#destroy()'26 unmodified lines