Skip to content

Commit

Permalink
Make extractSessExpires method more reliable and precise
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanBurunkov committed Dec 28, 2024
1 parent ed495b3 commit c0da427
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/express-sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module.exports = ({ Store }) => class SQLite3Store extends Store {
*/
touch(sid, sess, cb) {
const expires = extractSessExpires(sess);
if (expires === false) {
if (expires === -1) {
if (isFunc(cb)) cb(null, true);
return;
}
Expand Down
15 changes: 8 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { isObj, isFunc } = require('tm-is');
const { isObj, isFunc, isValidDate } = require('tm-is');

/**
* Process very basic callback logic.
* @param {Object} err Error object o null.
* Processes very basic callback logic.
* @param {Object} err Error object or null.
* @param {*} res Result data.
* @param {Function} cb Callback.
* @param {Function} action Function to process result.
Expand All @@ -16,7 +16,7 @@ exports.processCallback = (err, res, cb, action) => {
};

/**
* Takes passed in args object and merges it into one.
* Takes passed in args objects and merges them into one.
* Argumets order matter. Arg with greater index overrides existed properties.
* @param {...Object} args Given objects. Use first arg as a default.
* @returns {Object} result options.
Expand All @@ -28,11 +28,12 @@ exports.buildOptions = (...args) => args
/**
* Extracts cookie expires value from the given session.
* @param {Object} s Session object.
* @returns {number|false} Session expires timestamp. false if session data isn't invalid.
* @returns {number} Session expires timestamp. -1 if session data isn't invalid.
*/
exports.extractSessExpires = (s) => (isObj(s) && isObj(s.cookie) && s.cookie.expires
exports.extractSessExpires = (s) => (isObj(s) && isObj(s.cookie) && isValidDate(s.cookie.expires)
? new Date(s.cookie.expires).getTime()
: false);
: -1
);

/**
* Extract session max age from session cookie.
Expand Down
24 changes: 14 additions & 10 deletions tests/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,28 @@ describe('Test utils functions', () => {
});

describe('Test extractSessExpires function', () => {
test('Should return flase if no session passed', () => {
expect(utils.extractSessExpires()).toBe(false);
test('Should return -1 if no session passed', () => {
expect(utils.extractSessExpires()).toBe(-1);
});

test('Should return flase if given session is not an object', () => {
expect(utils.extractSessExpires('sess')).toBe(false);
test('Should return -1 if given session is not an object', () => {
expect(utils.extractSessExpires('sess')).toBe(-1);
});

test('Should return flase if given session have no cookie element', () => {
expect(utils.extractSessExpires({})).toBe(false);
test('Should return -1 if given session have no cookie element', () => {
expect(utils.extractSessExpires({})).toBe(-1);
});

test('Should return flase if given session.cookie is not an object', () => {
expect(utils.extractSessExpires({ cookie: 'cookie' })).toBe(false);
test('Should return -1 if given session.cookie is not an object', () => {
expect(utils.extractSessExpires({ cookie: 'cookie' })).toBe(-1);
});

test('Should return flase if given session.cookie is empty', () => {
expect(utils.extractSessExpires({ cookie: {} })).toBe(false);
test('Should return -1 if given session.cookie is empty', () => {
expect(utils.extractSessExpires({ cookie: {} })).toBe(-1);
});

test('Should return -1 if given session.cookie is not in format which could be parsed by JS Date', () => {
expect(utils.extractSessExpires({ cookie: { expires: 'qwerty' } })).toBe(-1);
});

test('Should return cookie expires value in case of valid session data', () => {
Expand Down

0 comments on commit c0da427

Please sign in to comment.