awesome-ocap icon indicating copy to clipboard operation
awesome-ocap copied to clipboard

CapTP on cloudflare workers?

Open dckc opened this issue 4 years ago • 5 comments

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

dckc avatar Aug 09 '21 15:08 dckc

At some point I plan to expose Cap'n Proto RPC directly to Workers, especially for communication between Durable Objects...

kentonv avatar Aug 10 '21 14:08 kentonv

looks like some point was about a year ago...

dckc avatar May 24 '25 22:05 dckc

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.

kentonv avatar May 24 '25 22:05 kentonv

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).

dckc avatar May 24 '25 23:05 dckc

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).

kentonv avatar May 25 '25 18:05 kentonv