Reference

Architecture

The modules, the invariants they protect, and where the boundaries are.

Modules

ModuleOwns
src/persistence.tsThe 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.tsThe pure snapshot walker and SnapshotVersionError. No I/O.
src/autosave.tsThe instanced autosave scheduler. No module-level state.
src/preferences-kv.tsThe namespaced Preferences bridge and web atomic coordination.
src/preferences-native.tsNative atomic KV transactions and addressed-key legacy read-through.
src/index.tsThe 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.

  1. Every statement is parameterized. No SQL is built from strings.
  2. Corruption is never "no save". load() returns null only when the row is absent; any failure to parse, migrate or deserialize an existing row throws CorruptSaveError. list() skips corrupt rows one at a time instead of failing the list.
  3. Migrations are exact. Pure N → N+1 steps over a frozen clone, ending exactly on the target; anything else is a typed error, never a guess.
  4. Reads never write. Migrating a stored snapshot happens in memory; only save() and createSnapshot() write the current format.
  5. 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.
  6. 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_version is never used as a persistence verdict.
  7. Encryption fails closed. Without WebCrypto there is no key generation and therefore no encrypted database, rather than a weak key.
  8. One connection manager. Games extend the database through initializeSchema and withConnection on 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.