SQLite WASM HTTP VFS Backend
sqlite-wasm-http provides an HTTP Virtual File System (VFS) backend for the official SQLite WASM distribution, enabling direct querying of remote SQLite databases over HTTP. It is designed to be a robust solution for browser and Node.js environments (Node.js 18+ required for full functionality) and supports features like multiple concurrent connections with shared caching (requiring `SharedArrayBuffer` and COOP/COEP headers) or a simplified fall-back without it. The library is currently at v1.2.0 and appears to have a semi-regular release cadence, with updates often tied to new official SQLite WASM builds or bug fixes. A key differentiator is its focus on the official SQLite WASM distribution, aiming to be an industry reference, and its support for shared cache across workers, offering performance benefits over similar solutions.
Common errors
-
ReferenceError: SharedArrayBuffer is not defined
cause Attempting to use the shared cache backend in a browser environment without the necessary Cross-Origin Isolation headers (COOP/COEP) or in a Node.js environment that doesn't fully support Web Workers/SharedArrayBuffer.fixFor browsers, ensure your server sends `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp` headers. For Node.js, ensure you are running Node.js 18.x or higher, which provides built-in `web-worker` and `fetch` support. The library will attempt to fallback if these are not available. -
TypeError: require is not a function
cause Trying to import `sqlite-wasm-http` using CommonJS `require()` syntax in an environment configured for ES modules, or in a project that needs to output ES modules.fixRefactor your imports to use ES module syntax (e.g., `import { createSQLiteThread } from 'sqlite-wasm-http';`). If using TypeScript, ensure your `tsconfig.json` `compilerOptions.module` is set to `ESNext` or `Node16`. -
Error: Unknown module: sqlite3-worker1-promiser-node.mjs
cause This specific file (`sqlite3-worker1-promiser-node.mjs`) was eliminated in `v1.1.1` to reduce bundle size, indicating an outdated setup or caching issue if encountered on newer versions.fixUpdate `sqlite-wasm-http` to at least `v1.1.1`. Clear your `node_modules` and package manager cache (`npm cache clean --force` or `pnpm store prune`). Ensure your bundler is resolving to the latest version.
Warnings
- breaking The library is strictly ES6 module mode only. CommonJS is not supported, including TypeScript transpiled to CommonJS. Projects must be configured to transpile to ES6 modules.
- gotcha Using the shared cache version with `SharedArrayBuffer` requires specific HTTP headers: `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`. Without these, shared cache will not function, and the library will fallback to a non-shared version.
- gotcha For optimal performance, especially with remote databases, it is highly recommended to set your SQLite database `page_size` to 1024 bytes and run `VACUUM` and `PRAGMA JOURNAL_MODE = DELETE;`.
- breaking Node.js 18.19 introduced breaking changes that impacted integration tests. Users running specific Node.js 18.x versions might encounter unexpected behavior or require specific configurations.
- gotcha The project is currently tagged as 'Experimental' in its README, indicating that while functional, its API or internal workings might undergo significant changes in future versions.
Install
-
npm install sqlite-wasm-http -
yarn add sqlite-wasm-http -
pnpm add sqlite-wasm-http
Imports
- createSQLiteThread
const { createSQLiteThread } = require('sqlite-wasm-http');import { createSQLiteThread } from 'sqlite-wasm-http'; - createHttpBackend
const createHttpBackend = require('sqlite-wasm-http').createHttpBackend;import { createHttpBackend } from 'sqlite-wasm-http'; - SQLite.SQLValue
import type { SQLite } from 'sqlite-wasm-http'; // ... then use SQLite.SQLValue
Quickstart
import { createSQLiteThread, createHttpBackend } from 'sqlite-wasm-http';
async function runRemoteQuery() {
const remoteURL = 'https://velivole.b-cdn.net/maptiler-osm-2017-07-03-v3.6.1-europe.mbtiles';
// createHttpBackend will autodetect if you can use SharedArrayBuffer or not
const httpBackend = createHttpBackend({
maxPageSize: 4096, // This is the current default SQLite page size
timeout: 10000, // 10s
cacheSize: 4096 // 4 MB (corresponds to 4096 pages of 1KB each for default 1024 page_size)
});
// Multiple DB workers can be created, all sharing the same backend cache
const db = await createSQLiteThread({ httpBackend });
try {
const results = await db.exec({
sql: 'SELECT name FROM sqlite_master WHERE type=\'table\';'
});
console.log('Tables:', results.results[0].row);
const countResult = await db.exec({
sql: 'SELECT count(*) FROM tiles WHERE zoom_level = ?;',
bind: [1]
});
console.log('Tiles at zoom 1:', countResult.results[0].row);
} finally {
db.close();
}
}
runRemoteQuery().catch(console.error);