# phiresky/sql.js-httpvfs architecture

> Architecture diagram of the GitHub repository phiresky/sql.js-httpvfs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/phiresky/sql.js-httpvfs
- Repository: https://github.com/phiresky/sql.js-httpvfs

## Overview

#### 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: [`src/sqlite.worker.ts`](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/sqlite.worker.ts) (Database worker), [`src/index.ts`](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/index.ts) (createDbWorker API), [`sql.js/Makefile`](https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/Makefile) (sql.js artifact build), [`src/lazyFile.ts`](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/lazyFile.ts) (Lazy HTTP file VFS)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser library"]
  node_consumer["Consumer application<br/>browser consumer"]
  node_public_api["createDbWorker API<br/>TypeScript entry point<br/>[index.ts]"]
  node_db_worker["Database worker<br/>Web Worker<br/>[sqlite.worker.ts]"]
  node_lazy_file["Lazy HTTP file VFS<br/>Emscripten file backend<br/>[lazyFile.ts]"]
  node_dom_vtab["DOM virtual table<br/>SQLite extension<br/>[vtab.ts]"]
  node_shared_types["Shared declarations<br/>TypeScript types<br/>[types.d.ts]"]
end

subgraph group_runtime["SQLite/WASM runtime"]
  node_sqljs_api["Forked sql.js API<br/>SQLite/WASM runtime<br/>[api.js]"]
  node_emscripten_pre["Emscripten pre-shell<br/>runtime integration<br/>[shell-pre.js]"]
  node_emscripten_post["Emscripten post-shell<br/>runtime integration<br/>[shell-post.js]"]
end

subgraph group_delivery["Database delivery"]
  node_remote_db[("Remote SQLite assets<br/>static HTTP/CDN origin")]
  node_chunk_config["Chunk layout config<br/>JSON metadata"]
  node_db_preparation["Database preparation<br/>chunking script<br/>[create_db.sh]"]
  node_example["Consumer reference<br/>example application<br/>[index.ts]"]
end

subgraph group_build["Build and distribution"]
  node_wrapper_bundle["Wrapper bundle<br/>published package assets<br/>[webpack.config.js]"]
  node_sqljs_build["sql.js artifact build<br/>upstream build pipeline<br/>[Makefile]"]
  node_wasm_asset["sql-wasm.wasm<br/>published WASM asset"]
end

node_consumer -->|"calls"| node_public_api
node_public_api -->|"worker RPC"| node_db_worker
node_public_api -.->|"uses"| node_shared_types
node_db_worker -->|"attaches database files"| node_lazy_file
node_db_worker -->|"initializes and queries"| node_sqljs_api
node_db_worker -.->|"loads optional extension"| node_dom_vtab
node_lazy_file -->|"HTTP Range reads"| node_remote_db
node_lazy_file -->|"interprets chunk layout"| node_chunk_config
node_lazy_file -.->|"enforces byte limit and diagnostics"| node_db_worker
node_db_preparation -->|"produces database assets"| node_remote_db
node_db_preparation -->|"emits"| node_chunk_config
node_example -->|"uses"| node_public_api
node_wrapper_bundle -->|"bundles"| node_public_api
node_wrapper_bundle -->|"publishes worker bundle"| node_db_worker
node_sqljs_build -->|"builds"| node_sqljs_api
node_sqljs_build -->|"includes"| node_emscripten_pre
node_sqljs_build -->|"includes"| node_emscripten_post
node_sqljs_build -->|"produces"| node_wasm_asset
node_db_worker -->|"loads by URL"| node_wasm_asset

click node_public_api "https://github.com/phiresky/sql.js-httpvfs/blob/master/src/index.ts"
click node_db_worker "https://github.com/phiresky/sql.js-httpvfs/blob/master/src/sqlite.worker.ts"
click node_lazy_file "https://github.com/phiresky/sql.js-httpvfs/blob/master/src/lazyFile.ts"
click node_dom_vtab "https://github.com/phiresky/sql.js-httpvfs/blob/master/src/vtab.ts"
click node_shared_types "https://github.com/phiresky/sql.js-httpvfs/blob/master/src/types.d.ts"
click node_sqljs_api "https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/src/api.js"
click node_emscripten_pre "https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/src/shell-pre.js"
click node_emscripten_post "https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/src/shell-post.js"
click node_db_preparation "https://github.com/phiresky/sql.js-httpvfs/blob/master/create_db.sh"
click node_example "https://github.com/phiresky/sql.js-httpvfs/blob/master/example/src/index.ts"
click node_wrapper_bundle "https://github.com/phiresky/sql.js-httpvfs/blob/master/webpack.config.js"
click node_sqljs_build "https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/Makefile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_consumer,node_public_api,node_db_worker,node_lazy_file,node_dom_vtab,node_shared_types toneBlue
class node_sqljs_api,node_emscripten_pre,node_emscripten_post toneAmber
class node_remote_db,node_chunk_config,node_db_preparation,node_example toneMint
class node_wrapper_bundle,node_sqljs_build,node_wasm_asset toneRose
```

## Components

### Browser library

- **Consumer application**: browser consumer
- [createDbWorker API](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/index.ts): TypeScript entry point · Promise-based database API and worker RPC client. · `src/index.ts`
- [Database worker](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/sqlite.worker.ts): Web Worker · Runs SQLite, queries, and network-backed file reads off the UI thread. · `src/sqlite.worker.ts`
- [Lazy HTTP file VFS](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/lazyFile.ts): Emscripten file backend · Reads remote SQLite bytes on demand with caching, read heads, and prefetch. · `src/lazyFile.ts`
- [DOM virtual table](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/vtab.ts): SQLite extension · Separate bridge from SQL queries to mutable browser DOM state. · `src/vtab.ts`
- [Shared declarations](https://github.com/phiresky/sql.js-httpvfs/blob/master/src/types.d.ts): TypeScript types · `src/types.d.ts`

### SQLite/WASM runtime

- [Forked sql.js API](https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/src/api.js): SQLite/WASM runtime · `sql.js/src/api.js`
- [Emscripten pre-shell](https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/src/shell-pre.js): runtime integration · `sql.js/src/shell-pre.js`
- [Emscripten post-shell](https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/src/shell-post.js): runtime integration · `sql.js/src/shell-post.js`

### Database delivery

- **Remote SQLite assets**: static HTTP/CDN origin · Database files are served through HTTP Range requests.
- **Chunk layout config**: JSON metadata · Describes chunked database layouts for lazy reads.
- [Database preparation](https://github.com/phiresky/sql.js-httpvfs/blob/master/create_db.sh): chunking script · Prepares a database and emits chunk-serving configuration. · `create_db.sh`
- [Consumer reference](https://github.com/phiresky/sql.js-httpvfs/blob/master/example/src/index.ts): example application · Demonstrates worker/WASM URLs and inline remote-file configuration. · `example/src/index.ts`

### Build and distribution

- [Wrapper bundle](https://github.com/phiresky/sql.js-httpvfs/blob/master/webpack.config.js): published package assets · Worker JavaScript must remain separately addressable. · `webpack.config.js`
- [sql.js artifact build](https://github.com/phiresky/sql.js-httpvfs/blob/master/sql.js/Makefile): upstream build pipeline · Builds the underlying SQLite-to-WASM artifacts. · `sql.js/Makefile`
- **sql-wasm.wasm**: published WASM asset · Must be reachable by a separately addressable URL.

## Connections

- 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
