Simpl-Resultat/src/utils/startupConnection.ts
le king fu c1d69d23c5 fix: replace the home and profile folders exactly in the Journal (#497)
withoutFolders guesses where a path starts and ends, and five review
passes each found a line it let a folder through: blanks, soft stops, a
kept stop in a folder name, the fallback after a cut, a path glued to a
character that starts nothing. The new src/services/logFolders.ts knows
the two folders that hold the OS user name instead: the home folder
(homeDir) becomes "~" and the folder of the profile databases
(appConfigDir) "<profile folder>", the latter first.

- replaceKnownFolders: either separator and any run of them between
  segments (JSON.stringify doubles each backslash), case ignored with a
  drive letter, trailing separator of the folder dropped, and a
  boundary after it, so /home/max leaves /home/maxime alone. Never
  throws.
- setKnownFolders builds the patterns once, every segment escaped
  (CWE-185: a raw "(" would make every captured console call throw),
  and ignores an empty or root folder.
- initKnownFolders resolves both folders once, independently
  (Promise.allSettled), never rejected; knownFoldersReady resolves when
  they are known or 2 s after initKnownFolders, and at once when it was
  never called.

logService replaces them in every entry, whatever its channel, and
replayLogEntries rewrites the entries written before they arrived,
sessionStorage included; main.tsx wires it right after initLogCapture.
db.ts waits for knownFoldersReady inside the exclusive section of
connectToProfile, before repair_migrations, and its four sites clean a
line with cleanLogLine: the exact folders, then withoutFolders, which
does not change. "Migration checksums repaired for" goes through
logWarn, and startupConnection writes through a logError that App.tsx
passes: neither file writes to the Journal through console.* any more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 21:24:35 -04:00

69 lines
2.2 KiB
TypeScript

// Opening the active profile at startup, out of App.tsx so it can be tested
// without rendering the router (#395).
/** Delay after which the startup error page shows while the profile opens. */
export const STARTUP_TIMEOUT_MS = 10_000;
export const MAX_RETRIES = 3;
export const RETRY_DELAY_MS = 1_000;
export interface StartupConnection {
/** Opens the active profile (`connectActiveProfile`). */
connect: () => Promise<void>;
setDbReady: (ready: boolean) => void;
setStartupError: (error: string | null) => void;
/** Writes a failed attempt to the Journal: `logError` of logService, passed by App.tsx (#497). */
logError: (...args: unknown[]) => void;
/** Shown when the opening outlasts `STARTUP_TIMEOUT_MS`. */
timeoutMessage: string;
}
/**
* Open the profile, retrying up to `MAX_RETRIES` times, and show the error
* page after `STARTUP_TIMEOUT_MS` while it is still opening. The opening goes
* on past the delay (a catch-up of a large profile can, ADR 0021): when it
* then succeeds, the error is cleared and the app shows (#395). Returns the
* cleanup of the effect that calls it.
*/
export function startProfileConnection({
connect,
setDbReady,
setStartupError,
logError,
timeoutMessage,
}: StartupConnection): () => void {
let cancelled = false;
setDbReady(false);
setStartupError(null);
const timeout = setTimeout(() => {
setStartupError(timeoutMessage);
}, STARTUP_TIMEOUT_MS);
const attemptConnect = async (attempt: number): Promise<void> => {
try {
await connect();
if (cancelled) return;
clearTimeout(timeout);
// The delay may have shown the error page already.
setStartupError(null);
setDbReady(true);
} catch (err) {
if (cancelled) return;
logError(`Failed to connect profile (attempt ${attempt}/${MAX_RETRIES}):`, err);
if (attempt < MAX_RETRIES) {
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS));
if (!cancelled) return attemptConnect(attempt + 1);
} else {
clearTimeout(timeout);
setStartupError(err instanceof Error ? err.message : String(err));
}
}
};
void attemptConnect(1);
return () => {
cancelled = true;
clearTimeout(timeout);
};
}