CapTP on cloudflare workers?
I wonder about using CapTP on cloudflare workers. What for? I dunno... maybe to host a chat demo, to start.
@michaelfig @kriskowal is this even worth thinking about? (should I move this issue somewhere in https://github.com/ocapn/ ?)
context: I got the idea while looking at Using Cloudflare workers with rnode and your webApp - Rholang SDK usage - Rchain network
cc @kentonv @cwebber
At some point I plan to expose Cap'n Proto RPC directly to Workers, especially for communication between Durable Objects...
looks like some point was about a year ago...
Sort of. Workers' JavaScript RPC layers on top of capnp RPC under the hood, and implements a capability model. But Workers RPC does not have schemas; it uses entirely dynamic typing, to match the expectations of JavaScript developers. The serialization is not strictly Cap'n Proto -- JavaScript values are serialized using V8's built-in serializer, with the bytes then being embedded in capnp messages for transport.
Workers RPC is also currently limited to worker-to-worker communications, that is, entirely within Cloudflare's network. I am, however, working on a version of the protocol that can operate outside Cloudflare's network -- but that version won't be based on Cap'n Proto at all, since the Cap'n Proto encoding doesn't make a lot of sense when the application doesn't use schemas.
I still would like to also make schema-ful Cap'n Proto available in Workers someday but I'm not sure when that will happen.
JavaScript values are serialized using V8's built-in serializer
can those values include capability references?
with the bytes then being embedded in capnp messages for transport.
Do those messages have something like Payload with content and capTable?
(btw... @endo/marshal calls them body and slots and has a CapData structure analagous to Payload).
can those values include capability references?
Of course! But (for now) they are not arbitrary capnp capabilities; they are expected to implement the JSRPC calling convention specifically.
(V8 serialization allows the embedder to extend it with arbitrary "host objects", which is how we embed capabilities.)
Do those messages have something like Payload with content and capTable?
The protocol is defined here: https://github.com/cloudflare/workerd/blob/main/src/workerd/io/worker-interface.capnp#L426
Under the hood, this layers on top of capnp and leverages the capnp cap table, but as you can see there is another layer of table called "externals" here. The v8-serialized value contains references into "externals" which in turn contains capnp capabilities which are references into capTable. This is necessary so that the generic code to copy a capnp struct can work on these values without losing track of the capabilities (since said generic code doesn't know how to decode the V8 serialization).