Skip to content
Search Journal
Search journal entries and projects across Journal.
Rust & Systemsdeep dive

Building StructAura: Deterministic AST Fingerprinting with Rust and WebAssembly

How I built a versioned JavaScript and TypeScript structural fingerprint using Tree-sitter, a shared Rust core, canonical encoding and iterative Merkle hashing.

A source hash will change if you edit a comment or give the formatter some breathing space. It will be correct at the byte level, but useless if the question is whether the syntax has changed.

StructAura parses JavaScript or TypeScript using Tree-sitter, performs a few versioned syntax-only transformations and hashes the canonical tree. The Rust core is used by both the native version and the WASM version running in the browser. I wanted to make sure the fingerprint definition stayed the same in both places.

Matching fingerprints mean the canonical trees match for the given language and normalization version. The fingerprint does not include type analysis, constant folding or any indication that two functions are equivalent.

The Tree-sitter and web-tree-sitter versions use different runtimes, but both adapters output the same flat preorder data: syntax kind, optional leaf text and the number of retained children. The canonical tree is rebuilt in the Rust core, where canonicalization, encoding and hashing happen.

native parser   ┐
                ├─ records → Rust core → fingerprint
browser parser  ┘              (wasm-bindgen on the web)

The canonical tree is rebuilt and traversed using an explicit vector in reverse preorder. This avoids putting deeply nested input on the application call stack. If the data is truncated or incomplete, an error will be returned.

Version 1 removes comments and some anonymous punctuation, unwraps a single parenthesized expression, and removes string delimiters without decoding escape characters. The order of children is maintained, and the escape spelling is retained. This means two strings can still have different hashes even if they have the same runtime meaning. Changing this behaviour would require a normalizationVersion bump so existing fingerprints keep their meaning.

The encoder uses domain tags, fixed-size big-endian integers, length-prefixed bytes and typed properties. Property keys are ordered, while child hashes stay in source order. Hashing is done iteratively and uses a Merkle structure. A change to any descendant will change the hashes up to the root. The root includes the format version, language and normalization version. compareFingerprints checks these before comparing the digests. JavaScript and TypeScript fingerprints have different identities, even if their digest strings match.

The release build numbers were recorded. Small inputs had a median time of around 116 µs across 15 samples, excluding warm-up. Inputs around 64–70 KB took about 12–14 ms at p95. The stage times give an indication of where work is being done. They cannot be added together, though, because the instrumentation changes how long the pipeline takes to run.

The browser package has an async init() because the WASM module and grammars need to be loaded. Once it has been initialised, fingerprinting is synchronous. Large files should be run in a worker to avoid freezing the page.

// Same retained syntax; comments and layout differ.
const total = left + right;
const total = left + right;

For the current JavaScript normalization version, these formatting changes produce the same canonical fingerprint. Changing right to tax changes the remaining syntax, so the fingerprint changes too. The fingerprints describe the normalized structure; they don’t tell you whether the programs are equivalent.

Where StructAura could go next

There are a few areas I’d like to explore next. Adding more language support is one of them. The parser adapters already give each language a way to produce the records consumed by the Rust core, so another language could follow the same path.

I’d also like to look at larger files and more varied source code in the benchmarks. The current measurements give me a starting point, but I want to understand how parsing, rebuilding the canonical tree and hashing behave as inputs grow. Running the browser work in a worker is another useful direction for keeping larger files from blocking the page.

One interesting possibility is incremental fingerprinting. Since the hashes are built from a Merkle tree, an edit might only require recalculating the hashes for the parts of the tree that changed and their parents. The hard part is knowing which parts can be reused safely after parsing and normalization. That would be worth exploring before making any changes to the current fingerprint contract.