两个小巧的 Bun 原生软件包:基于 Bun.serve 的 tRPC 和 Kysely Postgres 方言。

1作者: lacion8 个月前
我一直在几个 monorepo 中使用 Bun 优先栈,最终提取了两个小包,可能会为其他人节省一些粘合代码。这两个包都使用 MIT 协议,仅支持 ESM,并避免了 Node 模拟。<p>— trpc-bun (Bun 原生 tRPC 适配器:HTTP + WebSocket) 用途:在 Bun.serve 上运行 tRPC,提供一流的 HTTP 和 WS 支持。 实现方式:通过 fetch 适配器 + server.upgrade 实现 HTTP;通过 Bun 的 websocket 处理器实现 WS;一行代码服务器组合器;可选的重新连接广播。 要求:Bun ≥ 1.3.0, @trpc&#x2F;server ≥ 11.6.0, TS ≥ 5。 仓库:https:&#x2F;&#x2F;github.com&#x2F;lacion&#x2F;trpc-bun<p>亮点: • 仅使用公共 tRPC 服务器 API (v11) • createTrpcBunFetchAdapter, createTrpcBunWebSocketAdapter, configureTrpcBunServer • 通过 WS 传递连接参数、订阅、修改、错误处理 • 重复 ID 保护,优雅的停止/断开连接 • bun test + GitHub Actions CI<p>— kysely-bun-sql (基于 Bun SQL 的 Kysely Postgres 方言) 用途:使用 Bun 原生 SQL 客户端的、轻量级、无依赖的 Kysely 方言/驱动程序。 实现方式:底层使用连接池 SQL (reserve()/release()),Kysely 的 Postgres 适配器 + 查询编译器。 要求:Bun ≥ 1.1.31, Kysely ≥ 0.28, TS ≥ 5。 仓库:https:&#x2F;&#x2F;github.com&#x2F;lacion&#x2F;kysely-bun-sql<p>亮点: • 通过新的 SQL() 或 env 自动检测实现 Bun 原生 PostgreSQL • 通过 Bun SQL 实现连接池、预处理语句、参数绑定 • 完整的 Kysely 集成 (适配器、查询编译器、内省器) • 通过 Kysely 实现事务和保存点 • 无运行时依赖,仅支持 ESM<p>我一直在实际项目中运行这两个包;发布它们是为了让更多人关注边缘情况。欢迎就正确性、人体工程学和 Bun 版本相关的怪癖提出反馈。欢迎提交 Issue/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&#x2F;server ≥ 11.6.0, TS ≥ 5. Repo: https:&#x2F;&#x2F;github.com&#x2F;lacion&#x2F;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&#x2F;disconnect • bun test + GitHub Actions CI<p>— kysely-bun-sql (Kysely Postgres dialect backed by Bun SQL) What: Tiny, dependency-free Kysely dialect&#x2F;driver using Bun’s native SQL client. How: Pooled SQL under the hood (reserve()&#x2F;release()), Kysely’s Postgres adapter + query compiler. Reqs: Bun ≥ 1.1.31, Kysely ≥ 0.28, TS ≥ 5. Repo: https:&#x2F;&#x2F;github.com&#x2F;lacion&#x2F;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&#x2F;PRs welcome. Thanks for taking a look.