// engine/audit-trail/utils.ts — Shared low-level utilities for the audit-trail module
import { readFile } from 'node:fs/promises';
/**
 * Type guard for Node.js filesystem errors (ErrnoException).
 */
export function isNodeError(err) {
    return err instanceof Error && 'code' in err;
}
/**
 * Compute the canonical JSON string used for hashing an AuditEvent.
 * Excludes the `hash` field itself to avoid circular dependency.
 * CRITICAL: must stay in sync with how events are written in logger.ts.
 * If this function diverges from the write path, chain verification breaks silently.
 */
export function canonicalJson(event) {
    return JSON.stringify({
        id: event.id,
        timestamp: event.timestamp,
        eventType: event.eventType,
        specId: event.specId,
        userId: event.userId,
        action: event.action,
        details: event.details,
        previousHash: event.previousHash,
    });
}
/**
 * Read a JSONL file and return all non-empty lines.
 * Returns an empty array when the file does not exist (ENOENT).
 */
export async function readJsonlLines(filePath) {
    let raw;
    try {
        raw = await readFile(filePath, 'utf-8');
    }
    catch (err) {
        if (isNodeError(err) && err.code === 'ENOENT') {
            return [];
        }
        throw err;
    }
    return raw.split('\n').filter((l) => l.trim().length > 0);
}
//# sourceMappingURL=utils.js.map