fix: SSE stream 500 — replace error() with plain Response in stream handler
This commit is contained in:
parent
15227b9c6e
commit
749c6f391e
|
|
@ -86,27 +86,24 @@ export const ingest = new Elysia()
|
|||
})
|
||||
|
||||
// SSE: stream live pings — auth via Bearer header or cookie
|
||||
.get("/monitors/:id/stream", async ({ params, headers, cookie, error }) => {
|
||||
// Case-insensitive bearer parsing
|
||||
.get("/monitors/:id/stream", async ({ params, headers, cookie }) => {
|
||||
const authHeader = headers["authorization"] ?? "";
|
||||
const bearer = authHeader.match(/^bearer\s+(.+)$/i)?.[1]?.trim();
|
||||
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);
|
||||
if (!resolved) return error(401, { error: "Unauthorized" });
|
||||
if (!resolved) return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
|
||||
|
||||
// Verify ownership
|
||||
const [monitor] = await sql`
|
||||
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);
|
||||
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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue