How phiresky/sql.js-httpvfs works
Purpose and Public Entry Point
sql.js-httpvfs is a browser-side, read-only SQLite access layer. It runs SQLite compiled to WebAssembly in a Web Worker and supplies database bytes lazily over HTTP Range requests, avoiding a full initial database download. The library API starts at src/index.ts; consumers call createDbWorker(...) and receive promise-based database methods.
Worker Execution Boundary
The main thread creates and communicates with the database worker through src/index.ts. SQLite, Emscripten filesystem integration, query execution, and network-backed file reads run off-thread in src/sqlite.worker.ts, isolating blocking/WASM work from browser UI code. Consumers must provide URLs for the worker bundle and SQLite WASM artifact.
Remote Database Virtual Filesystem
src/lazyFile.ts is the core HTTP VFS implementation. It exposes remote SQLite files to Emscripten while fetching byte ranges on demand, caching fetched data in memory, and using virtual read heads/prefetch behavior for sequential access. It supports full database files and chunked database layouts described by JSON configuration.
SQLite/WASM Integration
src/sqlite.worker.ts initializes the forked sql.js runtime and attaches lazy files as SQLite-accessible database inputs. The vendored upstream runtime is under sql.js/src/api.js, with Emscripten shell integration in sql.js/src/shell-pre.js and sql.js/src/shell-post.js. The resulting runtime is browser-dependent: WebAssembly, Web Workers, Fetch/XHR Range support, and Emscripten’s filesystem emulation are material requirements.
Query Protocol and Resource Controls
Worker RPC exposes SQL.js-like operations as asynchronous calls through src/index.ts and src/sqlite.worker.ts. A byte-read accounting boundary limits network consumption: requests exceeding the configured maxBytesToRead fail as SQLite disk I/O errors. The worker also exposes fetched-byte totals and accessed-page diagnostics, allowing callers to assess query/index locality.
DOM Virtual Table Extension
src/vtab.ts implements a proof-of-concept SQLite virtual table backed by browser DOM access. Unlike the remote database path, this extension creates a bridge between SQL queries and mutable browser DOM state; it is therefore a separate capability and trust boundary within the browser runtime. Shared type declarations are in src/types.d.ts.
Database Preparation and Delivery
Databases are static assets hosted by any HTTP-capable static/CDN origin; no application server is required. create_db.sh prepares a database for chunked serving and emits configuration consumed by the lazy-file layer. The included consumer reference is example/src/index.ts, paired with example/example.sqlite3, demonstrating bundler-served worker/WASM assets and an inline remote-file configuration.
Build and Distribution Structure
The root TypeScript package is bundled via webpack.config.js. The nested sql.js/ repository is the forked upstream SQLite-to-WASM build source; its Makefile and sql.js/package.json produce the underlying sql.js artifacts before the wrapper is built. Published distribution assets must keep the worker JavaScript and sql-wasm.wasm reachable as separately addressable URLs.
Main entry points: sqlite.worker.ts Database worker · index.ts createDbWorker API · Makefile sql.js artifact build · lazyFile.ts Lazy HTTP file VFS
Components 16
Browser library
- Consumer application
- createDbWorker API · Promise-based database API and worker RPC client.
src/index.ts - Database worker · Runs SQLite, queries, and network-backed file reads off the UI thread.
src/sqlite.worker.ts - Lazy HTTP file VFS · Reads remote SQLite bytes on demand with caching, read heads, and prefetch.
src/lazyFile.ts - DOM virtual table · Separate bridge from SQL queries to mutable browser DOM state.
src/vtab.ts - Shared declarations
src/types.d.ts
SQLite/WASM runtime
- Forked sql.js API
sql.js/src/api.js - Emscripten pre-shell
sql.js/src/shell-pre.js - Emscripten post-shell
sql.js/src/shell-post.js
Database delivery
- Remote SQLite assets · Database files are served through HTTP Range requests.
- Chunk layout config · Describes chunked database layouts for lazy reads.
- Database preparation · Prepares a database and emits chunk-serving configuration.
create_db.sh - Consumer reference · Demonstrates worker/WASM URLs and inline remote-file configuration.
example/src/index.ts
Build and distribution
- Wrapper bundle · Worker JavaScript must remain separately addressable.
webpack.config.js - sql.js artifact build · Builds the underlying SQLite-to-WASM artifacts.
sql.js/Makefile - sql-wasm.wasm · Must be reachable by a separately addressable URL.
Connections 19
- Consumer application → createDbWorker API: calls
- createDbWorker API → Database worker: worker RPC
- createDbWorker API → Shared declarations: uses
- Database worker → Lazy HTTP file VFS: attaches database files
- Database worker → Forked sql.js API: initializes and queries
- Database worker → DOM virtual table: loads optional extension
- Lazy HTTP file VFS → Remote SQLite assets: HTTP Range reads
- Lazy HTTP file VFS → Chunk layout config: interprets chunk layout
- Lazy HTTP file VFS → Database worker: enforces byte limit and diagnostics
- Database preparation → Remote SQLite assets: produces database assets
- Database preparation → Chunk layout config: emits
- Consumer reference → createDbWorker API: uses
- Wrapper bundle → createDbWorker API: bundles
- Wrapper bundle → Database worker: publishes worker bundle
- sql.js artifact build → Forked sql.js API: builds
- sql.js artifact build → Emscripten pre-shell: includes
- sql.js artifact build → Emscripten post-shell: includes
- sql.js artifact build → sql-wasm.wasm: produces
- Database worker → sql-wasm.wasm: loads by URL