/** * Establish the persistent session cookie (PLAN.md section 3.3) before the * game WebSocket connects. * * The `/session` endpoint mints a session id on the first call and sets the * `x-haven-session` cookie (HttpOnly). The browser then carries it on every * same-origin request — including the `/ws` upgrade — so a player keeps the * same session id across page reloads *and* server restarts. That is what * lets the server's restored seats (persisted and keyed by session id) land * back on the right player after a crash; without the cookie the upgrade * would mint a fresh session and the restored seat would be orphaned. * * The id also arrives in the `welcome` message, but that is too late to seed * the cookie the upgrade depends on — hence this explicit bootstrap call. */ export async function establishSessionCookie(): Promise { try { // `same-origin` credentials send/receive the cookie; the response // sets it if the browser has none yet, and is a no-op set otherwise. await fetch('/session', { credentials: 'same-origin' }); } catch { // Offline or blocked: the WS upgrade will still (re)mint a session; // the tab just starts as a fresh session instead of a restored one. } }