/** Minimal shape of H3's event.node.req used to read x-nonce. */
interface NodeRequest {
  headers: Record<string, string | string[] | undefined>;
}

/**
 * Reads the nonce from a TanStack Start / H3 server request.
 * The nonce is set on req.headers["x-nonce"] by withSecurityHeaders().
 */
export function getNonce(req: NodeRequest): string {
  const value = req.headers["x-nonce"];
  if (Array.isArray(value)) return value[0] ?? "";
  return value ?? "";
}
