feat: add Bun runtime support with SQLite adapter abstraction#422
feat: add Bun runtime support with SQLite adapter abstraction#422
Conversation
Fixes issue #419 where Node.js v25 fails with ERR_UNSUPPORTED_ESM_URL_SCHEME when importing the package. The issue was caused by is-bun-module@2.0.0 which has a "bun" condition in its exports field that Node.js cannot resolve. By downgrading to v1.3.0 via pnpm overrides, we eliminate the problematic export condition while maintaining all functionality for dev dependencies. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Implement runtime-aware SQLite adapter layer to support both Node.js (via better-sqlite3) and Bun (via bun:sqlite) environments. The adapter pattern provides a minimal 6-method API surface, making the abstraction lightweight and maintainable. - Create DatabaseAdapter abstraction with SqliteDatabase and SqliteStatement interfaces - Implement BetterSqlite3Adapter for Node.js environments - Implement BunSqliteAdapter for Bun environments - Add runtime detection via globalThis.Bun check - Update DatabaseConnectionManager to use adapter factory - Update QueryMonitor to use abstract database types - Update EnhancedCookieQueryService to work with adapters - Update tests to mock adapter factory instead of BetterSqlite3 - Zero breaking changes to public API All tests pass (520 passing), build succeeds, linting clean. Closes #405 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
PR Review: Bun Runtime SQLite Adapter AbstractionOverviewThis PR introduces a clean adapter pattern to abstract SQLite operations, enabling get-cookie to work with both Node.js ( Findings🔴 Critical
🟡 Warnings
🔵 Suggestions
👍 What's Good
SummaryThe original blocker is resolved. Remaining items are warnings/suggestions that don't affect correctness for the current use cases. This is ready to merge as-is if the test gap is acceptable as a follow-up. |
Bun's Database constructor creates a new file by default when the path
doesn't exist. Passing { create: false } when fileMustExist is set
matches better-sqlite3's fileMustExist:true semantics and ensures
missing cookie database paths surface as errors rather than silently
producing empty results.
Also adds BunDatabaseConstructor interface for typed require() cast.
better-sqlite3's pragma() returns data (e.g. PRAGMA journal_mode returns the current mode). Returning void discards this capability. Using unknown preserves it without compromising type safety.
bun:sqlite is a Bun runtime built-in and cannot be bundled by esbuild. Explicitly marking it as external ensures the bundler never attempts to resolve or inline it, keeping the Node.js build clean and making the lazy-loading intent explicit in the build configuration.
Summary of ChangesAll review feedback has been addressed: 🔴 Critical (fixed)
🟡 Warnings (addressed)
🔵 Suggestions (implemented)
All checks pass locally: |
There was a problem hiding this comment.
Critical blocker resolved — BunSqliteAdapter now correctly honours fileMustExist via Bun's create: false option. pragma() return type widened appropriately and bun:sqlite correctly marked external in both tsup configs. Remaining items (adapter unit tests, is-bun-module override, pragma() read semantics on Bun) are non-blocking for the current use cases. LGTM.
Summary
Implements comprehensive Bun runtime support by adding a runtime-aware SQLite adapter layer. This enables the get-cookie package to work seamlessly with both Node.js (via better-sqlite3) and Bun (via bun:sqlite) without requiring code changes from users.
Previous work: Downgraded is-bun-module to resolve Node.js ESM loading errors.
What Changed
New Adapter Layer (
src/core/browsers/sql/adapters/)SqliteDatabaseandSqliteStatementinterfaces with runtime detection and factory functionCore Updates
createSqliteDatabase()factory instead of direct BetterSqlite3 instantiationSqliteDatabasetypeKey Benefits
✅ Zero Breaking Changes: All existing code continues to work unchanged
✅ Type Safe: Full TypeScript support with abstract interfaces
✅ Lightweight: Minimal 6-method API surface (prepare, pragma, close, all, get, run)
✅ Automatic Detection: Runtime detection via globalThis.Bun check
✅ Dynamic Imports: Adapters lazily loaded only when needed
Why
Addresses issue #405 by implementing Bun runtime support using the adapter pattern. This follows SOLID principles and makes the codebase extensible for future SQLite implementations without modifying existing code.
Testing
Implementation Quality
The adapter pattern provides:
Risk / Rollout
Risk Level: Low
Rollout: Immediate - ready for production
🤖 Generated with Claude Code