Commit 7cb9132
Add bun support (#159)
* Add Bun ServerWebSocket support
Bun uses callback-based WebSocket handlers registered on `Bun.serve()`
rather than the standard `addEventListener` interface. Passing a Bun
ServerWebSocket to `newWebSocketRpcSession()` errors because the
`addEventListener` method does not exist (#61).
A new BunWebSocketTransport implements the RpcTransport interface using
the same resolver pattern as the existing WebSocket and MessagePort
transports. It exposes dispatchMessage, dispatchClose, and dispatchError
methods that Bun handler callbacks invoke to feed messages into the
transport.
Two convenience functions sit on top of the transport.
`newBunWebSocketRpcHandler()` returns a complete handler object you can
pass straight to `Bun.serve()`, wiring everything up automatically via
`ws.data`. `newBunWebSocketRpcSession()` returns the stub and transport
for users who need custom handler logic.
The Bun-specific code ships in a separate entry point (index-bun.ts)
mapped via a "bun" conditional export in package.json, following the
same pattern as the existing workerd build. Other runtimes never load
this code.
The test suite in __tests__/bun.test.ts runs under Bun native test
runner against real `Bun.serve()` instances with actual WebSocket
connections. Run with `npm run test:bun` or `npm run test:ci`.
Closes #61
* Add Bun to the CI test workflow
Adds the oven-sh/setup-bun action so the Bun runtime is available in
the Playwright container. The test step now runs `test:ci` which chains
Vitest and Bun tests in sequence.
* Document Bun support and development setup
Adds an "HTTP server on Bun" section to the readme showing both the
handler helper and the manual wiring escape hatch. Notes that the Bun
helpers ship via a conditional export and are never loaded by other
runtimes.
Adds a development section documenting the test scripts: npm test for
vitest only, npm run test:bun for the Bun suite, and npm run test:ci
for both together.
* Hoist platform conditions so TypeScript resolves per-platform types
Previously, "bun" and "workerd" were nested inside "import"/"require",
which meant TypeScript always fell through to the single top-level
"types" entry and picked up index.d.ts regardless of the active
condition.
Each platform block is now a top-level condition with its own "types",
"import", and "require" keys:
- "bun" resolves to index-bun.d.ts, which exposes BunWebSocketTransport,
BunServerWebSocket, and the Bun-specific session helpers
- "workerd" shares the same declarations as the default but keeps a
distinct runtime (index-workers.js) that imports cloudflare:workers
and registers it for native RPC interop
- The bare "types"/"import"/"require" fallback covers Node.js and
every other environment
* Update README.md
Remove example of custom Bun websocket transport
* Add changeset
---------
Co-authored-by: aron-cf <aron-cf@users.noreply.github.com>
Co-authored-by: Kenton Varda <kenton@cloudflare.com>1 parent cfa1b95 commit 7cb9132
9 files changed
Lines changed: 594 additions & 11 deletions
File tree
- .changeset
- .github/workflows
- __tests__
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
34 | | - | |
| 37 | + | |
35 | 38 | | |
36 | 39 | | |
37 | 40 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
630 | 630 | | |
631 | 631 | | |
632 | 632 | | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
633 | 672 | | |
634 | 673 | | |
635 | 674 | | |
| |||
0 commit comments