Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix restoring cookie.originalMaxAge when store returns Date
fixes #664
- Loading branch information
1 parent
479940a
commit 30e23f1
Showing
4 changed files
with
85 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
var session = require('../../') | ||
var util = require('util') | ||
|
||
/* istanbul ignore next */ | ||
var defer = typeof setImmediate === 'function' | ||
? setImmediate | ||
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) } | ||
|
||
module.exports = SmartStore | ||
|
||
function SmartStore () { | ||
session.Store.call(this) | ||
this.sessions = Object.create(null) | ||
} | ||
|
||
util.inherits(SmartStore, session.Store) | ||
|
||
SmartStore.prototype.destroy = function destroy (sid, callback) { | ||
delete this.sessions[sid] | ||
defer(callback, null) | ||
} | ||
|
||
SmartStore.prototype.get = function get (sid, callback) { | ||
var sess = this.sessions[sid] | ||
|
||
if (!sess) { | ||
return | ||
} | ||
|
||
// parse | ||
sess = JSON.parse(sess) | ||
|
||
if (sess.cookie) { | ||
// expand expires into Date object | ||
sess.cookie.expires = typeof sess.cookie.expires === 'string' | ||
? new Date(sess.cookie.expires) | ||
: sess.cookie.expires | ||
|
||
// destroy expired session | ||
if (sess.cookie.expires && sess.cookie.expires <= Date.now()) { | ||
delete this.sessions[sid] | ||
sess = null | ||
} | ||
} | ||
|
||
defer(callback, null, sess) | ||
} | ||
|
||
SmartStore.prototype.set = function set (sid, sess, callback) { | ||
this.sessions[sid] = JSON.stringify(sess) | ||
defer(callback, null) | ||
} |