fix: SSE stream 500 — replace error() with plain Response in stream handler

This commit is contained in:
M1 2026-03-17 06:59:43 +04:00
parent 15227b9c6e
commit 749c6f391e
1 changed files with 5 additions and 8 deletions

View File

@ -86,27 +86,24 @@ export const ingest = new Elysia()
}) })
// SSE: stream live pings — auth via Bearer header or cookie // SSE: stream live pings — auth via Bearer header or cookie
.get("/monitors/:id/stream", async ({ params, headers, cookie, error }) => { .get("/monitors/:id/stream", async ({ params, headers, cookie }) => {
// Case-insensitive bearer parsing
const authHeader = headers["authorization"] ?? ""; const authHeader = headers["authorization"] ?? "";
const bearer = authHeader.match(/^bearer\s+(.+)$/i)?.[1]?.trim(); const bearer = authHeader.match(/^bearer\s+(.+)$/i)?.[1]?.trim();
const key = bearer ?? cookie?.pingql_key?.value; const key = bearer ?? cookie?.pingql_key?.value;
if (!key) return error(401, { error: "Unauthorized" }); if (!key) return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
const resolved = await resolveKey(key); const resolved = await resolveKey(key);
if (!resolved) return error(401, { error: "Unauthorized" }); if (!resolved) return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
// Verify ownership
const [monitor] = await sql` const [monitor] = await sql`
SELECT id FROM monitors WHERE id = ${params.id} AND account_id = ${resolved.accountId} SELECT id FROM monitors WHERE id = ${params.id} AND account_id = ${resolved.accountId}
`; `;
if (!monitor) return error(404, { error: "Not found" }); if (!monitor) return new Response(JSON.stringify({ error: "Not found" }), { status: 404 });
// SSE connection limit per monitor
const limit = Number(process.env.MAX_SSE_PER_MONITOR ?? 10); const limit = Number(process.env.MAX_SSE_PER_MONITOR ?? 10);
if ((bus.get(params.id)?.size ?? 0) >= limit) { if ((bus.get(params.id)?.size ?? 0) >= limit) {
return error(429, { error: "Too many connections for this monitor" }); return new Response(JSON.stringify({ error: "Too many connections for this monitor" }), { status: 429 });
} }
return makeSSEStream(params.id); return makeSSEStream(params.id);