- Add `app` export and wrap startup logic behind `if (import.meta.main)` - Extract `hono` middleware into `sdk/hono.ts` for clean separation - Refactor module imports slightly to support in-memory native mocking (`db`, `valkey`, `spire_ffi`, `ratelimit`, `audit`) - Implement comprehensive native Deno mock tests in `server/main.test.ts` - Fix type checking across project files Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
34 lines
916 B
TypeScript
34 lines
916 B
TypeScript
import { sqlWrapper } from "./db.ts";
|
|
|
|
/**
|
|
* SIDE EFFECT: Asynchronously logs an audit record to the database.
|
|
* Does not block the main execution thread. Errors are logged but swallowed
|
|
* to prevent failing the core request due to a logging issue.
|
|
*/
|
|
export let auditLog = function auditLog(
|
|
userId: string | null,
|
|
action: string,
|
|
resource: string | null,
|
|
details: Record<string, unknown> | null,
|
|
ipAddress: string,
|
|
): void {
|
|
// Fire and forget
|
|
sqlWrapper.sql`
|
|
INSERT INTO audit_records (user_id, action, resource, details, ip_address)
|
|
VALUES (${userId}, ${action}, ${resource}, ${
|
|
details ? JSON.stringify(details) : null
|
|
}, ${ipAddress})
|
|
`.catch((error: any) => {
|
|
console.error("[Audit Logger] Failed to insert audit record:", error);
|
|
});
|
|
};
|
|
|
|
export const auditWrapper = {
|
|
get auditLog() {
|
|
return auditLog;
|
|
},
|
|
set auditLog(val: any) {
|
|
auditLog = val;
|
|
},
|
|
};
|