capnweb 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -1
- package/dist/index-bun.cjs +3512 -0
- package/dist/index-bun.cjs.map +1 -0
- package/dist/index-bun.d.cts +125 -0
- package/dist/index-bun.d.ts +125 -0
- package/dist/index-bun.js +3495 -0
- package/dist/index-bun.js.map +1 -0
- package/dist/index-workers-C1na2_sM.d.cts +444 -0
- package/dist/index-workers-C1na2_sM.d.ts +444 -0
- package/dist/index-workers.d.cts +1 -1
- package/dist/index-workers.d.ts +1 -1
- package/dist/index.d.cts +2 -444
- package/dist/index.d.ts +2 -444
- package/package.json +16 -9
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Cap'n Web is a spiritual sibling to [Cap'n Proto](https://capnproto.org) (and is
|
|
|
6
6
|
* That said, it integrates nicely with TypeScript.
|
|
7
7
|
* Also unlike Cap'n Proto, Cap'n Web's underlying serialization is human-readable. In fact, it's just JSON, with a little pre-/post-processing.
|
|
8
8
|
* It works over HTTP, WebSocket, and postMessage() out-of-the-box, with the ability to extend it to other transports easily.
|
|
9
|
-
* It works in all major browsers, Cloudflare Workers, Node.js, and other modern JavaScript runtimes.
|
|
9
|
+
* It works in all major browsers, Cloudflare Workers, Node.js, Bun, Deno, and other modern JavaScript runtimes.
|
|
10
10
|
The whole thing compresses (minify+gzip) to under 10kB with no dependencies.
|
|
11
11
|
|
|
12
12
|
Cap'n Web is more expressive than almost every other RPC system, because it implements an object-capability RPC model. That means it:
|
|
@@ -630,6 +630,45 @@ Deno.serve(async (req) => {
|
|
|
630
630
|
});
|
|
631
631
|
```
|
|
632
632
|
|
|
633
|
+
### HTTP server on Bun
|
|
634
|
+
|
|
635
|
+
Bun's server-side WebSocket API uses [callback-based handlers](https://bun.sh/docs/runtime/http/websockets) instead of the standard `addEventListener` interface. Cap'n Web provides `newBunWebSocketRpcHandler()` which returns a handler object you can pass directly to `Bun.serve()`.
|
|
636
|
+
|
|
637
|
+
```ts
|
|
638
|
+
import { RpcTarget, newBunWebSocketRpcHandler, newHttpBatchRpcResponse } from "capnweb";
|
|
639
|
+
|
|
640
|
+
class MyApiImpl extends RpcTarget implements MyApi {
|
|
641
|
+
// ... define API, same as above ...
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Create a WebSocket handler that manages RPC sessions automatically.
|
|
645
|
+
// The callback is invoked once per connection to create a fresh API instance.
|
|
646
|
+
let rpcHandler = newBunWebSocketRpcHandler(() => new MyApiImpl());
|
|
647
|
+
|
|
648
|
+
Bun.serve({
|
|
649
|
+
async fetch(req, server) {
|
|
650
|
+
let url = new URL(req.url);
|
|
651
|
+
if (url.pathname === "/api") {
|
|
652
|
+
// Upgrade WebSocket requests.
|
|
653
|
+
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
|
654
|
+
if (server.upgrade(req)) return;
|
|
655
|
+
return new Response("WebSocket upgrade failed", { status: 500 });
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// Handle HTTP batch requests.
|
|
659
|
+
let response = await newHttpBatchRpcResponse(req, new MyApiImpl());
|
|
660
|
+
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
661
|
+
return response;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
return new Response("Not Found", { status: 404 });
|
|
665
|
+
},
|
|
666
|
+
|
|
667
|
+
// Pass the handler directly — no manual wiring needed.
|
|
668
|
+
websocket: rpcHandler,
|
|
669
|
+
});
|
|
670
|
+
```
|
|
671
|
+
|
|
633
672
|
### HTTP server on other runtimes
|
|
634
673
|
|
|
635
674
|
Every runtime does HTTP handling and WebSockets a little differently, although most modern runtimes use the standard `Request` and `Response` types from the Fetch API, as well as the standard `WebSocket` API. You should be able to use these two functions (exported by `capnweb`) to implement both HTTP batch and WebSocket handling on all platforms:
|