Misc Session refactoring
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 12e90b0 · Browse files · 4 files·+36 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+36 added−41 removed
- tests:+10 added−17 removed
- other:+26 added−24 removed
Provenance
- Commit
a08b14a- Tree
cc5f320- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
12e90b0
4 files changed+36 added−41 removed
modified:lib/express/plugins/session.js+24 added-17 removed
65 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, callback) {78- if(sid && this.store[sid]) callback( null, this.store[sid] );79- else this.newSession(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 92 commit: function(session, callback) {93 this.store[session.id] = session 91- if( callback ) callback( null , session );94+ if (callback) callback(null, session)95 },96 97 /**98 * Clear all sessions.99 *100+ * @param {function} callback101 * @api public102 */103 104 clear: function(callback) {105 this.store = {}102- if( callback ) callback();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 117 destroy: function(sid, callback) {118 delete this.store[sid]114- if( callback ) callback();119+ if (callback) callback(sid)120 },121 122 /**118- * Return the number of sessions currently stored.123+ * Pass the number of sessions currently stored.124 *120- * @return {int}125+ * @param {function} callback126 * @api public127 */128 129 length: function(callback) {125- callback(null, this.store.values.length);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 141 reap: function(ms, callback) {1 unmodified line143 this.store.each(function(session, sid){144 if (session.lastAccess < threshold)145 this.destroy(sid)146 }, this)141- if( callback ) callback(); 147+ if (callback) callback()148 },149 150 /**145- * Retrieves a shiny new session151+ * Creates and passes a shiny new session.152 *153 * @param {function} callback148- * @api private154+ * @api public155 */156+ 150- newSession: function(callback) {151- callback(null, new exports.Base(utils.uid()));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() {186- var self = this;193+ var self = this194 (function _reap() {195 setTimeout(function() {196 try {197 self.store.reap(self.lifetime || (1).day, _reap) 47 unmodified linesmodified:lib/express/request.js+2 added-7 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- puts("Error: ("+name+") - " + e );250- next(e)251- }247 }248 })()249 return this250 },30 unmodified linesmodified:spec/spec.plugins.flash.js+1 added-2 removed
4 unmodified lines567891091011121365 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; var sess = new Base(123) Session.store.commit(sess, function(){}) end describe 'Flash'65 unmodified linesmodified:spec/spec.plugins.session.js+9 added-15 removed
16 unmodified lines17 end18 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)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 end19 unmodified lines45 })46 get('/login', function(){47 return this.session.name48 })51- var sess= new Base(123);52- sess.same= true;53- memory.commit(sess)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'18 unmodified lines72 73 describe 'when the session does exist'74 it 'should return the previous session'75 var result80- var sess= new Base(1);81- sess.same= true;82- memory.commit(sess)76+ memory.commit(new Base('1'))77 memory.fetch('1', function(error, session){84- result= session78+ result = session79 })86- result.should.have_property 'same', true80+ result.id.should.eql '1'81 end82 end83 end84 8 unmodified lines93 end94 95 describe '#length()'96 it 'should return the number of session'103- var len97+ var length98 memory.commit({ id: '1' })99 memory.commit({ id: '2' })106- memory.length(function(error, l) {107- len= l100+ memory.length(function(error, len) {101+ length = len102 })109- len.should.eql 2103+ length.should.eql 2104 end105 end106 107 describe '#destroy()'26 unmodified lines