feat: add TTL to GameSessionStore, replay protection and session cleanup to evaluateAnswer
This commit is contained in:
parent
54705943fa
commit
fdeb769640
6 changed files with 218 additions and 7 deletions
|
|
@ -1,15 +1,27 @@
|
|||
import type { GameSessionStore, GameSessionData } from "./GameSessionStore.js";
|
||||
|
||||
export class InMemoryGameSessionStore implements GameSessionStore {
|
||||
private sessions = new Map<string, GameSessionData>();
|
||||
type SessionEntry = { data: GameSessionData; expiresAt: number };
|
||||
|
||||
create(sessionId: string, data: GameSessionData): Promise<void> {
|
||||
this.sessions.set(sessionId, data);
|
||||
export class InMemoryGameSessionStore implements GameSessionStore {
|
||||
private sessions = new Map<string, SessionEntry>();
|
||||
|
||||
create(
|
||||
sessionId: string,
|
||||
data: GameSessionData,
|
||||
ttlMs: number,
|
||||
): Promise<void> {
|
||||
this.sessions.set(sessionId, { data, expiresAt: Date.now() + ttlMs });
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
get(sessionId: string): Promise<GameSessionData | null> {
|
||||
return Promise.resolve(this.sessions.get(sessionId) ?? null);
|
||||
const entry = this.sessions.get(sessionId);
|
||||
if (!entry) return Promise.resolve(null);
|
||||
if (Date.now() > entry.expiresAt) {
|
||||
this.sessions.delete(sessionId);
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
return Promise.resolve(entry.data);
|
||||
}
|
||||
|
||||
delete(sessionId: string): Promise<void> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue