-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsafe-write.js
More file actions
27 lines (26 loc) · 1.02 KB
/
Copy pathsafe-write.js
File metadata and controls
27 lines (26 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { writeFileSync, renameSync, rmSync } from "node:fs";
/**
* Atomically write `data` to `path` via a `${path}.tmp` staging file and
* `renameSync`. On Windows, Node's renameSync uses MoveFileExW with
* MOVEFILE_REPLACE_EXISTING, so the destination is replaced atomically with
* no rmSync window. On any failure, best-effort delete the `.tmp` file and
* re-throw the original error.
*
* The caller is responsible for `mkdirSync` of the parent directory and for
* any post-write `chmod` / permissions hardening; this helper only owns the
* write+rename pair.
*
* @param {string} path Final destination path.
* @param {string|Buffer|Uint8Array} data Bytes to write.
* @param {string|object} [opts] Encoding string or fs.writeFileSync options.
*/
export function safeAtomicWriteFileSync(path, data, opts) {
const tmp = `${path}.tmp`;
try {
writeFileSync(tmp, data, opts);
renameSync(tmp, path);
} catch (err) {
try { rmSync(tmp, { force: true }); } catch { /* best-effort cleanup */ }
throw err;
}
}