capnweb 0.0.0-76fdff1 → 0.0.0-7cb9132
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 +54 -3
- 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.cjs +826 -43
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +1 -1
- package/dist/index-workers.d.ts +1 -1
- package/dist/index-workers.js +826 -43
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +826 -43
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -372
- package/dist/index.d.ts +2 -372
- package/dist/index.js +826 -43
- package/dist/index.js.map +1 -1
- package/package.json +22 -14
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:
|
|
@@ -201,13 +201,13 @@ The following types can be passed over RPC (in arguments or return values), and
|
|
|
201
201
|
* `Date`
|
|
202
202
|
* `Uint8Array`
|
|
203
203
|
* `Error` and its well-known subclasses
|
|
204
|
+
* `ReadableStream` and `WritableStream`, with automatic flow control.
|
|
205
|
+
* `Headers`, `Request`, and `Response` from the Fetch API.
|
|
204
206
|
|
|
205
207
|
The following types are not supported as of this writing, but may be added in the future:
|
|
206
208
|
* `Map` and `Set`
|
|
207
209
|
* `ArrayBuffer` and typed arrays other than `Uint8Array`
|
|
208
210
|
* `RegExp`
|
|
209
|
-
* `ReadableStream` and `WritableStream`, with automatic flow control.
|
|
210
|
-
* `Headers`, `Request`, and `Response`
|
|
211
211
|
|
|
212
212
|
The following are intentionally NOT supported:
|
|
213
213
|
* Application-defined classes that do not extend `RpcTarget`.
|
|
@@ -305,6 +305,10 @@ The trick here is record-replay: On the calling side, Cap'n Web will invoke your
|
|
|
305
305
|
|
|
306
306
|
Since all of the not-yet-determined values seen by the callback are represented as `RpcPromise`s, the callback's behavior is deterministic. Any actual computation (arithmetic, branching, etc.) can't possibly use these promises as (meaningful) inputs, so would logically produce the same results for every invocation of the callback. Any such computation will actually end up being performed on the sending side, just once, with the results being imbued into the recording.
|
|
307
307
|
|
|
308
|
+
### Streaming with flow control
|
|
309
|
+
|
|
310
|
+
You may pass a `ReadableStream` or `WritableStream` over RPC. When doing so, the RPC system automatically creates an equivalent stream at the other end and pumps bytes (or arbitrarily-typed chunks) across. This is done in such a way as to ensure the available bandwidth is fully utilized while minimizing buffer bloat, by observing the bandwidth-delay product and applying backpressure when too much is written. Multiple streams can be sent across the same connection -- they will be multiplexed appropriately, similar to HTTP/2 stream multiplexing.
|
|
311
|
+
|
|
308
312
|
### Cloudflare Workers RPC interoperability
|
|
309
313
|
|
|
310
314
|
Cap'n Web works on any JavaScript platform. But, on Cloudflare Workers specifically, it's designed to play nicely with the [the built-in RPC system](https://blog.cloudflare.com/javascript-native-rpc/). The two have basically the same semantics, the only difference being that Workers RPC is a built-in API provided by the Workers Runtime, whereas Cap'n Web is implemented in pure JavaScript.
|
|
@@ -528,6 +532,14 @@ export default {
|
|
|
528
532
|
}
|
|
529
533
|
```
|
|
530
534
|
|
|
535
|
+
#### Compatibility with Workers' built-in RPC
|
|
536
|
+
|
|
537
|
+
Cloudflare Workers has long featured [a built-in RPC system with semantics similar to Cap'n Web](https://developers.cloudflare.com/workers/runtime-apis/rpc/).
|
|
538
|
+
|
|
539
|
+
Cap'n Web is designed to be compatible with Workers RPC, meaning you can pass Cap'n Web RPC stubs over Workers RPC and vice versa. The system will automatically wrap one stub type in the other and arrange to proxy calls.
|
|
540
|
+
|
|
541
|
+
For best compatibility, make sure to set your [Workers compatibilty date](https://developers.cloudflare.com/workers/configuration/compatibility-dates/) to at least `2026-01-20`, or enable the [compatibility flag](https://developers.cloudflare.com/workers/configuration/compatibility-flags/) `rpc_params_dup_stubs`. (As of this writing, `2026-01-20` is in the future, so you will need to use the flag for now.)
|
|
542
|
+
|
|
531
543
|
### HTTP server on Node.js
|
|
532
544
|
|
|
533
545
|
A server on Node.js is a bit more involved, due to the awkward handling of WebSockets in Node's HTTP library.
|
|
@@ -618,6 +630,45 @@ Deno.serve(async (req) => {
|
|
|
618
630
|
});
|
|
619
631
|
```
|
|
620
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
|
+
|
|
621
672
|
### HTTP server on other runtimes
|
|
622
673
|
|
|
623
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:
|