import { Request, Response } from '@/gemini'; import { Handler } from '@/mission-control'; export type SessionHandler = ( request: Request, response: Response, session: T, ) => void | Promise; const withSession = ( sessions: Map, empty: T, request: Request, ) => { if (request.fingerprint === null) { throw new Error('Missing Certificate'); } const key = request.fingerprint; let session = sessions.get(key); if (session === undefined) { session = { ...empty }; sessions.set(key, session); } return session; }; type WrappedHandler = (handler: SessionHandler) => Handler; const createSessionHandler = (empty: T): WrappedHandler => { const sessions = new Map(); return (handler: SessionHandler) => (request: Request, response: Response) => handler(request, response, withSession(sessions, empty, request)); }; export default createSessionHandler;