workers-rs icon indicating copy to clipboard operation
workers-rs copied to clipboard

Added support for DO RPC

Open spigaz opened this issue 11 months ago • 3 comments

I'm not sure its the best approach but apparently it works, hopefully without regressions.

If I understood correctly, DOs required an extra shim to establish the connection to DurableObject from cloudflare:workers to get over this:

Error: TypeError: The receiving Durable Object does not support RPC, because its class was not declared with `extends DurableObject`. In order to enable RPC, make sure your class extends the special class `DurableObject`, which can be imported from the module "cloudflare:workers".

Modular approach

Right now, it gets the DOs information from Wrangler.toml.

But it allows to add support easily to other Wrangler formats and other shims, including for service workers.

The DOs shim is only injected when DOs are detected, so no overhead on cold starts if not necessary.

I updated the DurableObject fetch method to be optional.

Examples

I already added a couple of examples:

  • One with a RPC Server and a RPC Client
  • One with the non RPC version

I already had more examples in the pipeline, but the PR was getting too big.

The server side usage is quite simple

But support is still limited to native types and client side remains as it was.

use wasm_bindgen::prelude::wasm_bindgen;
use worker::*;

#[durable_object]
pub struct RPCServer {
    counter: u32,
}

#[wasm_bindgen]
impl RPCServer {
    #[wasm_bindgen]
    pub fn add(&mut self, a: u32, b: u32) -> u32 {
        console_error_panic_hook::set_once();
        self.counter += 1;
        a + b + self.counter
    }
}

#[durable_object]
impl DurableObject for RPCServer {
    fn new(state: State, _env: Env) -> Self {
        Self { counter: 0 }
    }
}

spigaz avatar May 09 '25 20:05 spigaz

but it would help to have an explicit test as well for this behaviour including perhaps some of the edge cases and error cases.

I thought about that, even about adding more examples, but the PR was getting big and I wasn't even sure it was going to get approved.

Besides the tests weren't being valued properly.

But its okay, just let me know the test cases you want me to cover that I'll do my best.

spigaz avatar Jun 03 '25 18:06 spigaz

The failing test can be fixed with this change - https://github.com/cloudflare/workers-rs/pull/737/files#diff-dadc77a902a1d8dd154afd35c4c3dd86a6b287c5fd6c806f105973a1dd1ec7e8.

It looks like everything is passing except for the formatting otherwise.

In terms of increasing test coverage:

  • Is it possible to move the simplest example into the worker-sandbox folder as a case there with tests against it. I think the examples should be more about readable code then.
  • In terms of additional edge cases it would be nice to test more variations of the RPC shapes beyond just numbers. What happens when unsupported types are used? Testing known errors / invalid paths would also be great to see if possible.

guybedford avatar Jun 03 '25 18:06 guybedford

The idea of the sample was to get what Kevin did working on DOs as a starter, as I'm not sure how far he went with RPC support.

I'll give it a look and try to understand what the current boundaries are.

spigaz avatar Jun 03 '25 20:06 spigaz