mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-23 04:09:49 +00:00
feat :
- Added tunnel - Removed cloud feature
This commit is contained in:
@@ -1,22 +1,6 @@
|
||||
import { getProviderConnectionById, updateProviderConnection } from "@/lib/localDb";
|
||||
import { getMachineId } from "@/shared/utils/machine";
|
||||
import { getUsageForProvider } from "open-sse/services/usage.js";
|
||||
import { getExecutor } from "open-sse/executors/index.js";
|
||||
import { syncToCloud } from "@/app/api/sync/cloud/route";
|
||||
|
||||
/**
|
||||
* Sync to cloud if enabled
|
||||
*/
|
||||
async function syncToCloudIfEnabled() {
|
||||
try {
|
||||
const machineId = await getMachineId();
|
||||
if (!machineId) return;
|
||||
await syncToCloud(machineId);
|
||||
} catch (error) {
|
||||
console.error("[Usage API] Error syncing to cloud:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh credentials using executor and update database
|
||||
* @returns {{ connection, refreshed: boolean }}
|
||||
@@ -119,16 +103,9 @@ export async function GET(request, { params }) {
|
||||
}
|
||||
|
||||
// Refresh credentials if needed using executor
|
||||
let refreshed = false;
|
||||
try {
|
||||
const result = await refreshAndUpdateCredentials(connection);
|
||||
connection = result.connection;
|
||||
refreshed = result.refreshed;
|
||||
|
||||
// Sync to cloud only if token was refreshed
|
||||
if (refreshed) {
|
||||
await syncToCloudIfEnabled();
|
||||
}
|
||||
} catch (refreshError) {
|
||||
console.error("[Usage API] Credential refresh failed:", refreshError);
|
||||
return Response.json({
|
||||
|
||||
@@ -1,33 +1,53 @@
|
||||
import { getUsageStats, statsEmitter } from "@/lib/usageDb";
|
||||
import { getUsageStats, statsEmitter, getActiveRequests } from "@/lib/usageDb";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET() {
|
||||
const encoder = new TextEncoder();
|
||||
const state = { closed: false, keepalive: null, send: null };
|
||||
const state = { closed: false, keepalive: null, send: null, sendPending: null, cachedStats: null };
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
// Full stats refresh (heavy) + immediate lightweight push
|
||||
state.send = async () => {
|
||||
if (state.closed) return;
|
||||
try {
|
||||
const stats = await getUsageStats();
|
||||
if (stats.activeRequests?.length > 0) {
|
||||
console.log(`[SSE] Push | active=${stats.activeRequests.length} | ${stats.activeRequests.map(r => r.provider).join(",")}`);
|
||||
// Push lightweight update immediately so UI reflects changes fast
|
||||
if (state.cachedStats) {
|
||||
const { activeRequests, recentRequests, errorProvider } = await getActiveRequests();
|
||||
const quickStats = { ...state.cachedStats, activeRequests, recentRequests, errorProvider };
|
||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(quickStats)}\n\n`));
|
||||
}
|
||||
// Then do full recalc and update cache
|
||||
const stats = await getUsageStats();
|
||||
state.cachedStats = stats;
|
||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(stats)}\n\n`));
|
||||
} catch {
|
||||
// Controller closed → self-cleanup
|
||||
state.closed = true;
|
||||
statsEmitter.off("update", state.send);
|
||||
statsEmitter.off("pending", state.sendPending);
|
||||
clearInterval(state.keepalive);
|
||||
}
|
||||
};
|
||||
|
||||
// Lightweight push: only refresh activeRequests + recentRequests on pending changes
|
||||
state.sendPending = async () => {
|
||||
if (state.closed || !state.cachedStats) return;
|
||||
try {
|
||||
const { activeRequests, recentRequests, errorProvider } = await getActiveRequests();
|
||||
const stats = { ...state.cachedStats, activeRequests, recentRequests, errorProvider };
|
||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(stats)}\n\n`));
|
||||
} catch {
|
||||
state.closed = true;
|
||||
statsEmitter.off("pending", state.sendPending);
|
||||
}
|
||||
};
|
||||
|
||||
await state.send();
|
||||
console.log(`[SSE] Client connected | listeners=${statsEmitter.listenerCount("update") + 1}`);
|
||||
|
||||
statsEmitter.on("update", state.send);
|
||||
statsEmitter.on("pending", state.sendPending);
|
||||
|
||||
state.keepalive = setInterval(() => {
|
||||
if (state.closed) { clearInterval(state.keepalive); return; }
|
||||
@@ -43,6 +63,7 @@ export async function GET() {
|
||||
cancel() {
|
||||
state.closed = true;
|
||||
statsEmitter.off("update", state.send);
|
||||
statsEmitter.off("pending", state.sendPending);
|
||||
clearInterval(state.keepalive);
|
||||
console.log("[SSE] Client disconnected");
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user