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 {
|
|
|
|
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}`);
|
2021-12-19 03:31:34 -05:00
|
|
|
const {cookie: {expires} = {}} = s || {};
|
|
|
|
if (expires && new Date() >= new Date(expires)) return await this._destroy(sid);
|
2021-09-16 23:01:10 -04:00
|
|
|
return 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}`);
|
2021-09-16 23:01:10 -04:00
|
|
|
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;
|