17 lines
568 B
JavaScript
17 lines
568 B
JavaScript
|
|
// Small JSON files written atomically: the gateway's whole state is files
|
||
|
|
// under its root, so a backup is a copy of one folder.
|
||
|
|
|
||
|
|
import fs from 'node:fs';
|
||
|
|
import path from 'node:path';
|
||
|
|
|
||
|
|
export function readJson(file, fallback) {
|
||
|
|
try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return fallback; }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function writeJson(file, value) {
|
||
|
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
||
|
|
const tmp = `${file}.${process.pid}.tmp`;
|
||
|
|
fs.writeFileSync(tmp, JSON.stringify(value, null, 2), { mode: 0o600 });
|
||
|
|
fs.renameSync(tmp, file);
|
||
|
|
}
|