Added session reaping
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent fbf547d · Browse files · 2 files·+69 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+69 added−2 removed
- tests:+55 added−1 removed
- other:+14 added−1 removed
Provenance
- Commit
8ed99f6- Tree
9141710- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
fbf547d
2 files changed+69 added−2 removed
modified:lib/express/plugins/session.js+14 added-1 removed
30 unmodified lines313233343536373839404142434445464748495026 unmodified lines77787980698182838485867 unmodified lines30 unmodified lines }, clear: function() { this.store = {} }, destroy: function(sid) { delete this.store[sid] }, reap: function(ms) { var threshold = Number(new Date(Number(new Date) - ms)) for (var sid in this.store) if (this.store.hasOwnProperty(sid)) if (this.store[sid].lastAccess < threshold) this.destroy(sid) }})
exports.Session = Plugin.extend({26 unmodified lines request: function(event) { var sid if (!(sid = event.request.cookie('sid'))) event.request.cookie('sid', sid = uid(), options()) event.request.session = exports.Session.store.fetch(sid) event.request.session = exports.Session.store.fetch(sid) event.request.session.touch() }, /** * Delegate to store, allowing it to save sessions changes.7 unmodified linesmodified:spec/spec.plugins.session.js+55 added-1 removed
19 unmodified lines20212223242425262728293031324 unmodified lines37383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929319 unmodified lines get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'set-cookie' end end describe 'with MemoryStore' describe 'MemoryStore' before_each memory = new (require('express/plugins/session').MemoryStore) end it 'should persist' post('/login', function(){ return this.session.name = 'tj' })4 unmodified lines post('/login', headers) get('/login', headers).status.should.eql 200 get('/login', headers).body.should.eql 'tj' end describe '#fetch()' describe 'when the session does not exist' it 'should return a new Session' memory.fetch('1').should.have_property 'lastAccess' end end describe 'when the session does exist' it 'should return the previous session' memory.commit({ id: '1', same: true }) memory.fetch('1').should.have_property 'same', true end end end describe '#clear()' it 'should remove all sessions' memory.commit({ id: '1' }) memory.commit({ id: '2' }) memory.clear() memory.should.not.have_property '1' memory.should.not.have_property '2' end end describe '#destroy()' it 'should destroy a single session' memory.commit({ id: '1' }) memory.commit({ id: '2' }) memory.destroy('1') memory.store.should.not.have_property '1' memory.store.should.have_property '2' end end describe '#reap()' it 'should destroy sessions older than the given age in milliseconds' memory.commit({ id: '1', lastAccess: Number(new Date) - 300 }) memory.commit({ id: '2', lastAccess: Number(new Date) - 250 }) memory.commit({ id: '3', lastAccess: Number(new Date) - 100 }) memory.commit({ id: '4', lastAccess: Number(new Date) }) memory.reap(200) memory.store.should.not.have_property '1' memory.store.should.not.have_property '2' memory.store.should.have_property '3' memory.store.should.have_property '4' end end end endendNo newline at end of fileNo newline at end of file