Part · audit.logWhat's actually behind
What's actually behind audit.log
The part exactly as partkit add audit.log vendors it into your repo — verified, locked, every byte readable. Nothing here is mocked.
audit.logv1.1.0
✓ attested🔒 read-onlyaudit.log@1
Lives at parts/audit.log/ in your repo — open, owned, readable. Not buried in node_modules. 317 lines of source you can audit.
content hash
7b1ebe7d9c…d9eb5bpinned in parts.lock — ctrlai guard fails CI if a single byte changestested against
node 25.3.0parts/audit.log/src/index.tstypescript · 2,460 bytes/**
* audit.log — public interface. The ONLY legal import surface.
* Contract: ../contract.json · What your app must provide: ../seams.md
*/
import { AuditError } from "./internal/errors";
import { INSERT_SQL, rowToEvent, SELECT_SQL } from "./internal/sql";
import type {
AuditEvent,
AuditEventInput,
AuditLog,
AuditQuery,
SqlExecutor,
} from "./internal/types";
import { validateEvent, validateQuery } from "./internal/validate";
export { AuditError } from "./internal/errors";
export type { AuditErrorCode } from "./internal/errors";
export type {
AuditEvent,
AuditEventInput,
AuditLog,
AuditQuery,
SqlExecutor,
} from "./internal/types";
/**
* Bind an append-only audit log to a database connection (the SqlExecutor
* seam). Constructing it performs no I/O and never throws — configuration is
* validated, and the database touched, only when `append`/`query` run
* (contract invariant 1, serverless-safe). Pass a per-request executor from
* your pool; the part runs on the connection/transaction you hand it.
*/
export function auditLog(db: SqlExecutor): AuditLog {
return {
append: (event: AuditEventInput): Promise<AuditEvent> => appendEvent(db, event),
query: (filter?: AuditQuery): Promise<AuditEvent[]> => queryEvents(db, filter ?? {}),
};
}
async function appendEvent(db: SqlExecutor, event: AuditEventInput): Promise<AuditEvent> {
const v = validateEvent(event); // throws AuditError("invalid_event") before any SQL
let result: { rows: Record<string, unknown>[] };
try {
result = await db.query(INSERT_SQL, [v.actor, v.action, v.target, v.metadataJson]);
} catch (e) {
throw new AuditError("storage", "failed to append audit event", { cause: e });
}
const row = result.rows[0];
if (row === undefined) {
throw new AuditError("storage", "append returned no row — is the audit_events migration applied?");
}
return rowToEvent(row);
}
async function queryEvents(db: SqlExecutor, filter: AuditQuery): Promise<AuditEvent[]> {
const v = validateQuery(filter); // throws AuditError("invalid_query") before any SQL
let result: { rows: Record<string, unknown>[] };
try {
result = await db.query(SELECT_SQL, [
v.actor,
v.action,
v.target,
v.since,
v.until,
v.before,
v.limit,
]);
} catch (e) {
throw new AuditError("storage", "failed to query audit events", { cause: e });
}
return result.rows.map(rowToEvent);
}