File
Blob: src/worker/routes/api/session.ts
| 1 | import { createControlPlaneDb } from "@/worker/db/d1/client"; |
| 2 | import { getSubjectById } from "@/worker/db/d1/repository"; |
| 3 | import type { AppContext } from "@/worker/types"; |
| 4 | |
| 5 | export function requireSession(c: AppContext) { |
| 6 | const session = c.get("session"); |
| 7 | if (!session) throw new Error("Session middleware did not run"); |
| 8 | return session; |
| 9 | } |
| 10 | |
| 11 | export async function subjectForSession(c: AppContext) { |
| 12 | const session = requireSession(c); |
| 13 | const subject = await getSubjectById(createControlPlaneDb(c.env.DAV_CONTROL_PLANE), session.subjectId); |
| 14 | if (!subject || subject.storageId !== session.storageId) return null; |
| 15 | return subject; |
| 16 | } |
| 17 | |
| 18 | export function msToIso(value: number | null | undefined): string | null { |
| 19 | return value === null || value === undefined ? null : new Date(value).toISOString(); |
| 20 | } |