Merge pull request #104 from aaronflorey/fix/opencode-sqlite-esm-loader

fix(sqlite): load node:sqlite in ESM runtime
This commit is contained in:
Resham Joshi 2026-04-19 02:11:21 -07:00 committed by GitHub
commit 64aae10175
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,8 +1,12 @@
import { createRequire } from 'node:module'
/// Thin SQLite read-only wrapper over Node's built-in `node:sqlite` module (stable in
/// Node 24, experimental in Node 22 / 23). Replaces the earlier `better-sqlite3` binding
/// so the dependency graph no longer pulls in the deprecated `prebuild-install` package
/// (issue #75). Works across Cursor and OpenCode session DBs, both of which we only read.
const requireForSqlite = createRequire(import.meta.url)
type Row = Record<string, unknown>
export type SqliteDatabase = {
@ -55,10 +59,7 @@ function loadDriver(): boolean {
} as typeof process.emit
try {
// Dynamic require via createRequire avoids TypeScript chasing types we don't need at
// build time (node:sqlite landed in @types/node much later than in Node itself).
// eslint-disable-next-line @typescript-eslint/no-require-imports
const mod = eval('require')('node:sqlite') as { DatabaseSync: DatabaseSyncCtor }
const mod = requireForSqlite('node:sqlite') as { DatabaseSync: DatabaseSyncCtor }
DatabaseSync = mod.DatabaseSync
return true
} catch (err) {