$ show observablehq/stdlib
@observablehq/stdlib is the official standard library for the Observable notebook platform. It provides a comprehensive set of utilities for building interactive data visualizations and applications directly in the browser. The library covers everything from DOM element creation (HTML/SVG/Markdown/LaTeX rendering), file access (FileAttachments with CSV, JSON, SQLite, Excel, Arrow, Parquet support), interactive inputs and reactive programming via generators (observe, queue, range), promise utilities (delay, tick, when), and database clients (SQLite via sql.js, DuckDB via WebAssembly). It is architecturally a monolithic client-side library that exports a main `Library` class, composed from small, single-purpose modules under `src/`. The primary entry point is `src/index.js` (package module) which re-exports all public symbols. The library is consumed primarily by the Observable runtime, but it can also be used standalone via the `Library` constructor. External dependencies are minimal (d3-array, d3-dsv, d3-require); heavy parts like DuckDB and Plot load dynamically from a CDN at runtime. The codebase demonstrates high craftsmanship in its generator implementation (push-to-pull adaptation) and schema inference system for tabular data. However, some modules are tightly coupled to the Observable operation model (e.g., `table.js`). Overall, it is a well-structured, extensively tested library that serves a specific ecosystem but contains several reusable patterns.
The LLM identified 2 files worth highlighting for reuse:
Implements a generator that converts a push-based data source into a pull-able generator. The `observe` function takes an initialization function which receives a `change` callback; each call to `change` updates a stored value and resolves the current pending promise. All following calls are buffered and debounced (only the latest value is kept). The returned generator yields a promise for the current value when awaited, making it lossy but ensuring fast reactivity. This is a general utility for adapting event emitters and observables to synchronous-like iteration, and it can be extracted and used in any project that needs to integrate push-based APIs with synchronous loops.
Provides the core tagged template literal engine used by `html`, `svg`, and `md` in the Observable environment. It parses a template literal string, extracts placeholder indices, and after appends calls a custom renderer function (string-to-DOM). Any `Node` objects passed as interpolation values are placed into comment placeholders which are later walked and replaced with the actual nodes. The engine wraps the complete result appropriately (single child extraction, `<span>` wrapping for fragments, SVG namespace handling). It is self-contained, depends only on the browser environment, and is reusable for any project that wants to build DOM from template strings with embedded elements.