Skip to content

Commit

Permalink
Converted session to class based approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Jul 28, 2023
1 parent 2c31f8a commit f3d63a3
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 115 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ function getcookie(req, name, secrets) {

function hash(sess) {
// serialize
var str = JSON.stringify(sess, function (key, val) {
const str = JSON.stringify(sess, function (key, val) {
// ignore sess.cookie property
if (this === sess && key === 'cookie') {
return
Expand Down
208 changes: 94 additions & 114 deletions session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var defer = typeof setImmediate === 'function'
* Expose Session.
*/

module.exports = Session;

/**
* Create a new `Session` with the given request and `data`.
Expand All @@ -31,126 +30,107 @@ module.exports = Session;
* @api private
*/

function Session(req, data) {
Object.defineProperty(this, 'req', { value: req });
Object.defineProperty(this, 'id', { value: req.sessionID });
class Session {

constructor(req, data) {
Object.defineProperty(this, 'req', {value: req});
Object.defineProperty(this, 'id', {value: req.sessionID});

if (typeof data === 'object' && data !== null) {
// merge data into this, ignoring prototype properties
for (var prop in data) {
if (!(prop in this)) {
this[prop] = data[prop]
if (typeof data === 'object' && data !== null) {
// merge data into this, ignoring prototype properties
for (var prop in data) {
if (!(prop in this)) {
this[prop] = data[prop]
}
}
}
}
}

/**
* Update reset `.cookie.maxAge` to prevent
* the cookie from expiring when the
* session is still active.
*
* @param {Function} fn optional done callback
* @return {Session} for chaining
* @api public
*/

defineMethod(Session.prototype, 'touch', function touch(fn) {
this.resetMaxAge();
if (fn) defer(fn);
return this;
});

/**
* Reset `.maxAge` to `.originalMaxAge`.
*
* @return {Session} for chaining
* @api public
*/

defineMethod(Session.prototype, 'resetMaxAge', function resetMaxAge() {
this.cookie.maxAge = this.cookie.originalMaxAge;
return this;
});

/**
* Save the session data with optional callback `fn(err)`.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/

defineMethod(Session.prototype, 'save', function save(fn) {
this.req.sessionStore.set(this.id, this, fn || function(){});
return this;
});

/**
* Re-loads the session data _without_ altering
* the maxAge properties. Invokes the callback `fn(err)`,
* after which time if no exception has occurred the
* `req.session` property will be a new `Session` object,
* although representing the same session.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/

defineMethod(Session.prototype, 'reload', function reload(fn) {
var req = this.req
var store = this.req.sessionStore

store.get(this.id, function(err, sess){
if (err) return fn(err);
if (!sess) return fn(new Error('failed to load session'));
store.createSession(req, sess);
fn();
});
return this;
});

/**
* Destroy `this` session.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/
/**
* Update reset `.cookie.maxAge` to prevent
* the cookie from expiring when the
* session is still active.
*
* @param {Function} fn optional done callback
* @return {Session} for chaining
* @api public
*/
touch(fn){
this.resetMaxAge();
if (fn) defer(fn);
return this;
}
/**
* Reset `.maxAge` to `.originalMaxAge`.
*
* @return {Session} for chaining
* @api public
*/
resetMaxAge(){
this.cookie.maxAge = this.cookie.originalMaxAge;
return this;
}

defineMethod(Session.prototype, 'destroy', function destroy(fn) {
delete this.req.session;
this.req.sessionStore.destroy(this.id, fn);
return this;
});
/**
* Save the session data with optional callback `fn(err)`.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/
save(fn){
this.req.sessionStore.set(this.id, this, fn || function(){});
return this;
}
/**
* Re-loads the session data _without_ altering
* the maxAge properties. Invokes the callback `fn(err)`,
* after which time if no exception has occurred the
* `req.session` property will be a new `Session` object,
* although representing the same session.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/
reload(fn){
const req = this.req
const store = this.req.sessionStore

store.get(this.id, function(err, sess){
if (err) return fn(err);
if (!sess) return fn(new Error('failed to load session'));
store.createSession(req, sess);
fn();
});
return this;
}

/**
* Regenerate this request's session.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/
/**
* Destroy `this` session.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/
destroy(fn){
delete this.req.session;
this.req.sessionStore.destroy(this.id, fn);
return this;
}

defineMethod(Session.prototype, 'regenerate', function regenerate(fn) {
this.req.sessionStore.regenerate(this.req, fn);
return this;
});
/**
* Regenerate this request's session.
*
* @param {Function} fn
* @return {Session} for chaining
* @api public
*/
regenerate(fn){
this.req.sessionStore.regenerate(this.req, fn);
return this;
}
}

/**
* Helper function for creating a method on a prototype.
*
* @param {Object} obj
* @param {String} name
* @param {Function} fn
* @private
*/
function defineMethod(obj, name, fn) {
Object.defineProperty(obj, name, {
configurable: true,
enumerable: false,
value: fn,
writable: true
});
};
module.exports = Session;

0 comments on commit f3d63a3

Please sign in to comment.