← Files RivetARCHIVED FILE
node_modules/rivet-design/dist/utils/writeFileAtomic.js
1.51 KB · Nov 5, 2026 · 12:09 UTC
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFileAtomic = exports.writeFileAtomicSync = void 0;
const crypto_1 = require("crypto");
const fs_1 = __importDefault(require("fs"));
const promises_1 = __importDefault(require("fs/promises"));
const temporaryPathFor = (filePath) => `${filePath}.${(0, crypto_1.randomUUID)()}.tmp`;
const writeFileAtomicSync = (filePath, data, { mode }) => {
const temporaryPath = temporaryPathFor(filePath);
try {
fs_1.default.writeFileSync(temporaryPath, data, { mode, flag: 'wx' });
fs_1.default.chmodSync(temporaryPath, mode);
fs_1.default.renameSync(temporaryPath, filePath);
}
catch (error) {
fs_1.default.rmSync(temporaryPath, { force: true });
throw error;
}
};
exports.writeFileAtomicSync = writeFileAtomicSync;
const writeFileAtomic = async (filePath, data, { mode }) => {
const temporaryPath = temporaryPathFor(filePath);
try {
await promises_1.default.writeFile(temporaryPath, data, { mode, flag: 'wx' });
await promises_1.default.chmod(temporaryPath, mode);
await promises_1.default.rename(temporaryPath, filePath);
}
catch (error) {
await promises_1.default.rm(temporaryPath, { force: true });
throw error;
}
};
exports.writeFileAtomic = writeFileAtomic;
//# sourceMappingURL=writeFileAtomic.js.mapSHA-256: fec7d6438c573619a596db1547ad0a284268b23412d75ebcdeee3ecc3bbfed51