fix: harden audit durability and fallback config writes

This commit is contained in:
Sucukdeluxe
2026-08-13 21:07:28 +02:00
parent 07c81c0123
commit 76ad81a0d3
4 changed files with 177 additions and 2 deletions
+22 -2
View File
@@ -6,6 +6,26 @@ function getUploadAuditLogPath(uploadLogPath, pathApi = nodePath) {
return pathApi.join(pathApi.dirname(uploadLogPath), 'upload-audit.log');
}
async function appendDurably(fs, targetPath, line) {
const handle = await fs.promises.open(targetPath, 'a');
let appendError = null;
let closeError = null;
try {
await handle.appendFile(line, 'utf-8');
await handle.sync();
} catch (error) {
appendError = error;
}
try {
await handle.close();
} catch (error) {
closeError = error;
}
if (appendError && closeError) throw new AggregateError([appendError, closeError], 'Audit append and close failed');
if (appendError) throw appendError;
if (closeError) throw closeError;
}
function createUploadAuditWriter(options) {
const source = options && typeof options === 'object' ? options : {};
const fs = source.fs;
@@ -20,7 +40,7 @@ function createUploadAuditWriter(options) {
const maxBackups = Number.isFinite(source.maxBackups) ? source.maxBackups : 2;
let activePath = null;
if (!fs || !fs.promises || typeof fs.promises.appendFile !== 'function' || typeof resolveUploadLogTarget !== 'function') {
if (!fs || !fs.promises || typeof fs.promises.open !== 'function' || typeof resolveUploadLogTarget !== 'function') {
throw new TypeError('createUploadAuditWriter requires fs and resolveUploadLogTarget');
}
@@ -49,7 +69,7 @@ function createUploadAuditWriter(options) {
}
}
rotateLogFile(targetPath, maxBytes, maxBackups);
await fs.promises.appendFile(targetPath, line, 'utf-8');
await appendDurably(fs, targetPath, line);
activePath = targetPath;
return true;
} catch (error) {