refactor(main): split main.ts into focused runtime modules (#123)

This commit is contained in:
2026-06-12 17:35:46 -07:00
committed by GitHub
parent 94a65416ae
commit 33e767458f
32 changed files with 3582 additions and 2003 deletions
+38
View File
@@ -1,4 +1,5 @@
import fs from 'node:fs';
import { execFileSync } from 'node:child_process';
import path from 'node:path';
export type BackgroundStatsServerState = {
@@ -65,6 +66,43 @@ export function isBackgroundStatsServerProcessAlive(pid: number): boolean {
}
}
function readProcessStartedAtMs(pid: number): number | null {
try {
if (process.platform === 'win32') {
const output = execFileSync(
'powershell.exe',
[
'-NoProfile',
'-Command',
`(Get-CimInstance Win32_Process -Filter "ProcessId=${pid}").CreationDate.ToUniversalTime().ToString("o")`,
],
{ encoding: 'utf8', timeout: 1000 },
).trim();
const parsed = Date.parse(output);
return Number.isFinite(parsed) ? parsed : null;
}
const output = execFileSync('ps', ['-o', 'lstart=', '-p', String(pid)], {
encoding: 'utf8',
timeout: 1000,
}).trim();
const parsed = Date.parse(output);
return Number.isFinite(parsed) ? parsed : null;
} catch {
return null;
}
}
export function verifyBackgroundStatsServerIdentity(pid: number, startedAtMs: number): boolean {
const processStartedAtMs = readProcessStartedAtMs(pid);
if (processStartedAtMs === null) {
return false;
}
const earliestAllowedStateWriteMs = processStartedAtMs;
const latestAllowedStateWriteMs = processStartedAtMs + 60_000;
return startedAtMs >= earliestAllowedStateWriteMs && startedAtMs <= latestAllowedStateWriteMs;
}
export function resolveBackgroundStatsServerUrl(
state: Pick<BackgroundStatsServerState, 'port'>,
): string {