podcast manager
1import { CancellationError } from "@repo/common/errors.ts";
2
3import { errorHandler } from "./handler-error.ts";
4import { preauthHandler } from "./handler-preauth.ts";
5import { realmHandler } from "./handler-realm.ts";
6import { attachSocket, detachSocket } from "./state.ts";
7
8export async function socketHandler(this: WebSocket) {
9 const controller = new AbortController();
10 this.addEventListener("close", () =>
11 queueMicrotask(() => {
12 controller.abort(new CancellationError("socket closed"));
13 }));
14
15 try {
16 const { realm, ...auth } = await preauthHandler(this, controller.signal);
17
18 try {
19 attachSocket(realm, auth.identid, this);
20 await realmHandler(this, realm, auth, controller.signal);
21 } finally {
22 detachSocket(realm, auth.identid, this);
23 }
24 } catch (e) {
25 errorHandler(this, e);
26 } finally {
27 if (this.readyState !== this.CLOSED) {
28 this.send(`kthkbye\n`);
29 this.close();
30 }
31 }
32}