etherpad-lite/src/node/db/SessionStore.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-01-21 21:06:52 +00:00
'use strict';
2021-01-21 21:06:52 +00:00
const DB = require('./DB');
const Store = require('express-session').Store;
const log4js = require('log4js');
const util = require('util');
const logger = log4js.getLogger('SessionStore');
class SessionStore extends Store {
async _get(sid) {
2020-11-23 13:24:19 -05:00
logger.debug(`GET ${sid}`);
const s = await DB.get(`sessionstorage:${sid}`);
if (!s) return;
if (typeof s.cookie.expires === 'string') s.cookie.expires = new Date(s.cookie.expires);
if (s.cookie.expires && new Date() >= s.cookie.expires) {
await this._destroy(sid);
return;
}
return s;
}
async _set(sid, sess) {
2020-11-23 13:24:19 -05:00
logger.debug(`SET ${sid}`);
await DB.set(`sessionstorage:${sid}`, sess);
}
async _destroy(sid) {
2020-11-23 13:24:19 -05:00
logger.debug(`DESTROY ${sid}`);
await DB.remove(`sessionstorage:${sid}`);
}
}
// 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;