How to return a plain object and not a stub from a Worker with RPC binding?
I'm working with a Cloudflare Worker that is called from a Cloudflare Pages project using Services Binding + RPC. I'd like to be able to configure or convert a plain object returned from a target instance to be serialized to an object and not be returned as a stub.
As an example, consider the following projects:
TYPE LIB: Standalone library with types I want to reference in other projects.
SHARED WORKER: Worker providing services via service bindings and RPC.
END APP: The calling code to the shared worker.
When I try the above, it appears to return myobj as a stub, and TypeScript in VSCode complains as the types don't match. Further, it would seem any reference to myobj properties would be executed as RPC calls which is inefficient.
Is there a way to configure or convert myobj efficiently so that it is a plain object matching the MyObject type?
Thanks in advance!
1 Reply
Bumping this as I found myself in a very similar situation!
If we change OP's example MyObject to:
then the returned object has the following type:
This is of course just an example. My use case is with a large and complex
zod
parsed object... array.
Now, either I don't fully understand when and how RPC methods via service-bound Workers should be used, or there are issues in @cloudflare/workers-types: 4.20241205.0
which defines:
but might not be called/applied.
TLDR: Are we always supposed to serialize and deserialize complex objects between Workers? Then what's the purpose of RPC methods "running on the same thread" if we can't pass concrete typed objects?