Add WASM fallbacks for tree-sitter and SQLite, fix installer

Replace native tree-sitter with web-tree-sitter + tree-sitter-wasms for
universal cross-platform support. Add node-sqlite3-wasm as a fallback
when better-sqlite3 native bindings aren't available. Move better-sqlite3
and sqlite-vss to optionalDependencies so installs never fail.

Fix installer to use npx fallback when global npm install fails, so MCP
config, hooks, and quick-start instructions all work without the bare
codegraph command in PATH.

Fix tests: update schema version expectation, fix db test paths and
method names, extract MAX_OUTPUT_LENGTH as module constant, normalize
Windows path separators in import resolver.
This commit is contained in:
Colby McHenry
2026-02-14 00:56:15 -06:00
parent 429359c25f
commit 8346440592
24 changed files with 707 additions and 781 deletions
+227
View File
@@ -0,0 +1,227 @@
/**
* SQLite Adapter
*
* Provides a unified interface over better-sqlite3 (native) and
* node-sqlite3-wasm (WASM fallback) for universal cross-platform support.
*/
export interface SqliteStatement {
run(...params: any[]): { changes: number; lastInsertRowid: number | bigint };
get(...params: any[]): any;
all(...params: any[]): any[];
}
export interface SqliteDatabase {
prepare(sql: string): SqliteStatement;
exec(sql: string): void;
pragma(str: string): any;
transaction<T>(fn: (...args: any[]) => T): (...args: any[]) => T;
close(): void;
readonly open: boolean;
}
export type SqliteBackend = 'native' | 'wasm';
let activeBackend: SqliteBackend | null = null;
/**
* Get the currently active SQLite backend.
*/
export function getActiveBackend(): SqliteBackend | null {
return activeBackend;
}
/**
* Translate @named parameters (better-sqlite3 style) to positional ? params
* for node-sqlite3-wasm, which only supports positional binding.
*
* Returns the rewritten SQL and an ordered list of parameter names.
* If no named params are found, returns null for paramOrder (positional mode).
*/
function translateNamedParams(sql: string): { sql: string; paramOrder: string[] | null } {
const paramOrder: string[] = [];
const rewritten = sql.replace(/@(\w+)/g, (_match, name: string) => {
paramOrder.push(name);
return '?';
});
if (paramOrder.length === 0) {
return { sql, paramOrder: null };
}
return { sql: rewritten, paramOrder };
}
/**
* Convert better-sqlite3-style params to a positional array for node-sqlite3-wasm.
*
* Handles three calling conventions:
* - Named object: run({ id: '1', name: 'a' }) → positional array via paramOrder
* - Positional args: run('a', 'b') → ['a', 'b']
* - No args: run() → undefined
*/
function resolveParams(params: any[], paramOrder: string[] | null): any {
if (params.length === 0) return undefined;
// If paramOrder exists and first arg is a plain object, do named→positional translation
if (paramOrder && params.length === 1 && params[0] !== null && typeof params[0] === 'object' && !Array.isArray(params[0]) && !(params[0] instanceof Buffer) && !(params[0] instanceof Uint8Array)) {
const obj = params[0];
return paramOrder.map(name => obj[name]);
}
// Positional: single value or already an array
if (params.length === 1) return params[0];
return params;
}
/**
* Wraps node-sqlite3-wasm to match the better-sqlite3 interface.
*
* Key differences handled:
* - better-sqlite3 uses @named params; node-sqlite3-wasm uses positional ? only
* - better-sqlite3 uses variadic args: stmt.run(a, b, c)
* - node-sqlite3-wasm uses a single array/object: stmt.run([a, b, c])
* - node-sqlite3-wasm has `isOpen` instead of `open`
* - node-sqlite3-wasm doesn't have a `pragma()` method
* - node-sqlite3-wasm doesn't have a `transaction()` method
*/
class WasmDatabaseAdapter implements SqliteDatabase {
private _db: any;
// Track raw WASM statements so we can finalize them on close.
// node-sqlite3-wasm won't release its file lock if statements are left open.
private _openStmts = new Set<any>();
constructor(dbPath: string) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { Database } = require('node-sqlite3-wasm');
this._db = new Database(dbPath);
}
get open(): boolean {
return this._db.isOpen;
}
prepare(sql: string): SqliteStatement {
const { sql: rewrittenSql, paramOrder } = translateNamedParams(sql);
const stmt = this._db.prepare(rewrittenSql);
this._openStmts.add(stmt);
return {
run(...params: any[]) {
const resolved = resolveParams(params, paramOrder);
const result = resolved !== undefined ? stmt.run(resolved) : stmt.run();
return {
changes: result?.changes ?? 0,
lastInsertRowid: result?.lastInsertRowid ?? 0,
};
},
get(...params: any[]) {
const resolved = resolveParams(params, paramOrder);
return resolved !== undefined ? stmt.get(resolved) : stmt.get();
},
all(...params: any[]) {
const resolved = resolveParams(params, paramOrder);
return resolved !== undefined ? stmt.all(resolved) : stmt.all();
},
};
}
exec(sql: string): void {
this._db.exec(sql);
}
pragma(str: string): any {
const trimmed = str.trim();
// Write pragma: "key = value"
if (trimmed.includes('=')) {
const eqIdx = trimmed.indexOf('=');
const key = trimmed.substring(0, eqIdx).trim();
const value = trimmed.substring(eqIdx + 1).trim();
// WAL is not supported in WASM SQLite — use DELETE journal mode
if (key === 'journal_mode' && value.toUpperCase() === 'WAL') {
this._db.exec('PRAGMA journal_mode = DELETE');
return;
}
// mmap is not available in WASM — silently skip
if (key === 'mmap_size') {
return;
}
// synchronous = NORMAL is unsafe without WAL — use FULL
if (key === 'synchronous' && value.toUpperCase() === 'NORMAL') {
this._db.exec('PRAGMA synchronous = FULL');
return;
}
this._db.exec(`PRAGMA ${key} = ${value}`);
return;
}
// Read pragma: "key" — return the value
const stmt = this._db.prepare(`PRAGMA ${trimmed}`);
const result = stmt.get();
stmt.finalize();
return result;
}
transaction<T>(fn: (...args: any[]) => T): (...args: any[]) => T {
return (...args: any[]) => {
this._db.exec('BEGIN');
try {
const result = fn(...args);
this._db.exec('COMMIT');
return result;
} catch (error) {
this._db.exec('ROLLBACK');
throw error;
}
};
}
close(): void {
// Finalize all tracked statements before closing.
// node-sqlite3-wasm won't release its directory-based file lock
// if any prepared statements remain open.
for (const stmt of this._openStmts) {
try { stmt.finalize(); } catch { /* already finalized */ }
}
this._openStmts.clear();
this._db.close();
}
}
/**
* Create a database connection. Tries native better-sqlite3 first,
* falls back to node-sqlite3-wasm.
*/
export function createDatabase(dbPath: string): SqliteDatabase {
let nativeError: string | undefined;
let wasmError: string | undefined;
// Try native better-sqlite3 first
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const Database = require('better-sqlite3');
const db = new Database(dbPath);
activeBackend = 'native';
return db as SqliteDatabase;
} catch (error) {
nativeError = error instanceof Error ? error.message : String(error);
}
// Fall back to WASM
try {
const db = new WasmDatabaseAdapter(dbPath);
activeBackend = 'wasm';
console.warn('[CodeGraph] Using WASM SQLite backend (native better-sqlite3 unavailable)');
return db;
} catch (error) {
wasmError = error instanceof Error ? error.message : String(error);
}
throw new Error(
`Failed to load any SQLite backend.\n` +
` Native (better-sqlite3): ${nativeError}\n` +
` WASM (node-sqlite3-wasm): ${wasmError}`
);
}