feat(launcher): add the launcher application skeleton
The installer and launcher is an Electron app in apps/launcher: main process, a sandboxed preload that only forwards allowed channels, and a React window translated into Chinese and English. Every renderer -> main call is declared once in the IPC contract (shared/ipc/contract.ts) with zod schemas for its input and output; the main side validates both when it registers a handler. The main process throws error codes only, and the window translates them. The launcher's own settings (for now the window language) are kept in the user data folder and written through a temporary file and a rename. Development runs can point the user data folder elsewhere with WTO_LAUNCHER_USER_DATA. The workspace packages (@wto/patcher, @wto/server) stay external to the bundle: they ship as TypeScript sources and will run in utility processes, where Electron's Node strips the types and they find their web pages, tuning and keys next to their sources as they do today. The root typecheck leaves out the launcher, which has its own tsconfig files for the main process and the window. Checked with the launcher typecheck, electron-vite build, the root typecheck, and a start on a scratch user data folder.
This commit is contained in:
1 parent
930ea07630
commit
70d59d7bf7
30 files changed
+3484
-1
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
||||
import { dirname } from 'node:path';
|
||||
|
||||
/**
|
||||
* JSON files the launcher keeps: written to a temporary file and renamed over the target, so a
|
||||
* crash or power loss mid-write never leaves a truncated file.
|
||||
*/
|
||||
|
||||
/** The parsed file, or `fallback` when it does not exist. Any other read error is thrown. */
|
||||
export async function readJsonOr(path: string, fallback: unknown): Promise<unknown> {
|
||||
let text: string;
|
||||
try {
|
||||
text = await readFile(path, 'utf8');
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return fallback;
|
||||
throw error;
|
||||
}
|
||||
return JSON.parse(text) as unknown;
|
||||
}
|
||||
|
||||
export async function writeJsonAtomic(path: string, value: unknown): Promise<void> {
|
||||
await mkdir(dirname(path), { recursive: true });
|
||||
// A random suffix keeps two writes of the same file from renaming each other's halves.
|
||||
const temporary = `${path}.${randomBytes(4).toString('hex')}.tmp`;
|
||||
await writeFile(temporary, JSON.stringify(value, null, 2) + '\n', 'utf8');
|
||||
try {
|
||||
await renameWithRetry(temporary, path);
|
||||
} catch (error) {
|
||||
await unlink(temporary).catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A virus scanner or the search indexer can hold a just-written file open for a moment, and the
|
||||
* rename over it then fails with EPERM, EACCES or EBUSY. Retry those briefly.
|
||||
*/
|
||||
async function renameWithRetry(from: string, to: string, attempts = 6): Promise<void> {
|
||||
for (let attempt = 1; ; attempt++) {
|
||||
try {
|
||||
await rename(from, to);
|
||||
return;
|
||||
} catch (error) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
const transient = code === 'EPERM' || code === 'EACCES' || code === 'EBUSY';
|
||||
if (!transient || attempt >= attempts) throw error;
|
||||
await new Promise((resolve) => setTimeout(resolve, 20 * attempt));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user