Blame · Line-by-line history
events.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 6522084 | 1 | /** |
| 2 | * SSE event stream routes — real-time updates for gate runs, PRs, and notifications. | |
| 3 | * | |
| 4 | * GET /api/events/stream?channels=gate:repoId,pr:prId,notification | |
| 5 | */ | |
| 6 | ||
| 7 | import { Hono } from "hono"; | |
| 8 | import { softAuth } from "../middleware/auth"; | |
| 9 | import type { AuthEnv } from "../middleware/auth"; | |
| 10 | import { createSSEStream, getActiveConnections } from "../lib/sse"; | |
| 11 | ||
| 12 | const events = new Hono<AuthEnv>(); | |
| 13 | ||
| 14 | events.get("/api/events/stream", softAuth, (c) => { | |
| 15 | const user = c.get("user"); | |
| 16 | const channelParam = c.req.query("channels") || ""; | |
| 17 | const requestedChannels = channelParam | |
| 18 | .split(",") | |
| 19 | .map((ch) => ch.trim()) | |
| 20 | .filter(Boolean); | |
| 21 | ||
| 22 | if (requestedChannels.length === 0) { | |
| 23 | return c.json({ error: "No channels specified" }, 400); | |
| 24 | } | |
| 25 | ||
| 26 | // Auto-add user-specific notification channel if authenticated | |
| 27 | const channels = [...requestedChannels]; | |
| 28 | if (user && !channels.some((ch) => ch.startsWith("notification"))) { | |
| 29 | channels.push(`notification:${user.id}`); | |
| 30 | } | |
| 31 | ||
| 32 | return createSSEStream(channels, user?.id); | |
| 33 | }); | |
| 34 | ||
| 35 | events.get("/api/events/health", (c) => { | |
| 36 | return c.json({ connections: getActiveConnections() }); | |
| 37 | }); | |
| 38 | ||
| 39 | export default events; |