UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

44 lines 1.81 kB
import { compareDesc, minutesToMilliseconds } from 'date-fns'; import memoizee from 'memoizee'; export default class SessionService { constructor({ sessionStore }, { getLogger }) { this.logger = getLogger('lib/services/session-service.ts'); this.sessionStore = sessionStore; this.resolveMaxSessions = memoizee(async () => await this.sessionStore.getMaxSessionsCount(), { promise: true, maxAge: minutesToMilliseconds(1), }); } async getActiveSessions() { return this.sessionStore.getActiveSessions(); } async getSessionsForUser(userId) { return this.sessionStore.getSessionsForUser(userId); } async getSession(sid) { return this.sessionStore.get(sid); } async deleteSessionsForUser(userId) { return this.sessionStore.deleteSessionsForUser(userId); } async deleteStaleSessionsForUser(userId, maxSessions) { const userSessions = await this.sessionStore.getSessionsForUser(userId); const newestFirst = userSessions.sort((a, b) => compareDesc(a.createdAt, b.createdAt)); const sessionsToDelete = newestFirst.slice(maxSessions); await Promise.all(sessionsToDelete.map((session) => this.sessionStore.delete(session.sid))); return sessionsToDelete.length; } async deleteSession(sid) { return this.sessionStore.delete(sid); } async insertSession({ sid, sess, }) { return this.sessionStore.insertSession({ sid, sess }); } async getSessionsCount() { return Object.fromEntries((await this.sessionStore.getSessionsCount()).map(({ userId, count }) => [userId, count])); } async getMaxSessionsCount() { return this.resolveMaxSessions(); } } //# sourceMappingURL=session-service.js.map