forked from expressjs/session
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved store to class based approach.
- Loading branch information
1 parent
1cee294
commit f195620
Showing
3 changed files
with
122 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,56 @@ | ||
'use strict' | ||
|
||
var session = require('../../') | ||
var util = require('util') | ||
|
||
const session = require('../../') | ||
const util = require('util') | ||
const Store = require('../../session/store') | ||
/* istanbul ignore next */ | ||
var defer = typeof setImmediate === 'function' | ||
const 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) | ||
class SmartStore extends Store { | ||
constructor() { | ||
super() | ||
this.sessions = Object.create(null) | ||
} | ||
|
||
SmartStore.prototype.destroy = function destroy (sid, callback) { | ||
delete this.sessions[sid] | ||
defer(callback, null) | ||
} | ||
destroy(sid, callback) { | ||
delete this.sessions[sid] | ||
defer(callback) | ||
} | ||
|
||
SmartStore.prototype.get = function get (sid, callback) { | ||
var sess = this.sessions[sid] | ||
get(sid, callback) { | ||
let sess = this.sessions[sid] | ||
|
||
if (!sess) { | ||
return | ||
} | ||
if (!sess) { | ||
return | ||
} | ||
|
||
// parse | ||
sess = JSON.parse(sess) | ||
// 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 | ||
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 | ||
// destroy expired session | ||
if (sess.cookie.expires && sess.cookie.expires <= Date.now()) { | ||
delete this.sessions[sid] | ||
sess = null | ||
} | ||
} | ||
|
||
defer(callback, null, sess) | ||
} | ||
|
||
defer(callback, null, sess) | ||
set(sid, sess, callback) { | ||
this.sessions[sid] = JSON.stringify(sess) | ||
defer(callback) | ||
} | ||
} | ||
|
||
SmartStore.prototype.set = function set (sid, sess, callback) { | ||
this.sessions[sid] = JSON.stringify(sess) | ||
defer(callback, null) | ||
} | ||
|
||
module.exports = SmartStore |
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
'use strict' | ||
|
||
var session = require('../../') | ||
var util = require('util') | ||
const Store = require('../../session/store') | ||
|
||
module.exports = SyncStore | ||
|
||
function SyncStore () { | ||
session.Store.call(this) | ||
this.sessions = Object.create(null) | ||
} | ||
class SyncStore extends Store { | ||
constructor() { | ||
// @ts-ignore | ||
super() | ||
this.sessions = Object.create(null) | ||
} | ||
|
||
util.inherits(SyncStore, session.Store) | ||
destroy(sid, callback) { | ||
delete this.sessions[sid] | ||
callback() | ||
} | ||
|
||
SyncStore.prototype.destroy = function destroy (sid, callback) { | ||
delete this.sessions[sid] | ||
callback() | ||
} | ||
get(sid, callback) { | ||
callback(null, JSON.parse(this.sessions[sid])) | ||
} | ||
|
||
SyncStore.prototype.get = function get (sid, callback) { | ||
callback(null, JSON.parse(this.sessions[sid])) | ||
set(sid, sess, callback) { | ||
this.sessions[sid] = JSON.stringify(sess) | ||
callback() | ||
} | ||
} | ||
|
||
SyncStore.prototype.set = function set (sid, sess, callback) { | ||
this.sessions[sid] = JSON.stringify(sess) | ||
callback() | ||
} | ||
module.exports = SyncStore |