| 251 | // HTTP Server |
| 252 | // ---------------------------------------------------------------------------- |
| 253 | function requireAuth(req: Request): string { |
| 254 | const header = req.headers.get("Authorization") ?? ""; |
| 255 | const m = header.match(/^Bearer\s+(.+)$/i); |
| 256 | if (!m) throw new Error("401"); |
| 257 | const token = m[1].trim(); |
| 258 | if (!VALID_CLIENT_KEYS.has(token)) throw new Error("403"); |
| 259 | return token; |
| 260 | } |
| 261 | |
| 262 | function jsonResponse(obj: unknown, status = 200) { |
| 263 | return new Response(JSON.stringify(obj), { |