Added Session class
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent f4b4c03 · Browse files · 1 file·+33 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+33 added−14 removed
- other:+33 added−14 removed
Provenance
- Commit
fbf547d- Tree
c8f79f6- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
f4b4c03
1 file changed+33 added−14 removed
modified:lib/express/plugins/session.js+33 added-14 removed
123456789101112131415161718192021222324251026272813142930313233344 unmodified lines3940414243444546474849502930515253545556574 unmodified lines62636465434445464748496667686970717273747576575877787980
// Express - Session - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
function options() { var inOneDay = new Date(Number(new Date) + 86400) return set('session') || { expires: inOneDay }}
var Session = Class({ init: function(sid) { this.id = sid this.touch() }, touch: function() { this.lastAccess = Number(new Date) }})
exports.MemoryStore = Class({ init: function() { this.store = {} }, fetch: function(sid) { return this.store[sid] || {} return this.store[sid] || new Session(sid) }, commit: function(sid, hash) { return this.store[sid] = hash commit: function(session) { return this.store[session.id] = session }, clear: function() { this.store = {}4 unmodified lines extend: { /** * Initialize memory store. * * Options: * * - store: Session data-store constructor, defaults to MemoryStore * * @param {hash} options */ init: function() { this.store = new (set('session store') || exports.MemoryStore) init: function(opts) { opts = opts || {} this.store = new (opts.store || exports.MemoryStore)(options()) } }, // --- Events4 unmodified lines * Create session id when not found; delegate to store. */ request: function(event) { var request = event.request if (!(request.sid = request.cookie('sid'))) { var inOneDay = new Date(Number(new Date) + 86400), options = set('session') || { expires: inOneDay } request.cookie('sid', request.sid = uid(), options) } request.session = exports.Session.store.fetch(request.sid) var sid if (!(sid = event.request.cookie('sid'))) event.request.cookie('sid', sid = uid(), options()) event.request.session = exports.Session.store.fetch(sid) }, /** * Delegate to store, allowing it to save sessions changes. */ response: function(event) { if (event.request.sid) exports.Session.store.commit(event.request.sid, event.request.session) exports.Session.store.commit(event.request.session) } }})No newline at end of fileNo newline at end of file