atomicWrite (core/safe-write.ts)

career-ops · web/src/lib/core/safe-write.ts · typescript · MIT
files, tag:knn-classified

Module exposing: atomicWrite, backup, atomicWriteWithBackup

// Source: https://github.com/career-ops-hq/career-ops | License: MIT | Copyright 2026 Santiago Fernández de Valderrama
// Provided via justclaw.it.com; keep this notice with the code.
import fs from "node:fs";
import path from "node:path";
import { randomUUID } from "node:crypto";

// THE one place every user-layer write goes through. The core's #1 historical
// pain was data-loss (#649/#704/#920/#958); these guards make a web write
// crash-safe + non-clobbering by construction:
//   - atomic: write a UNIQUE temp file (pid + uuid → no concurrent-write race on
//     a single long-lived Next pid) in the SAME dir, then rename (atomic on POSIX),
//     so a kill mid-write can never truncate the real file.
//   - backup: optionally snapshot the prior contents to {file}.bak-{ts} before
//    

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key