fix: deny-by-default API auth + safe SSE controller

This commit is contained in:
decolua
2026-05-15 12:41:52 +07:00
parent 6493391415
commit bb86808582
3 changed files with 53 additions and 28 deletions
@@ -36,8 +36,14 @@ export async function POST(request) {
const encoder = new TextEncoder();
const stream = new ReadableStream({
async start(controller) {
let closed = false;
const send = (event, data) => {
controller.enqueue(encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`));
if (closed) return;
try {
controller.enqueue(encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`));
} catch {
closed = true;
}
};
try {
@@ -52,7 +58,7 @@ export async function POST(request) {
: error.message;
send("error", { error: msg });
} finally {
controller.close();
if (!closed) { try { controller.close(); } catch {} }
}
},
});