Skip to content
Myr

Merge branch 'session_with_callbacks' of git://github.com/ciaranj/express into async-session

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, 212dc88 · 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

5 files+115 added−38 removed

modified:examples/chat/app.js+15 added-9 removed
modified:lib/express/plugins/session.js+57 added-21 removed
7 unmodified lines
8var utils = require('express/utils')910// --- Session1112var Session = new Class({12exports.Base = new Class({13 14 /**15 * Initialize session _sid_.16 */
56 unmodified lines
73 * @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 line
85 * @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 line
108 * @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 line
120 * @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 line
131 * @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 },143144 /**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})154155// --- Session
26 unmodified lines
182 * @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 // --- Events
3 unmodified lines
207 /**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
244 unmodified lines
245 try {246 if (self.plugins.at(complete++).trigger(new Event(name, data), next) !== true)247 next()248 } catch(e) {249 puts("Error: ("+name+") - " + e );250 next(e)251 }252 }253 })()
32 unmodified lines
modified:spec/spec.plugins.flash.js+3 added
modified:spec/spec.plugins.session.js+39 added-8 removed
2 unmodified lines
3 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 lines
14 get('/login', function(){ return '' })15 get('/login').headers['set-cookie'].should.match(/^sid=(\w+);/)16 end17 end17 18 describe 'when sid cookie is present'1819 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 3839 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 line
47 })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 lines
65 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 lines
99 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