Skip to content

Commit

Permalink
Change expire mechanism to use cookie expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Oct 10, 2020
1 parent fe2ac1d commit fc44b75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.0.0 (YYYY-MM-DD)

* **Larger change:** Align session expiration logic with [connect-redis](https://github.com/tj/connect-redis/blob/30efd159103ace270c844a5967428b43e7b8ba4a/migration-to-v4.md#changes-to-ttl-management) which in turned aligned with connect-mongo. Fixes #54

## 6.2.1 (2020-08-19)

* **Fix:** Regression, query errors wasn't properly forwarded. Fixes #180 and #179. Thanks @alxndrsn! (5c324ac)
Expand Down
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const escapePgIdentifier = (value) => value.replace(/"/g, '""');

/** @typedef {(err: Error|null) => void} SimpleErrorCallback */

/** @typedef {{ cookie: { maxAge: number, [property: string]: any }, [property: string]: any }} SessionObject */
/** @typedef {{ cookie: { maxAge?: number, expire?: number, [property: string]: any }, [property: string]: any }} SessionObject */

/** @typedef {(delay: number) => number} PGStorePruneDelayRandomizer */
/** @typedef {Object<string, any>} PGStoreQueryResult */
Expand Down Expand Up @@ -198,17 +198,22 @@ module.exports = function (session) {
/**
* Figure out when a session should expire
*
* @param {number} [maxAge] - the maximum age of the session cookie
* @param {SessionObject} sess – the session object to store
* @returns {number} the unix timestamp, in seconds
* @access private
*/
getExpireTime (maxAge) {
let ttl = this.ttl;
_getExpireTime (sess) {
let expire;

ttl = ttl || (typeof maxAge === 'number' ? maxAge / 1000 : ONE_DAY);
ttl = Math.ceil(ttl + currentTimestamp());
if (sess && sess.cookie && sess.cookie.expires) {
const expireDate = new Date(sess.cookie.expires);
expire = Math.ceil(expireDate.valueOf() / 1000);
} else {
const ttl = this.ttl || ONE_DAY;
expire = Math.ceil(Date.now() / 1000 + ttl);
}

return ttl;
return expire;
}

/**
Expand Down Expand Up @@ -277,7 +282,7 @@ module.exports = function (session) {
* @access public
*/
set (sid, sess, fn) {
const expireTime = this.getExpireTime(sess.cookie.maxAge);
const expireTime = this._getExpireTime(sess);
const query = 'INSERT INTO ' + this.quotedTable() + ' (sess, expire, sid) SELECT $1, to_timestamp($2), $3 ON CONFLICT (sid) DO UPDATE SET sess=$1, expire=to_timestamp($2) RETURNING sid';

this.query(
Expand Down Expand Up @@ -311,7 +316,7 @@ module.exports = function (session) {
* @access public
*/
touch (sid, sess, fn) {
const expireTime = this.getExpireTime(sess.cookie.maxAge);
const expireTime = this._getExpireTime(sess);

this.query(
'UPDATE ' + this.quotedTable() + ' SET expire = to_timestamp($1) WHERE sid = $2 RETURNING sid',
Expand Down

0 comments on commit fc44b75

Please sign in to comment.