Skip to content

Rust ↔ TypeScript Sync

A zero-external-dependency end-to-end example proving live wire-format interoperability between Rust (zwire) and TypeScript (@zwire/core) over spec/protocol.md JSON:

  1. Out-of-order hydration (dehydrate_json → hydrate): The Rust server dehydrates committed state — including a u64 sequence number above Number.MAX_SAFE_INTEGER (9007199254740995, encoded as a decimal string for lossless JS BigInt decoding) and a ReactiveCollection Top-K leader — and streams it to the TypeScript client before the client registers its signals.
  2. Concurrent server push + optimistic rebase (SyncBridge → OptimisticLayer): While the TypeScript client holds two overlapping optimistic layers (-150 and -50), the Rust server processes an authoritative deposit (+500) and orderbook update (top_bid = 125), emitting a coalesced StatePatch[] batch. The TypeScript SyncBridge applies the remote patches and automatically rebases both active optimistic layers with zero lost updates.
  3. Bidirectional patch sync (@zwire/core/protocol → zwire-protocol): A local client mutation flushes as StatePatch[] back to Rust, where StatePatch::vec_from_json decodes and applies it via Rust’s SyncBridge while suppressing echoes.

From the repository root:

Terminal window
mise run test:examples
# or directly:
node ./examples/rust-ts-sync/run.ts