Skip to content

Modules

Short table of every source module, what it exports, and who imports it. Use this to figure out where to open the editor.

src/index.ts — CLI entry point

The binary's main(). Parses argv, prints the usage banner, and dispatches to one of serve, render, examples, llm, help, or version. No business logic — just argv → function call.

Exports: none (script entry). Imports: server.ts, cli.ts, version.ts, llm-docs/index.ts.

src/server.ts — HTTP server

startServer() spins up Bun.serve() on the configured host + port, routes requests to /render, /cache/:hash, /health, /examples, or 404. Handles auth with checkAuth().

Exports: startServer(config), ServerConfig interface. Imports: renderer.ts, cache.ts, examples.ts, version.ts, template.ts (type only).

src/cli.ts — CLI render + examples

  • cliRender() reads JSON from stdin or a file, calls renderChart(), writes the image to stdout or a file, and echoes Chart.js messages to stderr.
  • cliExamples() iterates EXAMPLES, calls renderChart() for each, writes NN-<slug>.{png,jpg} + NN-<slug>.json to --outdir.

Exports: cliRender, cliExamples, and their arg interfaces. Imports: renderer.ts, examples.ts, template.ts (type only).

src/renderer.ts — Chromium pipeline

The rendering core. Manages:

  • Chromium discovery & auto-installfindChromiumExecutable(), downloadChromeForTesting(), ensureChromiumInstalled().
  • Browser lifecycleensureBrowser(), launchBrowser(), closeBrowser(). Single module-level browser ref; launches lazily, auto-clears on disconnected.
  • Page lifecycleschedulePageCleanup() arms a setTimeout that force-closes the tab after PAGE_TIMEOUT_SECONDS.
  • Render entryrenderChart() is the single export the rest of the codebase calls. It handles hash compute, cache get/set, semaphore acquire/release, HTML build, page.goto(data:…), console capture, screenshot.
  • StatsrendererStats() for /health.

Exports: renderChart, closeBrowser, rendererStats, ConsoleMessage, RenderResult. Imports: puppeteer-core, template.ts, semaphore.ts, cache.ts.

src/template.ts — Browser-side HTML

Static HTML string containing every CDN <script> for Chart.js + 12 plugins, and an IIFE that:

  • registers non-auto-registering plugins (datalabels, chartjs-chart-geo);
  • forces animations OFF;
  • wraps console.warn / console.error into window.__chartMessages;
  • instantiates Chart inside try/catch;
  • sets window.__chartRendered = true when done.

Edit this file if you add/remove/upgrade a plugin. See Adding a Chart.js plugin.

Exports: buildHtml(options), RenderOptions interface, LIBS const. Imports: none.

src/cache.ts — In-memory cache

Map<string, CacheEntry> with LRU-ish eviction (oldest key evicted when size >= MAX_ENTRIES) and TTL (lazy expiry on getCache). computeHash() hashes {chart, width, height, devicePixelRatio, backgroundColor, format, quality} via SHA-256 and takes the first 16 hex chars.

Exports: computeHash, getCache, setCache, cacheStats. Imports: crypto (Node built-in), template.ts (type only).

src/semaphore.ts — Concurrency control

30-line async semaphore. acquire() returns immediately if under capacity, otherwise pushes a resolver into a FIFO queue. release() pops the head of the queue and resolves.

Exports: Semaphore class. Imports: none.

src/examples.ts — Built-in examples

EXAMPLES is an array of { title, config, width?, height? } used by:

  • The CLI's examples subcommand (writes PNG + JSON files)
  • The HTTP /examples gallery (embeds <img src="/render?chart=…"> tags)

Add new examples here; they show up in both the CLI output directory and the gallery page automatically.

Exports: EXAMPLES, buildExamplesHtml(). Imports: none.

src/version.ts — Version constant

Single-line module re-exporting VERSION from package.json (via Bun's await import(..., { type: 'json' }), so compile-time inlined by bun build).

Exports: VERSION. Imports: package.json.

src/llm-docs/ — LLM reference bundle

One TS file per Chart.js module. Each file exports doc: string — a multi-line Markdown snippet. llm-docs/index.ts aggregates them into getLlmDocs() which chartjs2img llm prints.

See Adding LLM docs.

The files

Module fileCovers
usage.tsHow to invoke chartjs2img; JSON vs HTTP distinction
chartjs-core.tsChart.js core: types, datasets, scales, title/legend/tooltip
plugin-datalabels.tschartjs-plugin-datalabels options
plugin-annotation.tschartjs-plugin-annotation options
plugin-zoom.tschartjs-plugin-zoom options
plugin-gradient.tschartjs-plugin-gradient options
chart-matrix.tsmatrix / heatmap chart type
chart-sankey.tssankey chart type
chart-treemap.tstreemap chart type
chart-wordcloud.tswordcloud chart type
chart-geo.tschoropleth + bubbleMap chart types
chart-graph.tsgraph / forceDirectedGraph / dendrogram / tree chart types
chart-venn.tsvenn + euler chart types
adapter-dayjs.tsdayjs date adapter for time-series axes

Dependency graph (roughly)

Module dependency graph: index.ts dispatches to server.ts and cli.ts; both end in renderer.ts which depends on template.ts, cache.ts, and semaphore.ts. cli.ts also imports examples.ts. index.ts separately wires llm-docs/index.ts to many llm-docs/*.ts modules for the `llm` subcommand.

No cyclic deps. Nothing imports server.ts or cli.ts except index.ts — keeping entry points thin.

Edit this pageLast updated: