Architecture
Modules
| Module | Owns |
|---|---|
src/persistence.ts | The connection manager (one per store, opened lazily), the saves and immutable snapshots tables, the SQLCipher key, the web-store flush, and the save/load/list/delete facade. |
src/migrations.ts | The pure snapshot walker and SnapshotVersionError. No I/O. |
src/autosave.ts | The instanced autosave scheduler. No module-level state. |
src/preferences-kv.ts | The namespaced Preferences bridge and web atomic coordination. |
src/preferences-native.ts | Native atomic KV transactions and addressed-key legacy read-through. |
src/index.ts | The public surface. Nothing else is exported. |
scripts/copy-wasm.mjs copies the sql.js binaries into dist/assets at build
time; scripts/verify-packed-consumer.mjs is the Node packed-tarball proof.
scripts/verify-packed-web-cold-reload.mjs packages the built tarball into a
clean Vite consumer, checks the IndexedDB record written by a save, then proves
the same row reopens after a full browser navigation into a fresh JavaScript
realm. Its user_version value is diagnostic only: an existing saves table is
valid with SQLite's default user version of zero.
ESM and CommonJS exports have separate declarations matching their module kind.
The CommonJS declarations inherit dist/cjs/package.json's type: commonjs.
Are The Types Wrong checks JavaScript and JSON entrypoints; .attw.json excludes
only the two binary WASM assets, whose paths, bytes and compilation are checked
by the packed consumer instead. That consumer typechecks both .ts and .cts.
Invariants
These are why the package exists. A change that breaks one reintroduces the bug it was built to prevent.
- Every statement is parameterized. No SQL is built from strings.
- Corruption is never "no save".
load()returnsnullonly when the row is absent; any failure to parse, migrate or deserialize an existing row throwsCorruptSaveError.list()skips corrupt rows one at a time instead of failing the list. - Migrations are exact. Pure
N → N+1steps over a frozen clone, ending exactly on the target; anything else is a typed error, never a guess. - Reads never write. Migrating a stored snapshot happens in memory; only
save()andcreateSnapshot()write the current format. - Inputs are bounded before they are trusted. Snapshot bytes are capped
before
JSON.parse, names are capped, and row counts are capped on write and on list. - The web store is durable when a write resolves. Every write flushes the
in-memory sql.js database to IndexedDB before its promise resolves, and flush
failures propagate. A packed browser consumer proves the bytes and row reopen
after a full navigation;
PRAGMA user_versionis never used as a persistence verdict. - Encryption fails closed. Without WebCrypto there is no key generation and therefore no encrypted database, rather than a weak key.
- One connection manager. Games extend the database through
initializeSchemaandwithConnectionon the same connection; the package never opens a second one.
Platform boundary
@capacitor-community/sqlite provides the native connection on iOS and Android
and drives the jeep-sqlite web component on the web. persistence-save registers
that component (from jeep-sqlite-current-sqljs) on first use in a browser and
points it at wasmAssetsPath. Capacitor packages are peers, so a game controls
their versions; the sql.js and jeep-sqlite versions are pinned here because the
WebAssembly binaries must match them exactly.
Immutable preservation boundary
The snapshots table is separate from rotating saves, with a SQLite-enforced
unique name. Creation is one parameterized INSERT, so concurrent attempts cannot
replace a prior snapshot. The normal save cap and delete API never address this
table. Names reject rather than truncate, and pages use exclusive row-id cursors.
Snapshot load and list fail on corruption or unavailable storage, so a caller
cannot mistake a failed preservation check for an empty archive.
Web flushes are serialized per database across facade instances, preventing a late older export from replacing a newer confirmed write. A snapshot write resolves only after the normal platform durability boundary. On a failed web flush the caller receives rejection, with durability uncertain; the insert is never reported as successful. Its immutable in-memory row may remain for explicit flush recovery. Real sql.js tests export and reopen database bytes; the packed web consumer proves snapshots survive IndexedDB cold reload.
Atomic Preferences boundary
The ordinary Preferences factory retains its native settings backend. The separate atomic factory uses exclusive origin-wide Preferences mutation locks on web and a dedicated SQLite table on native. Web reducers keep the lock through write verification and any verified rollback; all package preference mutations participate. The ordinary web setter fallback queue is only realm-local and is not advertised as an atomic update.
Native atomic keys read through to legacy Preferences until a table row exists.
NULL tombstones cut off read-through after removal; unrelated settings are not
migrated. A shared realm queue protects the one SQLite connection, while
BEGIN IMMEDIATE holds the native database writer lock across the latest read,
reducer, write and commit. Rollback stays inside that transaction and never
compensates after commit. A file-backed SQLite driver harness proves migration,
tombstones, failed-write rollback and writer exclusion against a second Node
process; this is not a claim of device integration proof.
All atomic-key writers must adopt the atomic factory. Raw or older-package writers are outside this guarantee. Native readers must also adopt it to see migrated keys. Insecure web contexts without Web Locks fail atomic updates before writes rather than pretending that a realm queue coordinates tabs.
The native driver harness also enforces Android's execSQL restriction against
statements with result columns. The busy_timeout assignment returns a timeout
row and therefore goes through query, while schema and transaction statements
use execute. Node's SQLite exec alone permits the former and cannot establish
that Android bridge compatibility. Native emulator acceptance remains a separate
gate from this driver contract.