Conversation
Bun's server-side WebSocket uses callback-based handlers registered on `Bun.serve()` rather than the standard `addEventListener` interface that capnweb's WebSocket transport relies on. This means passing a Bun `ServerWebSocket` to `newWebSocketRpcSession()` errors due to the missing interface. This commit adds a new `BunWebSocketTransport` that implements the `RpcTransport` interface directly, using the same resolver pattern as the existing WebSocket and `MessagePort` transports. It exposes `dispatchMessage`, `dispatchClose`, and `dispatchError` methods that Bun's handler callbacks invoke to feed messages into the transport. Two convenience functions sit on top of the transport. `newBunWebSocketRpcSession()` returns the stub and transport for users who need custom handler logic. `newBunWebSocketRpcHandler()` returns a complete WebSocket handler object that can be passed directly to Bun.serve(), wiring everything up automatically via `ws.data` so users don't have to touch the transport at all.
|
All contributors have signed the CLA ✍️ ✅ |
commit: |
🦋 Changeset detectedLatest commit: 60f740e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Document the new Bun WebSocket support added in the previous commit. Add an HTTP server on Bun section showing both the zero-wiring newBunWebSocketRpcHandler API and the lower-level newBunWebSocketRpcSession escape hatch. Add Bun to the introductory runtime list, note in the other runtimes section that Bun's ServerWebSocket requires the dedicated API, and update the custom transports section to list all four built-in transports.
|
I have read the CLA Document and I hereby sign the CLA |
|
recheck |
|
In package.json we currently have a special separate build for workerd that includes the workers-only stuff. Maybe we should have a separate build for bun, too? Avoid code bloat for other runtimes. |
| // We throw flow-control test under Node only because it's testing straightforward | ||
| // JavaScript -- no need to run it on every runtime. | ||
| include: ['__tests__/index.test.ts', '__tests__/flow-control.test.ts'], | ||
| include: ['__tests__/index.test.ts', '__tests__/flow-control.test.ts', '__tests__/bun.test.ts'], |
There was a problem hiding this comment.
I'm a bit confused how the bun test passes on node, unless it's not actually testing integration with Bun's builtin APIs. If it isn't testing integration then I don't think it's a useful test.
Can we add Bun to the test matrix, and write a test that actually tests against Bun's built-in APIs?
Add Bun ServerWebSocket support to capnweb.
Bun's server-side WebSocket uses callback-based handlers registered on
Bun.serve()rather than the standardaddEventListenerinterface. Passing a BunServerWebSockettonewWebSocketRpcSession()silently fails because the event listeners never fire. (#61)A new
BunWebSocketTransportimplements theRpcTransportinterface using the same resolver pattern as the existing WebSocket and MessagePort transports. It exposesdispatchMessage,dispatchClose, anddispatchErrormethods that Bun's 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 toBun.serve(), wiring everything up automatically viaws.data.newBunWebSocketRpcSession()returns the stub and transport for users who need custom handler logic.The readme now documents both approaches and notes that Bun is a supported runtime.
Quick start with the handler helper
Escape hatch with manual wiring
Testing locally
Run the test suite with
npm test. The new tests in__tests__/bun.test.tsexercise the transport, both convenience functions, and full round-trip calls through a loopback pair without needing a real Bun server.Unit tests cover message queuing, close and error propagation, abort semantics, buffer-to-string conversion, and end-to-end calls including bidirectional callbacks and error forwarding. All existing tests continue to pass.
Closes #61