24 lines
766 B
JavaScript
24 lines
766 B
JavaScript
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
export class AuditLog {
|
|
constructor(filePath) {
|
|
this.filePath = filePath ? path.resolve(filePath) : null;
|
|
this.pending = Promise.resolve();
|
|
}
|
|
|
|
record(event) {
|
|
const line = `${JSON.stringify({ timestamp: new Date().toISOString(), ...event })}\n`;
|
|
if (!this.filePath) {
|
|
process.stderr.write(`[mcp-audit] ${line}`);
|
|
return;
|
|
}
|
|
this.pending = this.pending
|
|
.then(() => fs.mkdir(path.dirname(this.filePath), { recursive: true }))
|
|
.then(() => fs.appendFile(this.filePath, line, { encoding: "utf8", mode: 0o600 }))
|
|
.catch(() => {
|
|
// Auditing must not make a coding operation unavailable. Deployment should monitor this path.
|
|
});
|
|
}
|
|
}
|