Skip to content

Added support for DO RPC #726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Added support for DO RPC #726

wants to merge 7 commits into from

Conversation

spigaz
Copy link
Contributor

@spigaz spigaz commented May 9, 2025

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 }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant