Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/app/api/translator/console-logs/stream/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@ export const dynamic = "force-dynamic";

initConsoleLogCapture();

export async function GET() {
export async function GET(request) {
const encoder = new TextEncoder();
const emitter = getConsoleEmitter();
const state = { closed: false, send: null, keepalive: null };
const state = { closed: false, send: null, sendClear: null, keepalive: null };

// Idempotent: safe to call from request.signal abort, cancel(), or enqueue failure.
const cleanup = () => {
if (state.closed) return;
state.closed = true;
if (state.send) emitter.off("line", state.send);
if (state.sendClear) emitter.off("clear", state.sendClear);
if (state.keepalive) clearInterval(state.keepalive);
};

// request.signal fires reliably on client disconnect; ReadableStream.cancel()
// is not always invoked in Next.js, which caused listeners to accumulate.
request.signal.addEventListener("abort", cleanup, { once: true });

const stream = new ReadableStream({
start(controller) {
Expand All @@ -23,7 +36,7 @@ export async function GET() {
try {
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: "line", line })}\n\n`));
} catch {
state.closed = true;
cleanup();
}
};

Expand All @@ -33,7 +46,7 @@ export async function GET() {
try {
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: "clear" })}\n\n`));
} catch {
state.closed = true;
cleanup();
}
};

Expand All @@ -46,17 +59,13 @@ export async function GET() {
try {
controller.enqueue(encoder.encode(": ping\n\n"));
} catch {
state.closed = true;
clearInterval(state.keepalive);
cleanup();
}
}, 25000);
},

cancel() {
state.closed = true;
emitter.off("line", state.send);
emitter.off("clear", state.sendClear);
clearInterval(state.keepalive);
cleanup();
},
});

Expand Down