ADR-009: TypeDBLite Runs in the Browser on WASM SQLite
- Status: Accepted
- Date: 2026-11-12
- Decision owners: TypeDBLite maintainers
- Amends: ADR-002 Decision 9, which retired an earlier WASM adapter
- Feature: TypeDBLite Sync Demo
Accepted under the founder’s decision of 2026-11-12 to promote the WASM binding from a spike to supported API, so that the sync demo, and anyone else, can run TypeDBLite in a browser through the public package.
Context
Section titled “Context”TypeDBLite is offline-first. That only means something if the engine runs where the user is, and
in a web app the user is in the browser. The SqliteDatabase interface was designed for this: a WASM
build of SQLite, running synchronously in a Web Worker, satisfies it. A spike proved it.
libs/runtime/spike/wasm-sqlite.ts ran the runtime suite on WASM SQLite. A demo in a downstream application
then ran the real interpreter on OPFS in Chromium, in Electron, and in Android’s WebView, and synced
it with a hub.
But the spike is not something a user can depend on:
- The binding is not exported, and
@sqlite.org/sqlite-wasmis not a dependency of this repository. - Bundling
@typedblite/runtimefor a browser pulls innode:sqlite,node:httpandnode:crypto. The entry point re-exports thenode:sqlitebinding and the SSE hub, the interpreter importsopenNodeSqliteas its default opener, and@typedblite/content-revisionhashes revisions withnode:crypto. That demo got around this with an esbuild alias of its own. - Its test configuration does not start. It imports a shared file from a path outside this repository.
ADR-002 Decision 9 retired a different WASM adapter: an opaque 11.5 MB binary, with no obtainable
source, that stood in for the engine. This is not that. The engine stays this repository’s
TypeScript, and only SQLite itself is WASM: SQLite’s own official build, published as
@sqlite.org/sqlite-wasm under Apache-2.0.
Decision
Section titled “Decision”-
WASM SQLite is a supported binding.
@typedblite/runtime/wasmexportsinitWasmSqlite,openWasmSqlite,wipeOpfsPoolandpoolInfo. Persistence is OPFS through SQLite’s SAH-pool VFS, which works in a dedicated Worker and needs neitherSharedArrayBuffernor COOP/COEP headers. Without the pool, a path opens in SQLite’s in-memorymemdb. -
@sqlite.org/sqlite-wasmis an optional peer dependency. A Node user never installs it, and a browser user chooses the version. This repository pins it as a dev dependency, at 3.54.1-build1, for its own tests and the demo. -
The runtime has a browser entry point, selected by the
browserexport condition. It exports everything except thenode:sqlitebinding and the SSE hub (serveChangeStreamandcreateChangeStreamHandler), which are Node-only by nature. The spoke side of replication (TypeDBLiteReplicator,ChangeStreamClient,replicateOnce) is in both entry points. It is built onfetch. -
The interpreter’s default opener is a conditional import,
#default-opener. Undernode, it isopenNodeSqlite. Elsewhere, it is an opener that refuses with a message saying to passopenDatabase. On Node,new TypeDBLiteInterpreter()keeps working exactly as before. -
Revisions are hashed by one synchronous SHA-256, written in TypeScript, everywhere.
content-revisionstops importingnode:crypto. With one implementation there is one code path, so a browser and a server cannot disagree by construction. It is still proven identical tonode:cryptoby a differential test, because a revision that changes splits every store that has one (ADR-003 Decision 3). -
The browser floor is documented, not polyfilled. The runtime, through Chevrotain, calls
Object.groupBy, which needs Chromium 117, Firefox 119 or Safari 17.4. The library does not patch globals. An application that targets older engines, such as Android 14’s built-in WebView (Chromium 113), adds its own polyfill, as the sync demo does. -
A non-OK answer from a hub is an error, never “nothing to pull”. A spoke whose hub answers 5xx, or does not answer in time, reports a failure. A spoke that reports itself in sync when it is not is the silent success that ADR-002 Decision 7 forbids.
Alternatives rejected
Section titled “Alternatives rejected”- Keep the alias trick. Every browser user would need to know which internal file to swap, and the alias would break silently on any rename.
node:cryptoon Node and Web Crypto in the browser. Web Crypto’sdigestis asynchronous, and revisions are computed inside the synchronous write path. Two implementations would also be two ways to disagree.- Polyfill
Object.groupByinside the library. That mutates the host’s globals for every user, in order to serve a minority of users who can do it themselves. - A separate
@typedblite/runtime-wasmpackage. That adds a package to version and publish, for one module and one peer dependency. A subpath export is enough.
Consequences
Section titled “Consequences”- Positive: a web app can depend on
@typedblite/runtime, import@typedblite/runtime/wasm, and run the real engine, offline, with its data in OPFS and replication to a hub. The sync demo proves this end to end. - Positive: the WASM test configuration becomes a real target, so the binding is tested on every run instead of in a spike.
- Constraint:
content-revision’s hash is now this repository’s own code. It is guarded by a differential test againstnode:crypto. Treat any change to it as a change to the revision algorithm. - Constraint: OPFS through the SAH pool supports one connection per file at a time. Two tabs on one store need coordinating, for example through a single worker or a lock. The binding documents this, and does not solve it.
TypeDBLite is an independent open-source project, built on TypeDB with thanks. It is not affiliated with, sponsored by, or endorsed by TypeDB Ltd. Released under the MPL-2.0 license.