More session work
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent fa37e5a · Browse files · 5 files·+34 added−28 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+34 added−28 removed
- other:+34 added−28 removed
Provenance
- Commit
047499d- Tree
d60d6b1- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
fa37e5a
5 files changed+34 added−28 removed
modified:examples/chat/public/javascripts/app.js+1 added-1 removed
3 unmodified lines456788910111216 unmodified lines3 unmodified lines $('form').submit(function(){ var message = $('input[name=message]'), name = $('input[name=name]') if (message.val()) $.post('/chat', { message: $.trim(name.val()) + ': ' + message.val() }, function(){ $.post('/chat', { name: name.val(), message: message.val() }, function(){ message.val('') }) else message.css('border', '1px solid red')16 unmodified linesmodified:examples/chat/views/chat.haml.html+1 added-1 removed
3 unmodified lines4567889103 unmodified lines :each msg in messages %li= msg%form{ method: 'post' } %input{ type: 'hidden', name: '_method', value: 'put' } %input{ type: 'text', name: 'name', value: 'guest' } %input{ type: 'text', name: 'name', value: name || 'guest' } %input{ type: 'text', name: 'message' } %input{ type: 'submit', value: 'Send' }No newline at end of fileNo newline at end of filemodified:examples/chat/app.js+5 added-4 removed
6 unmodified lines789101111121314141516175 unmodified lines232425262827282930313233343535363738394034 unmodified lines6 unmodified lines use(MethodOverride) use(ContentLength) use(CommonLogger) use(Cookie) use(Session) use(Session, { reapInterval: 10000, expires: 60000 }) set('root', dirname(__filename)) enable('cache static files') enable('session reap interval') //enable('cache view contents')})
var messages = []5 unmodified linesget('/chat', function(){ this.render('chat.haml.html', { locals: { title: 'Chat', messages: messages messages: messages, name: this.session.name } })})
post('/chat', function(){ this.session.name = this.param('name') messages .push(escape(this.param('message')) .push(escape(this.param('name')) + ': ' + escape(this.param('message')) .replace(/(http:\/\/[^\s]+)/g, '<a href="$1" target="express-chat">$1</a>') .replace(/:\)/g, '<img src="http://icons3.iconfinder.netdna-cdn.com/data/icons/ledicons/emoticon_smile.png">')) this.halt(200)})34 unmodified linesmodified:lib/express/plugins/session.js+26 added-21 removed
1 2 // Express - Session - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4-function options() {5- var inOneDay = new Date(Number(new Date) + 86400)6- return set('session') || { expires: inOneDay }7-}8-9-function reaper() {10- setInterval(function(){11- exports.Session.store.reap(set('session reap threshold') || 86400)12- }, set('session reap interval') || 3600)13-}14-4 var Session = Class({5 init: function(sid) {6 this.id = sid7 this.touch()8 },9 10 touch: function() {11 this.lastAccess = Number(new Date)12+ require('sys').p('touched ' + this.id)13 }14 })15 16 exports.MemoryStore = Class({13 unmodified lines30 this.store = {}31 },32 33 destroy: function(sid) {34+ require('sys').p('destroying ' + sid)35+ require('sys').p(this.store[sid])36 delete this.store[sid]37 },38 39 reap: function(ms) {40 var threshold = Number(new Date(Number(new Date) - ms))41+ require('sys').p('reaping less than ' + threshold + ' ms')42 for (var sid in this.store)43 if (this.store.hasOwnProperty(sid))44 if (this.store[sid].lastAccess < threshold)45 this.destroy(sid)4 unmodified lines50 extend: {51 52 /**53 * Initialize memory store and start reaper.54+ */55+ 56+ init: function(options) {57+ process.mixin(this, options)58+ this.store = new (this.store || exports.MemoryStore)(options)59+ this.startReaper()60+ },61+ 62+ /**63+ * Start reaping64 *62- * Options:63- *64- * - store: Session data-store constructor, defaults to MemoryStore 65- *66- * @param {hash} options65+ * @param {Type} Var66+ * @return {Type}67+ * @api public68 */69 69- init: function(opts) {70- opts = opts || {}71- this.store = new (opts.store || exports.MemoryStore)(options())72- reaper()70+ startReaper: function() {71+ var self = this,72+ oneDay = 86400000,73+ oneHour = 360000074+ setInterval(function(){75+ require('sys').p('reaping')76+ self.store.reap(self.expires || self.maxAge || oneDay)77+ }, self.reapInterval || oneHour)78 }79 },80 81 // --- Events6 unmodified lines88 89 request: function(event) {90 var sid91 if (!(sid = event.request.cookie('sid')))87- event.request.cookie('sid', sid = uid(), options())92+ event.request.cookie('sid', sid = uid(), set('session cookie'))93 event.request.session = exports.Session.store.fetch(sid)94 event.request.session.touch()95 },96 9 unmodified linesmodified:lib/express/plugin.js+1 added-1 removed
8 unmodified lines910111213131415161731 unmodified lines8 unmodified lines * @api public */
exports.use = function(plugin, options) { if ('init' in plugin) plugin.init() if ('init' in plugin) plugin.init(options) Express.plugins.push({ klass: plugin, options: options })31 unmodified lines