// The accounts a browser is signed in to an app as, as its session cookie // keeps them (session.ts): each account's HUD session, in the order they // were added, and which is in use (`active`). Signing in adds one, or, // signed in to it already, takes its new tokens in place of its old; // switching makes another the one in use; signing out takes the one in // use away, the one before it (else the first) in use after. At most `MAX`, // and no more than fit a cookie (`fit`): the earliest added, but never the // one in use, give way first. import type { Session } from './oauth'; export type Kept = { sessions: Session[]; active: number }; /** The most accounts a browser keeps. */ export const MAX = 5; /** The longest a cookie's value is let be: browsers take 4096 bytes of a cookie's name and value, its attributes beside. */ export const COOKIE_MAX = 3800; const isSession = (s: unknown): s is Session => { const o = s as Session | null; return typeof o === 'object' && o !== null && typeof o.access === 'string' && typeof o.clientId === 'string' && typeof o.expiresAt === 'number'; }; /** What a session cookie opened to: the accounts, or one session as a cookie held one before it held several. */ export function keptOf(opened: unknown): Kept | null { const o = opened as Partial | null; if (isSession(o)) return { sessions: [{ ...o, did: typeof o.did === 'string' ? o.did : null }], active: 0 }; if (!o || !Array.isArray(o.sessions) || !o.sessions.every(isSession) || o.sessions.length === 0) return null; const active = typeof o.active === 'number' && o.active >= 0 && o.active < o.sessions.length ? o.active : 0; return { sessions: o.sessions, active }; } /** A session signed in to, in use: in place of the account's own where it is kept (that one `replaced`), else added, the earliest added let go past `MAX` (`dropped`). */ export function added(kept: Kept | null, session: Session): { kept: Kept; replaced: Session | null; dropped: Session[] } { const sessions = [...(kept?.sessions ?? [])]; const at = sessions.findIndex((s) => s.did !== null && s.did === session.did); if (at >= 0) { const replaced = sessions[at]; sessions[at] = session; return { kept: { sessions, active: at }, replaced, dropped: [] }; } sessions.push(session); const dropped = sessions.splice(0, Math.max(0, sessions.length - MAX)); return { kept: { sessions, active: sessions.length - 1 }, replaced: null, dropped }; } /** The account `did` in use; null where it is not kept. */ export function used(kept: Kept, did: string): Kept | null { const at = kept.sessions.findIndex((s) => s.did === did); return at < 0 ? null : { sessions: kept.sessions, active: at }; } /** The session at `at` let go (signed out, or refused by HUD): the one before it in use after where it was the one in use, else the first; null with none left. */ export function removed(kept: Kept, at: number): Kept | null { const sessions = kept.sessions.filter((_, i) => i !== at); if (sessions.length === 0) return null; const active = kept.active === at ? Math.max(0, at - 1) : kept.active > at ? kept.active - 1 : kept.active; return { sessions, active }; } /** A session's new tokens in its place. */ export function renewed(kept: Kept, at: number, session: Session): Kept { return { sessions: kept.sessions.map((s, i) => (i === at ? session : s)), active: kept.active }; } /** The accounts as they fit a cookie, sealed by `seal`: the earliest added let go, but never the one in use, until it does (`dropped`). */ export async function fit(kept: Kept, seal: (kept: Kept) => Promise): Promise<{ sealed: string; kept: Kept; dropped: Session[] }> { let now = kept; const dropped: Session[] = []; for (;;) { const sealed = await seal(now); const first = now.active === 0 ? 1 : 0; if (sealed.length <= COOKIE_MAX || now.sessions.length === 1) return { sealed, kept: now, dropped }; dropped.push(now.sessions[first]); now = removed(now, first) as Kept; } }