fix: wa-sqlite initialisation fix

This commit is contained in:
Kacper Kula 2026-04-26 16:06:18 +01:00
parent 6d4dcc5ece
commit b6cd435068
4 changed files with 18 additions and 28 deletions

View file

@ -112,10 +112,13 @@ const workerPlugin = {
const wasmPath = join(process.cwd(), 'node_modules/wa-sqlite/dist/wa-sqlite-async.wasm');
const wasmContents = readFileSync(wasmPath);
const wasmBase64 = wasmContents.toString('base64');
const wasmDataUrl = `data:application/wasm;base64,${wasmBase64}`;
return {
contents: `export default ${JSON.stringify(wasmDataUrl)};`,
contents: `
const wasmBase64 = "${wasmBase64}";
const wasmBinary = Uint8Array.from(atob(wasmBase64), c => c.charCodeAt(0));
export default wasmBinary;
`,
loader: 'js',
};
});
@ -158,10 +161,13 @@ const workerPlugin = {
const wasmPath = join(process.cwd(), 'node_modules/wa-sqlite/dist/wa-sqlite-async.wasm');
const wasmContents = readFileSync(wasmPath);
const wasmBase64 = wasmContents.toString('base64');
const wasmDataUrl = `data:application/wasm;base64,${wasmBase64}`;
return {
contents: `export default ${JSON.stringify(wasmDataUrl)};`,
contents: `
const wasmBase64 = "${wasmBase64}";
const wasmBinary = Uint8Array.from(atob(wasmBase64), c => c.charCodeAt(0));
export default wasmBinary;
`,
loader: 'js',
};
});

View file

@ -7,7 +7,7 @@ import { sanitise } from "../../../utils/sanitiseColumn";
// Get the WASM URL from the virtual module
// @ts-ignore
import wasmUrl from 'virtual:wa-sqlite-wasm-url';
import wasmBinary from 'virtual:wa-sqlite-wasm-url';
/**
* Retry an async operation with exponential backoff
@ -110,15 +110,7 @@ export class SqlocalWorkerDatabase {
}
try {
// Initialize the module with bundled WASM
const asyncModule = await SQLiteAsyncESMFactory({
locateFile: (file: string) => {
if (file.endsWith('.wasm')) {
return wasmUrl;
}
return file;
}
});
const asyncModule = await SQLiteAsyncESMFactory({ wasmBinary, locateFile: (file: string) => file });
// Use Factory to get the actual sqlite3 API
this.sqlite3 = SQLite.Factory(asyncModule);

View file

@ -1,10 +1,10 @@
// Mock for virtual:wa-sqlite-wasm-url used in tests
// Returns a file:// URL that works with the fetch polyfill in jest.setup.mjs
// Returns the WASM binary as Uint8Array (same as production build)
import { join } from 'path';
import { pathToFileURL } from 'url';
import { readFileSync } from 'fs';
const wasmPath = join(process.cwd(), 'node_modules/wa-sqlite/dist/wa-sqlite-async.wasm');
const wasmUrl = pathToFileURL(wasmPath).href;
const wasmBinary = new Uint8Array(readFileSync(wasmPath));
export default wasmUrl;
export default wasmBinary;

View file

@ -4,7 +4,7 @@ import SQLiteAsyncESMFactory from 'wa-sqlite/dist/wa-sqlite-async.mjs';
import * as SQLite from 'wa-sqlite';
import { MemoryAsyncVFS } from 'wa-sqlite/src/examples/MemoryAsyncVFS.js';
// @ts-ignore - Virtual module from esbuild
import wasmUrl from 'virtual:wa-sqlite-wasm-url';
import wasmBinary from 'virtual:wa-sqlite-wasm-url';
type ParamsObject = Record<string, any>;
@ -33,15 +33,7 @@ export class WaSqliteMemoryDatabase {
throw new Error('Invalid SQLite database file format');
}
// Initialize wa-sqlite
const asyncModule = await SQLiteAsyncESMFactory({
locateFile: (file: string) => {
if (file.endsWith('.wasm')) {
return wasmUrl;
}
return file;
}
});
const asyncModule = await SQLiteAsyncESMFactory({ wasmBinary, locateFile: (file: string) => file });
this.sqlite3 = SQLite.Factory(asyncModule);