Feat : console log

This commit is contained in:
decolua 2026-03-02 09:31:16 +07:00
parent 50990e84b4
commit 4903a9b2cb
15 changed files with 323 additions and 30 deletions

View file

@ -0,0 +1,24 @@
import { NextResponse } from "next/server";
import { clearConsoleLogs, getConsoleLogs, initConsoleLogCapture } from "@/lib/consoleLogBuffer";
initConsoleLogCapture();
export async function GET() {
try {
const logs = getConsoleLogs();
return NextResponse.json({ success: true, logs });
} catch (error) {
console.error("Error getting console logs:", error);
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
}
}
export async function DELETE() {
try {
clearConsoleLogs();
return NextResponse.json({ success: true });
} catch (error) {
console.error("Error clearing console logs:", error);
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
}
}

View file

@ -0,0 +1,70 @@
import { getConsoleLogs, getConsoleEmitter, initConsoleLogCapture } from "@/lib/consoleLogBuffer";
export const dynamic = "force-dynamic";
initConsoleLogCapture();
export async function GET() {
const encoder = new TextEncoder();
const emitter = getConsoleEmitter();
const state = { closed: false, send: null, keepalive: null };
const stream = new ReadableStream({
start(controller) {
// Send all buffered logs immediately on connect
const buffered = getConsoleLogs();
if (buffered.length > 0) {
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: "init", logs: buffered })}\n\n`));
}
// Push new lines as they arrive
state.send = (line) => {
if (state.closed) return;
try {
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: "line", line })}\n\n`));
} catch {
state.closed = true;
}
};
// Notify client when cleared
state.sendClear = () => {
if (state.closed) return;
try {
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: "clear" })}\n\n`));
} catch {
state.closed = true;
}
};
emitter.on("line", state.send);
emitter.on("clear", state.sendClear);
// Keepalive ping every 25s
state.keepalive = setInterval(() => {
if (state.closed) { clearInterval(state.keepalive); return; }
try {
controller.enqueue(encoder.encode(": ping\n\n"));
} catch {
state.closed = true;
clearInterval(state.keepalive);
}
}, 25000);
},
cancel() {
state.closed = true;
emitter.off("line", state.send);
emitter.off("clear", state.sendClear);
clearInterval(state.keepalive);
},
});
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
},
});
}

View file

@ -38,7 +38,6 @@ function compareVersions(a, b) {
export async function GET() {
const latestVersion = await fetchLatestVersion();
console.log("🚀 ~ GET ~ latestVersion:", latestVersion)
const currentVersion = pkg.version;
const hasUpdate = latestVersion ? compareVersions(latestVersion, currentVersion) > 0 : false;