2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
const DB = require('./DB');
|
|
|
|
const Store = require('express-session').Store;
|
|
|
|
const log4js = require('log4js');
|
2021-09-16 23:01:10 -04:00
|
|
|
const util = require('util');
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2020-09-21 16:57:10 -04:00
|
|
|
const logger = log4js.getLogger('SessionStore');
|
2020-09-21 16:45:25 -04:00
|
|
|
|
2021-09-16 23:01:10 -04:00
|
|
|
class SessionStore extends Store {
|
2022-01-17 17:27:23 -05:00
|
|
|
async _checkExpiration(sid, sess) {
|
|
|
|
const {cookie: {expires} = {}} = sess || {};
|
|
|
|
if (expires && new Date() >= new Date(expires)) return await this._destroy(sid);
|
|
|
|
return sess;
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:01:10 -04:00
|
|
|
async _get(sid) {
|
2020-11-23 13:24:19 -05:00
|
|
|
logger.debug(`GET ${sid}`);
|
2021-09-16 23:01:10 -04:00
|
|
|
const s = await DB.get(`sessionstorage:${sid}`);
|
2022-01-17 17:27:23 -05:00
|
|
|
return await this._checkExpiration(sid, s);
|
2020-09-21 17:05:29 -04:00
|
|
|
}
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2021-09-16 23:01:10 -04:00
|
|
|
async _set(sid, sess) {
|
2020-11-23 13:24:19 -05:00
|
|
|
logger.debug(`SET ${sid}`);
|
2022-01-17 17:27:23 -05:00
|
|
|
sess = await this._checkExpiration(sid, sess);
|
|
|
|
if (sess != null) await DB.set(`sessionstorage:${sid}`, sess);
|
2020-09-21 17:05:29 -04:00
|
|
|
}
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2021-09-16 23:01:10 -04:00
|
|
|
async _destroy(sid) {
|
2020-11-23 13:24:19 -05:00
|
|
|
logger.debug(`DESTROY ${sid}`);
|
2021-09-16 23:01:10 -04:00
|
|
|
await DB.remove(`sessionstorage:${sid}`);
|
2020-09-21 17:05:29 -04:00
|
|
|
}
|
2021-09-16 23:01:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// express-session doesn't support Promise-based methods. This is where the callbackified versions
|
|
|
|
// used by express-session are defined.
|
|
|
|
for (const m of ['get', 'set', 'destroy']) {
|
|
|
|
SessionStore.prototype[m] = util.callbackify(SessionStore.prototype[`_${m}`]);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SessionStore;
|