Trace any pure TypeScript library and capture function I/O, environment values, and calling relationship without changing the source by hand.
- Instruments TypeScript sources via a custom transformer (CLI command
libtrace instrument). - Emits JSONL traces per function (by default grouped under
.libtraceand per-function files). - Records enter/exit parameters, captured free-variable environments, and parent/child call relationships.
- Supports
replayto turn a trace JSONL file into standalone TypeScript replay sources.
- Clone the repo and build the CLI.
npm run build- Instrument a project (defaults: include
src/**, exclude**/__test__/**):
node dist/bin.js instrument --project path/to/tsconfig.json --outDir .instrumentedUse --include / --exclude glob patterns and --verbose to inspect the resolved config.
- Run your instrumented code and set an output directory (default: traces out to
.libtrace):
LIBTRACE_DIR=./traces node .instrumented/your-entry.jsOr run any test code(e.g. vitest) after instrumentation.
- Inspect traces:
Each function gets a JSONL file containing
enter,call, andexitevents with serialized args, env, and outcomes.
Generate replay sources from a single trace file:
node dist/bin.js replay examples/simple-lib/traces/src_math.ts_-_addWithOffset_L13C1.jsonlThis writes replay_*.generated.ts files into the same directory (or pass --outDir).
Each file contains a replay_wrapper() function with arguments/env setup, a direct call, and basic return/throw checks.
A minimal example lives in examples/simple-lib:
node dist/bin.js instrument --project examples/simple-lib/tsconfig.json
node examples/simple-lib/run.js
node dist/bin.js replay examples/simple-lib/traces/src_math.ts_-_addWithOffset_L13C1.jsonlCheck examples/simple-lib/traces to see captured env values and child-call relationships.
- Runtime module is exposed as
libtrace/runtime; the transformer injects the import automatically. But you have to place thedist/runtimeundernode_modulesaslibtrace/runtimeand provide a simplepackage.jsonlike:{ "name": "libtrace", "type": "module", "exports": { "./runtime": "./runtime/index.js" } } - By default, output is grouped per function; configure
LIBTRACE_GROUP_BY_FUNC=falseto combine all into a single file.