# syntax=docker/dockerfile:1 # # SQLite with its recovery extension (sqlite3recover, the code behind the # sqlite3 shell's .recover command) compiled to WebAssembly for johnmcm.com's # /sqlite-repair. SQLite is in the public domain (LICENSE-sqlite.md). # # Reproducible: the Emscripten image is pinned by version and digest, and both # SQLite release archives by version and SHA-256 (sqlite.org publishes their # SHA3-256, also checked below). SQLite is built unmodified: the amalgamation # (sqlite3.c) plus ext/recover/dbdata.c and ext/recover/sqlite3recover.c from # the full source archive of the same release, in one translation unit with # src/binding.c. # # ./build.ps1 (or) docker build --target export --output type=local,dest=dist . # # These build files are published at /source/sqlite-recover/. ARG EMSDK_IMAGE=emscripten/emsdk:6.0.10@sha256:e077d54e2b8970575ebc4f185ac1de0b95c05f2b266134d4ba27449af7aebf65 FROM ${EMSDK_IMAGE} AS deps ARG SQLITE_YEAR=2026 ARG SQLITE_TAG=3540100 ARG AMALGAMATION_SHA256=0a9c8e34352dad46006aa82836d5383bff9dc6bc9ce5a60553c05aa30e6ad1f9 ARG AMALGAMATION_SHA3=92281c86e2957f9b9305890f9ac04204caa7b677cc868dc326d942689df28e30 ARG SRC_SHA256=387e7f6cad4797b17848c07d453dc6606917bb356a6cfe5a3abafd38c240c71b ARG SRC_SHA3=877c6f4996ce7adb368686bce21638de9e3d91534832ad3d9d119037d1bc22ad WORKDIR /src RUN curl -fsSL -o amalgamation.zip https://sqlite.org/${SQLITE_YEAR}/sqlite-amalgamation-${SQLITE_TAG}.zip \ && echo "${AMALGAMATION_SHA256} amalgamation.zip" | sha256sum -c - \ && curl -fsSL -o full.zip https://sqlite.org/${SQLITE_YEAR}/sqlite-src-${SQLITE_TAG}.zip \ && echo "${SRC_SHA256} full.zip" | sha256sum -c - \ && python3 -c "import hashlib,sys; a=sys.argv; assert hashlib.sha3_256(open('amalgamation.zip','rb').read()).hexdigest()==a[1]; assert hashlib.sha3_256(open('full.zip','rb').read()).hexdigest()==a[2]" "${AMALGAMATION_SHA3}" "${SRC_SHA3}" \ && python3 -m zipfile -e amalgamation.zip . \ && python3 -c "import zipfile,sys; z=zipfile.ZipFile('full.zip'); p='sqlite-src-'+sys.argv[1]+'/'; [z.extract(p+n,'.') for n in ('ext/recover/sqlite3recover.c','ext/recover/sqlite3recover.h','ext/recover/dbdata.c','LICENSE.md')]" "${SQLITE_TAG}" \ && mkdir /sqlite \ && cp sqlite-amalgamation-${SQLITE_TAG}/sqlite3.c sqlite-amalgamation-${SQLITE_TAG}/sqlite3.h sqlite-amalgamation-${SQLITE_TAG}/shell.c /sqlite/ \ && cp sqlite-src-${SQLITE_TAG}/ext/recover/* sqlite-src-${SQLITE_TAG}/LICENSE.md /sqlite/ # The module. One file system for the input (WORKERFS, which reads the File # the page was given a slice at a time, so it is never copied into memory) and # MEMFS for what is written: the recovered or cleaned database, journals and # temporary files. No threads, extension loading or memory-mapped I/O. FROM deps AS wasm WORKDIR /binding COPY src/binding.c ./ RUN mkdir -p /out \ && emcc -O2 -I/sqlite binding.c -o /out/sqlite-recover.mjs \ -DSQLITE_THREADSAFE=0 \ -DSQLITE_OMIT_LOAD_EXTENSION \ -DSQLITE_OMIT_DEPRECATED \ -DSQLITE_OMIT_SHARED_CACHE \ -DSQLITE_MAX_MMAP_SIZE=0 \ -DSQLITE_DEFAULT_MEMSTATUS=0 \ -DSQLITE_DEFAULT_FOREIGN_KEYS=0 \ -DSQLITE_TRUSTED_SCHEMA=0 \ -DSQLITE_TEMP_STORE=2 \ -DSQLITE_USE_URI=1 \ -DSQLITE_ENABLE_DBPAGE_VTAB \ -DSQLITE_ENABLE_DBSTAT_VTAB \ -DSQLITE_ENABLE_BYTECODE_VTAB \ -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4 \ -DSQLITE_ENABLE_FTS5 \ -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_GEOPOLY \ -DSQLITE_ENABLE_MATH_FUNCTIONS \ -DSQLITE_ENABLE_COLUMN_METADATA \ -DSQLITE_ENABLE_STAT4 \ -lworkerfs.js \ -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=createSqliteRecover \ -sENVIRONMENT=web,worker,node \ -sALLOW_MEMORY_GROWTH=1 \ -sINITIAL_MEMORY=33554432 \ -sMAXIMUM_MEMORY=2147483648 \ -sSTACK_SIZE=1048576 \ -sFORCE_FILESYSTEM=1 \ -sDYNAMIC_EXECUTION=0 \ -sINCOMING_MODULE_JS_API=print,printErr,locateFile \ -sEXPORTED_FUNCTIONS=_malloc,_free,_jm_init,_jm_watch,_jm_open,_jm_recover_db,_jm_recover_sql,_jm_recover_step,_jm_recover_state,_jm_recover_table,_jm_recover_out,_sqlite3_recover_errmsg,_sqlite3_recover_errcode,_sqlite3_recover_finish,_sqlite3_close_v2,_sqlite3_errmsg,_sqlite3_extended_errcode,_sqlite3_prepare_v2,_sqlite3_step,_sqlite3_finalize,_sqlite3_reset,_sqlite3_column_count,_sqlite3_column_name,_sqlite3_column_type,_sqlite3_column_int64,_sqlite3_column_double,_sqlite3_column_text,_sqlite3_column_bytes,_sqlite3_bind_text,_sqlite3_bind_int64,_sqlite3_changes,_sqlite3_libversion \ -sEXPORTED_RUNTIME_METHODS=FS,WORKERFS,HEAPU8,HEAP32,UTF8ToString,stringToUTF8,lengthBytesUTF8 # The sqlite3 shell, built natively from the same release, for the tests that # compare this module's recovery with the shell's .recover (local only). FROM deps AS shell RUN gcc -O2 -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \ -o /usr/local/bin/sqlite3 /sqlite/shell.c /sqlite/sqlite3.c -lm FROM scratch AS export COPY --from=wasm /out/ / COPY --from=deps /sqlite/LICENSE.md /LICENSE-sqlite.md