28 lines
1.3 KiB
TypeScript
28 lines
1.3 KiB
TypeScript
// PEP-225: the tab's WebSocket, set up inside the Next process (instrumentation.ts) where the
|
|
// web's modules live — the cookie check, navCounts, the boot id. apps/web/server.ts only hands
|
|
// `upgrade` requests of /api/ws to what is registered here (globalThis: the custom server and
|
|
// the Next bundle are different module graphs of one process).
|
|
import { getNotifyPrefs } from "@peppa/core";
|
|
import { setLiveProvider } from "@peppa/runner";
|
|
import { createLiveWs, readCookie, type LiveWs } from "@peppa/runner/live-ws";
|
|
import { bootId } from "./boot";
|
|
import { db } from "./db";
|
|
import { navCounts } from "./queries";
|
|
import { SESSION_COOKIE, userFromSessionToken } from "./session-user";
|
|
|
|
const g = globalThis as { __peppaLiveWs?: LiveWs };
|
|
|
|
export function registerLiveWs(): void {
|
|
setLiveProvider("attention", async (_db, userId) => {
|
|
const [c, { sound }] = await Promise.all([navCounts(userId), getNotifyPrefs(db, userId)]);
|
|
return { count: c.questions + c.waitingRunners, questions: c.questions, waitingRunners: c.waitingRunners, chats: c.chats, sound };
|
|
});
|
|
setLiveProvider("version", () => ({ boot: bootId() }));
|
|
g.__peppaLiveWs?.close();
|
|
g.__peppaLiveWs = createLiveWs({
|
|
db,
|
|
authenticate: (req) => userFromSessionToken(readCookie(req.headers.cookie, SESSION_COOKIE)),
|
|
boot: bootId,
|
|
});
|
|
}
|