UNPKG

@stacktrace-lite/core

Version:

> Parse, filter, and format JavaScript stack traces with plugins for browsers and Node.js.

47 lines (46 loc) 1.89 kB
import type { StackFrame } from './parser.js'; export type FormatMode = 'cli' | 'html' | 'json' | 'raw'; /** * Format an array of {@link StackFrame} entries into a string based on the desired output mode. * * @param frames - Parsed stack frames to format. * @param mode - Output style: * - `"cli"` – styled for terminal (default) * - `"html"` – safe for rendering into web UIs * - `"json"` – structured JSON * - `"raw"` – concatenated original lines (`frame.raw`) * @returns A formatted string representing the stack trace. * * @remarks * **Terminal styling (`"cli"`)** * Uses the [colorette](https://www.npmjs.com/package/colorette) library to render ANSI styling: * - `bold(...)` for function names * - `dim('at')` for the “at” constant * - `yellow(...)` for file names * - `gray(...)` for line/column position * Colorette is chosen for its speed and zero dependencies—efficient and NO_COLOR friendly.:contentReference[oaicite:1]{index=1} * * **HTML mode (`"html"`)** * Safely escapes HTML entities and assembles a `<pre>` block with semantic `<span>` tags (`function`, `at`, `file`, `pos`)—great for embedding in browser error overlays or dev UI. * * **JSON mode (`"json"`)** * Outputs the full frames array as pretty JSON (`null, 2` spacing), ideal for logging or backend ingestion. * * **Raw mode (`"raw"`)** * Joins the `raw` values of frames, preserving their original formatting without transformation. * * @example * ```ts * const trace = formatStack(frames, 'cli'); * console.log(trace); * * const htmlTrace = formatStack(frames, 'html'); * document.body.innerHTML = htmlTrace; * * const jsonTrace = formatStack(frames, 'json'); * sendToServer(jsonTrace); * ``` * * @throws Error when an unsupported mode string is provided. */ export declare function formatStack(frames: StackFrame[], mode?: FormatMode): string;