Blazius
Blazius
CDCloudflare Developers
Created by pneutam on 11/18/2024 in #workers-help
How to return a plain object and not a stub from a Worker with RPC binding?
Bumping this as I found myself in a very similar situation! If we change OP's example MyObject to:
type MyObject = {
name: string;
birthDate: Date;
}
type MyObject = {
name: string;
birthDate: Date;
}
then the returned object has the following type:
{
name: string;
birthDate: {
toString: Rpc.Stub<() => string>;
toDateString: Rpc.Stub<() => string>;
toTimeString: Rpc.Stub<() => string>;
toLocaleString: Rpc.Stub<...>;
... 39 more ...;
[Symbol.toPrimitive]: Rpc.Stub<...>;
}
};
{
name: string;
birthDate: {
toString: Rpc.Stub<() => string>;
toDateString: Rpc.Stub<() => string>;
toTimeString: Rpc.Stub<() => string>;
toLocaleString: Rpc.Stub<...>;
... 39 more ...;
[Symbol.toPrimitive]: Rpc.Stub<...>;
}
};
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:
type UnstubifyAll<A extends any[]> = {
[I in keyof A]: Unstubify<A[I]>;
};
type UnstubifyAll<A extends any[]> = {
[I in keyof A]: Unstubify<A[I]>;
};
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?
3 replies