fix: use dynamic import for node:sqlite with Node version guard

Replace static top-level import with runtime version check and dynamic
import() so Node < 22.5 gracefully falls back to CLI path instead of
crashing at module load time.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ekko
2026-04-18 09:34:59 +08:00
parent fd7071b75d
commit 35481e452d
2 changed files with 11 additions and 2 deletions
@@ -1,6 +1,10 @@
import { DatabaseSync } from 'node:sqlite'
import { getActiveProfileDir } from './hermes-profile'
const SQLITE_AVAILABLE = (() => {
const [major, minor] = process.versions.node.split('.').map(Number)
return major > 22 || (major === 22 && minor >= 5)
})()
export interface HermesSessionRow {
id: string
source: string
@@ -115,6 +119,11 @@ const BASE_SELECT = `
`
export async function listSessionSummaries(source?: string, limit = 2000): Promise<HermesSessionRow[]> {
if (!SQLITE_AVAILABLE) {
throw new Error(`node:sqlite requires Node >= 22.5, current: ${process.versions.node}`)
}
const { DatabaseSync } = await import('node:sqlite')
const db = new DatabaseSync(sessionDbPath(), { open: true, readOnly: true })
try {
+1 -1
View File
@@ -6,7 +6,7 @@ const closeMock = vi.fn()
const databaseSyncMock = vi.fn(() => ({ prepare: prepareMock, close: closeMock }))
const getActiveProfileDirMock = vi.fn(() => '/tmp/hermes-profile')
vi.mock('node:sqlite', () => ({
vi.doMock('node:sqlite', () => ({
DatabaseSync: databaseSyncMock,
}))