SPB Git

Repository Public

audit.ts 2.8 KB 45 lines typescript

1import { canonicalJson, sha256 } from "./crypto";2import { all, get, now, run } from "./db";34/** Stores a value in the content-addressed blob table and returns its SHA-256. */5export function putBlob(body: string | Uint8Array): string {6  const buf = typeof body === "string" ? Buffer.from(body, "utf8") : Buffer.from(body);7  const hash = sha256(buf);8  run("INSERT OR IGNORE INTO blobs (sha256, bytes, body, created_at) VALUES (?, ?, ?, ?)", hash, buf.length, buf, now());9  return hash;10}11export function getBlob(hash: string): Buffer | null {12  const r = get<{ body: Uint8Array }>("SELECT body FROM blobs WHERE sha256 = ?", hash);13  return r ? Buffer.from(r.body) : null;14}15export const getBlobText = (hash: string) => getBlob(hash)?.toString("utf8") ?? null;16export const putJson = (value: unknown) => putBlob(canonicalJson(value));17export function getJson<T>(hash: string | null | undefined): T | null {18  const text = hash ? getBlobText(hash) : null;19  return text === null ? null : JSON.parse(text) as T;20}2122/** `before`/`after` are snapshots (stored as blobs); `beforeRef`/`afterRef` name blobs that already exist. */23export interface AuditInput { actor?: string; target?: string; before?: unknown; after?: unknown; beforeRef?: string | null; afterRef?: string | null; detail?: unknown; ip?: string }2425/**26 * Append-only trail of every change: who, when, what, and a reference to the state before and after (the27 * SHA-256 of a snapshot kept in the blob table, so the change can be inspected later). Never receives28 * secrets: tokens, cookies and key material are not passed to it by any call site.29 */30export function audit(action: string, o: AuditInput = {}): void {31  run("INSERT INTO audit (at, actor, action, target, before_ref, after_ref, detail, ip) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",32    now(), o.actor ?? "system", action, o.target ?? null,33    o.beforeRef ?? (o.before === undefined || o.before === null ? null : putJson(o.before)), o.afterRef ?? (o.after === undefined || o.after === null ? null : putJson(o.after)),34    o.detail === undefined ? null : JSON.stringify(o.detail), o.ip ?? null);35}3637export interface AuditRow { id: number; at: string; actor: string; action: string; target: string | null; before_ref: string | null; after_ref: string | null; detail: string | null; ip: string | null }38export function listAudit(o: { limit?: number; before?: number; action?: string; target?: string } = {}): AuditRow[] {39  const where: string[] = [], params: (string | number)[] = [];40  if (o.before) { where.push("id < ?"); params.push(o.before); }41  if (o.action) { where.push("action LIKE ?"); params.push(`${o.action}%`); }42  if (o.target) { where.push("target = ?"); params.push(o.target); }43  return all<AuditRow>(`SELECT * FROM audit ${where.length ? `WHERE ${where.join(" AND ")}` : ""} ORDER BY id DESC LIMIT ?`, ...params, Math.min(o.limit ?? 100, 500));44}45
Files main
Menu

Theme