flybrain/services/bridge/src/atomic-file.ts
acamilo 660c3cf00d
Some checks failed
ci / node 22 (test + typecheck) (push) Has been cancelled
ci / rust stable (cargo test --workspace --release) (push) Has been cancelled
ci / infra/tests/lint.sh (push) Has been cancelled
ci / playwright apps/stage (allowed to fail) (push) Has been cancelled
flybrain v0.4.0: public tree (history retained privately)
2026-09-21 15:09:46 +00:00

20 lines
826 B
TypeScript

/**
* Atomic, mode-0600 JSON file writes, shared by `tools/authorize.mts` (tokens.json) and
* `src/redemptions.ts` (the redemption intent log). Writes to a temp file in the same directory
* and renames over the target, so a crash mid-write never leaves a truncated file behind.
*/
import { mkdir, rename, unlink, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
export async function atomicWriteJson(path: string, data: unknown): Promise<void> {
await mkdir(dirname(path), { recursive: true });
const tempPath = `${path}.${process.pid}.${Date.now()}.tmp`;
const json = JSON.stringify(data, null, 2);
try {
await writeFile(tempPath, json, { mode: 0o600 });
await rename(tempPath, path);
} catch (cause) {
await unlink(tempPath).catch(() => undefined);
throw cause;
}
}