Files
Stirling-PDF/frontend/src/desktop/services/backendReadinessGuard.ts
T
James BruntonandGitHub 8ab060a4be Prettier 2: Electric Boogaloo (#6113)
# Description of Changes
When I added Prettier formatting in #6052, my aim was to use just the
default settings in Prettier. Turns out, Prettier looks _really hard_
for any config files if it's not explicitly given one, which means that
if a developer has some sort of Prettier config file lying around on
their system, Prettier might find it and use it. Also, Prettier changes
its defaults based on stuff in `.editorconfig` without any good way of
disabling that behaviour explicitly in its config file.

To solve both of these issues, I've introduced a `.prettierrc` file
which sets Prettier's defaults explicitly, and then reformatted all our
code _again_ in Prettier's actual default settings. This should achieve
the aim of #6052 and remove the possibility for it breaking on different
dev computers.
2026-04-17 09:50:16 +00:00

88 lines
3.3 KiB
TypeScript

import i18n from "@app/i18n";
import { alert } from "@app/components/toast";
import { tauriBackendService } from "@app/services/tauriBackendService";
import { operationRouter } from "@app/services/operationRouter";
import { connectionModeService } from "@app/services/connectionModeService";
import { selfHostedServerMonitor } from "@app/services/selfHostedServerMonitor";
const BACKEND_TOAST_COOLDOWN_MS = 4000;
let lastBackendToast = 0;
/**
* Desktop-specific guard that ensures the relevant backend is ready before
* tools attempt to call any API endpoints.
*
* - SaaS mode: checks the local bundled backend via tauriBackendService.isOnline
* - Self-hosted mode (server online/checking): allows through — the operation
* targets the remote server and will surface network errors naturally
* - Self-hosted mode (server confirmed offline): allows through if local port is
* known (operationRouter falls back to local); suppresses toast since
* SelfHostedOfflineBanner already communicates the outage
*
* @param endpoint - Optional endpoint path to check if it needs local backend
* @returns true if backend is ready OR endpoint will be routed to SaaS
*/
export async function ensureBackendReady(endpoint?: string): Promise<boolean> {
// Skip waiting if endpoint will be routed to SaaS backend
if (endpoint) {
const skipCheck =
await operationRouter.shouldSkipBackendReadyCheck(endpoint);
if (skipCheck) {
console.debug(
"[backendReadinessGuard] Skipping backend ready check (SaaS routing)",
);
return true;
}
}
const mode = await connectionModeService.getCurrentMode();
if (mode === "selfhosted") {
let { status } = selfHostedServerMonitor.getSnapshot();
// 'checking' means the first poll hasn't returned yet. Wait briefly (up to
// 1.5 s) for it to resolve so we don't surface raw network errors during the
// first few seconds after launch. If it doesn't resolve in time we fall
// through and allow the operation — the HTTP layer will handle any error.
if (status === "checking") {
await Promise.race([
selfHostedServerMonitor.checkNow(),
new Promise<void>((resolve) => setTimeout(resolve, 1500)),
]);
status = selfHostedServerMonitor.getSnapshot().status;
}
if (status === "offline") {
// Server offline: allow through if local backend port is known.
// operationRouter will route to local for supported endpoints.
// Suppress the toast — SelfHostedOfflineBanner communicates the outage.
return !!tauriBackendService.getBackendUrl();
}
// Server online: allow through — the operation targets the remote server.
return true;
}
// SaaS mode: check local bundled backend
if (tauriBackendService.isOnline) {
return true;
}
// Trigger a fresh check so we get the latest status
await tauriBackendService.checkBackendHealth();
if (tauriBackendService.isOnline) {
return true;
}
const now = Date.now();
if (now - lastBackendToast > BACKEND_TOAST_COOLDOWN_MS) {
lastBackendToast = now;
alert({
alertType: "error",
title: i18n.t("backendHealth.offline", "Backend Offline"),
body: i18n.t("backendHealth.checking", "Checking backend status..."),
isPersistentPopup: false,
});
}
return false;
}