两个小型的 Bun 原生包:tRPC 通过 Bun.serve 和 Kysely Postgres 方言。
我在几个单体仓库中使用了 Bun-first 技术栈,并最终提取了两个小包,可能会为其他人节省一些粘合代码。这两个包均为 MIT 许可证,支持 ESM,仅避免使用 Node 兼容层。
— trpc-bun(Bun 原生的 tRPC 适配器:HTTP + WebSocket)
**功能**:在 Bun.serve 上运行 tRPC,提供一流的 HTTP 和 WebSocket 支持。
**实现方式**:通过 fetch 适配器和 server.upgrade 实现 HTTP;通过 Bun 的 WebSocket 处理器实现 WebSocket;一行代码组合服务器;可选的重连广播。
**要求**:Bun ≥ 1.3.0,@trpc/server ≥ 11.6.0,TS ≥ 5。
**仓库**:[https://github.com/lacion/trpc-bun](https://github.com/lacion/trpc-bun)
**亮点**:
• 仅使用公共 tRPC 服务器 API(v11)
• createTrpcBunFetchAdapter、createTrpcBunWebSocketAdapter、configureTrpcBunServer
• 通过 WebSocket 传递连接参数、订阅、变更、错误处理
• 防止重复 ID,优雅停止/断开连接
• bun test + GitHub Actions CI
— kysely-bun-sql(基于 Bun SQL 的 Kysely Postgres 方言)
**功能**:一个小巧、无依赖的 Kysely 方言/驱动,使用 Bun 的原生 SQL 客户端。
**实现方式**:底层使用 SQL 连接池(reserve()/release()),Kysely 的 Postgres 适配器 + 查询编译器。
**要求**:Bun ≥ 1.1.31,Kysely ≥ 0.28,TS ≥ 5。
**仓库**:[https://github.com/lacion/kysely-bun-sql](https://github.com/lacion/kysely-bun-sql)
**亮点**:
• 通过新的 SQL() 或环境自动检测实现 Bun 原生 PostgreSQL
• 连接池、预编译语句、通过 Bun SQL 进行参数绑定
• 完整的 Kysely 集成(适配器、查询编译器、反射器)
• 通过 Kysely 实现事务和保存点
• 无运行时依赖,仅支持 ESM
我在实际项目中运行了这两个包;发布它们是为了让更多人关注边缘案例。希望能收到关于正确性、易用性和 Bun 版本特性的反馈。欢迎提出问题或提交 PR。感谢您的关注。
查看原文
I’ve been using Bun-first stacks in a few monorepos and ended up extracting two small packages that might save others some glue code. Both are MIT, ESM-only, and avoid Node shims.<p>— trpc-bun (Bun-native tRPC adapter: HTTP + WebSocket)
What: Run tRPC on Bun.serve with first-class HTTP and WS.
How: HTTP via a fetch adapter + server.upgrade; WS via Bun’s websocket handler; one-liner server composer; optional reconnect broadcast.
Reqs: Bun ≥ 1.3.0, @trpc/server ≥ 11.6.0, TS ≥ 5.
Repo: https://github.com/lacion/trpc-bun<p>Highlights:
• Uses only public tRPC server APIs (v11)
• createTrpcBunFetchAdapter, createTrpcBunWebSocketAdapter, configureTrpcBunServer
• Connection params over WS, subscriptions, mutations, error shaping
• Duplicate-id protection, graceful stop/disconnect
• bun test + GitHub Actions CI<p>— kysely-bun-sql (Kysely Postgres dialect backed by Bun SQL)
What: Tiny, dependency-free Kysely dialect/driver using Bun’s native SQL client.
How: Pooled SQL under the hood (reserve()/release()), Kysely’s Postgres adapter + query compiler.
Reqs: Bun ≥ 1.1.31, Kysely ≥ 0.28, TS ≥ 5.
Repo: https://github.com/lacion/kysely-bun-sql<p>Highlights:
• Bun-native PostgreSQL via new SQL() or env auto-detection
• Pooling, prepared statements, parameter binding via Bun SQL
• Full Kysely integration (adapter, query compiler, introspector)
• Transactions and savepoints through Kysely
• No runtime deps, ESM only<p>I’ve been running both in real projects; publishing them to get more eyes on edge cases. Would love feedback on correctness, ergonomics, and Bun version quirks. Issues/PRs welcome. Thanks for taking a look.