# SQLite with its recovery extension, for johnmcm.com The WebAssembly module behind SQLite Repair (/sqlite-repair): SQLite and its recovery extension, sqlite3recover, which is the code the sqlite3 shell runs for `.recover`. It checks a damaged database (`PRAGMA quick_check`, `PRAGMA integrity_check`), rebuilds what is still readable into a new database or an SQL script, and makes clean copies with `VACUUM INTO`. Built by `Dockerfile` with the Emscripten image pinned by digest and both SQLite release archives by SHA-256 (and the SHA3-256 sqlite.org publishes). Run `build.ps1` with Docker Desktop running; the module, its worker, its engine and SQLite's licence statement go to `frontend/vendor/local/sqlite-recover@1/`. | Part | Version | SHA-256 of the archive | Licence | Source | |---|---|---|---|---| | SQLite amalgamation (`sqlite3.c`, `sqlite3.h`) | 3.54.1 | `0a9c8e34352dad46006aa82836d5383bff9dc6bc9ce5a60553c05aa30e6ad1f9` | Public domain | https://sqlite.org/2026/sqlite-amalgamation-3540100.zip | | `ext/recover/sqlite3recover.c`, `sqlite3recover.h`, `dbdata.c` | 3.54.1 | `387e7f6cad4797b17848c07d453dc6606917bb356a6cfe5a3abafd38c240c71b` | Public domain | https://sqlite.org/2026/sqlite-src-3540100.zip | All are built unmodified. Compiled in: the recovery extension and the `sqlite_dbpage` and `sqlite_dbdata` tables it reads pages through, `dbstat`, FTS3, FTS4, FTS5, R*Tree and Geopoly (so databases with search or spatial tables recover with them), the maths functions and column metadata. Left out: threads, extension loading and memory-mapped I/O. ## Files - `src/binding.c`: SQLite, `dbdata.c` and `sqlite3recover.c` in one translation unit, with a few helpers (`jm_open`, `jm_watch`, `jm_recover_db`, `jm_recover_sql`, `jm_recover_step` and the stage and table it is on, the automatic extension `jm_auto` and `jm_safe_stmt` for the SQL download). Everything else is SQLite's own C API, exported as it is. - `src/engine.mjs`: the work itself over that module (open, query with capped rows and text, schema, counts, previews, checks, recovery, SQL dump, search index rebuild, `VACUUM INTO`, and the header read without SQLite). Used by the worker and by `frontend/tests/sqlite-repair.test.mjs` in Node. - `src/worker.mjs`: the module worker the page talks to (`src/app/sqlite-repair/repair-engine.ts`). ## Files in the worker The database the page was given is mounted with Emscripten's WORKERFS, which reads the `File` a slice at a time, and opened read-only with `immutable=1`: it is never copied into memory and nothing can write to it. With a `-wal` or `-journal` file it has to be opened for writing so SQLite can apply the log or roll back the unfinished transaction, so both are copied into memory (MEMFS) first, up to 1 GB together, and opened with `locking_mode=EXCLUSIVE` (the WAL index then lives in the connection's memory and no `-shm` file is needed). A recovered or cleaned database is written to MEMFS, handed to the page as a `Blob`, removed from memory, and the `Blob` mounted read-only again for previews and the SQL download. After a recovery, full-text search indexes are rebuilt from the recovered text (`INSERT INTO t(t) VALUES('rebuild')`), since the recovery copies their index pages as they were. ## The file is not trusted - Table names reach SQL only through `quoteIdent()` (double quotes, doubled). - Every statement runs under a time limit: SQLite's progress handler asks the worker every 4,000 instructions whether the limit has passed, and stops the statement if so. The page also ends the worker if a step runs past its limit, or when Stop is pressed. - Rows and text handed back are capped; blobs are described, not copied. - The time limit and `SQLITE_DBCONFIG_TRUSTED_SCHEMA` off (views and triggers in the file may only call functions without side effects; also the compile-time default, `SQLITE_TRUSTED_SCHEMA=0`) are set on every connection as it opens, by an automatic extension, so they also cover the connections the recovery extension opens itself. The recovery runs the schema's CREATE statements in the new database, and a crafted file can make one of them a long `CREATE TABLE ... AS SELECT`; it is bounded like the rest. Defensive mode is left off because the recovery extension needs `PRAGMA writable_schema`. - The SQL download is meant for the sqlite3 shell, which reads a line at a time and runs a line starting with a dot as its own command (`.shell` runs a program). The recovery hands over each schema entry's text as the file had it, which can hold more than one statement. So every statement goes through `jm_safe_stmt()` first: cut after the first complete statement (`sqlite3_complete`), line breaks and comments outside quotes turned into spaces (some shell versions also end a statement at a line holding only `go` or `/`), and text SQLite's tokenizer cannot read left out. ## Checked against the sqlite3 shell `docker build --target shell` builds the sqlite3 shell natively from the same release. On the damaged databases the tests make (12 random pages overwritten, the last quarter cut off, the header wiped, a table's root page wiped, the schema page overwritten), this module's recovery and `sqlite3 damaged.db .recover | sqlite3 out.db` gave the same row count in every ordinary table and in lost_and_found. The only difference is in the full-text search tables after the cut-off file: the shell's copy fails `integrity_check` ("fts5: corruption found"), while this module rebuilds the index from the recovered text and passes. ## Licence SQLite is in the public domain; `LICENSE-sqlite.md` is SQLite's own statement (`LICENSE.md` from the source archive above), also copied next to the built module. These build files are written for johnmcm.com and published at /source/sqlite-recover/.