1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

fix: session fetching should not crash application when db is unavilable

This commit is contained in:
Ivar Conradi Østhus 2021-06-21 10:25:03 +02:00
parent 2da26e7f14
commit 2e66bce5ae
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
3 changed files with 40 additions and 4 deletions

View File

@ -66,7 +66,7 @@
"async": "^3.1.1", "async": "^3.1.1",
"bcrypt": "^5.0.1", "bcrypt": "^5.0.1",
"compression": "^1.7.4", "compression": "^1.7.4",
"connect-session-knex": "^2.0.0", "connect-session-knex": "^2.1.0",
"cookie-parser": "^1.4.5", "cookie-parser": "^1.4.5",
"cookie-session": "^2.0.0-rc.1", "cookie-session": "^2.0.0-rc.1",
"cors": "^2.8.5", "cors": "^2.8.5",

View File

@ -6,10 +6,14 @@ const KnexSessionStore = require('connect-session-knex')(session);
const TWO_DAYS = 48 * 60 * 60 * 1000; const TWO_DAYS = 48 * 60 * 60 * 1000;
const HOUR = 60 * 60 * 1000; const HOUR = 60 * 60 * 1000;
// eslint-disable-next-line prettier/prettier
type SessionConfig = Pick<IUnleashConfig, 'session' | 'server' | 'secureHeaders' | 'getLogger'>;
function sessionDb( function sessionDb(
config: Pick<IUnleashConfig, 'session' | 'server' | 'secureHeaders'>, config: SessionConfig,
stores: Pick<IUnleashStores, 'db'>, stores: Pick<IUnleashStores, 'db'>,
): any { ): any {
const logger = config.getLogger('/middleware/session-db.ts');
let store; let store;
const { db } = config.session; const { db } = config.session;
const age = config.session.ttlHours * HOUR || TWO_DAYS; const age = config.session.ttlHours * HOUR || TWO_DAYS;
@ -19,6 +23,32 @@ function sessionDb(
tablename: 'unleash_session', tablename: 'unleash_session',
createtable: false, createtable: false,
}); });
store.get = async (sid, fn) => {
try {
await store.ready;
const condition = 'CAST(? as timestamp with time zone) <= expired';
const content = await store.knex
.select('sess')
.from(store.tablename)
.where(store.sidfieldname, '=', sid)
.andWhereRaw(condition, new Date())
.then(response => {
let ret;
if (response[0]) {
ret = response[0].sess;
if (typeof ret === 'string') {
ret = JSON.parse(ret);
}
}
return ret;
});
fn(null, content);
} catch (e) {
logger.error(e);
fn(null, undefined);
}
};
} else { } else {
store = new session.MemoryStore(); store = new session.MemoryStore();
} }
@ -38,7 +68,13 @@ function sessionDb(
maxAge: age, maxAge: age,
}, },
}); });
return (req, res, next) => sessionMiddleware(req, res, next); return (req, res, next) => {
try {
sessionMiddleware(req, res, next);
} catch (e) {
console.log(e);
}
};
} }
export default sessionDb; export default sessionDb;
module.exports = sessionDb; module.exports = sessionDb;

View File

@ -1657,7 +1657,7 @@ confusing-browser-globals@^1.0.10:
resolved "https://registry.yarnpkg.com/confusing-browser-globals/-/confusing-browser-globals-1.0.10.tgz#30d1e7f3d1b882b25ec4933d1d1adac353d20a59" resolved "https://registry.yarnpkg.com/confusing-browser-globals/-/confusing-browser-globals-1.0.10.tgz#30d1e7f3d1b882b25ec4933d1d1adac353d20a59"
integrity sha512-gNld/3lySHwuhaVluJUKLePYirM3QNCKzVxqAdhJII9/WXKVX5PURzMVJspS1jTslSqjeuG4KMVTSouit5YPHA== integrity sha512-gNld/3lySHwuhaVluJUKLePYirM3QNCKzVxqAdhJII9/WXKVX5PURzMVJspS1jTslSqjeuG4KMVTSouit5YPHA==
connect-session-knex@^2.0.0: connect-session-knex@^2.1.0:
version "2.1.0" version "2.1.0"
resolved "https://registry.yarnpkg.com/connect-session-knex/-/connect-session-knex-2.1.0.tgz#7f1e32594d37f7a1ad24e6dee97d8f2d9799695e" resolved "https://registry.yarnpkg.com/connect-session-knex/-/connect-session-knex-2.1.0.tgz#7f1e32594d37f7a1ad24e6dee97d8f2d9799695e"
integrity sha512-6xHoDajVWxwByaq6UjfU+qGE89Nurajek4JsxeajtXscr8xznzbsIp+Q/3FKh3/2smfgoSeY+93+Gn+3ZXQVDQ== integrity sha512-6xHoDajVWxwByaq6UjfU+qGE89Nurajek4JsxeajtXscr8xznzbsIp+Q/3FKh3/2smfgoSeY+93+Gn+3ZXQVDQ==