@@ -3,10 +3,20 @@ import { dirname } from 'node:path';
33import { createRequire } from 'node:module' ;
44import { ulid as makeUlid } from 'ulid' ;
55import { createEnvelope } from './envelope.js' ;
6- // node:sqlite 是较新内建,只暴露 `node:sqlite`(无 `sqlite` 裸别名)。
7- // 经 createRequire 动态加载,避免打包器静态剥前缀成 `sqlite` 后解析失败(vitest/vite)。
86const nodeRequire = createRequire ( import . meta. url ) ;
9- const { DatabaseSync } = nodeRequire ( 'node:sqlite' ) ;
7+ function isBunRuntime ( ) {
8+ return typeof process . versions === 'object' && typeof process . versions . bun === 'string' ;
9+ }
10+ function openSqliteDatabase ( path ) {
11+ if ( isBunRuntime ( ) ) {
12+ const { Database } = nodeRequire ( 'bun:sqlite' ) ;
13+ return new Database ( path ) ;
14+ }
15+ // node:sqlite is exposed only as `node:sqlite` (no bare `sqlite` alias). Load it through createRequire so
16+ // bundlers do not statically strip the prefix and break Vitest/Vite or Bun standalone builds.
17+ const { DatabaseSync } = nodeRequire ( 'node:sqlite' ) ;
18+ return new DatabaseSync ( path ) ;
19+ }
1020export const MAX_ATTEMPTS = 5 ;
1121export const DEFAULT_IMPORT_JSONL_MAX_LINE_BYTES = 16 * 1024 * 1024 ;
1222const JSONL_READ_CHUNK_BYTES = 64 * 1024 ;
@@ -95,7 +105,7 @@ export class MailboxStore {
95105 db ;
96106 constructor ( opts ) {
97107 mkdirSync ( dirname ( opts . path ) , { recursive : true } ) ;
98- this . db = new DatabaseSync ( opts . path ) ;
108+ this . db = openSqliteDatabase ( opts . path ) ;
99109 this . db . exec ( 'PRAGMA journal_mode = WAL' ) ;
100110 // PRAGMA can't be parameterized, so the value is string-interpolated — sanitize to a non-negative integer
101111 // first so a non-numeric busyTimeoutMs can never become SQL injection (defense-in-depth; #196).
0 commit comments