Skip to content

Commit

Permalink
chore: Remove Proxy from json rpc client (#10554)
Browse files Browse the repository at this point in the history
Using a proxy as a json rpc client means that dynamic checks for
functions in the returned object fail. For instance, `typeof
pxe.getTransactions` returns true even if the method is not part of the
pxe schema, since the proxy creates a fake function for every single
property requested. But then it fails when we try to invoke it.

Since we now have schemas, we can drop usage of the proxy and just
create an object with exactly the methods we need.
  • Loading branch information
spalladino authored Dec 9, 2024
1 parent 7441767 commit 93b1c45
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,10 @@ export function createSafeJsonRpcClient<T extends object>(
return (schema as ApiSchema)[methodName].returnType().parse(res.result);
};

// Intercept any RPC methods with a proxy
const proxy = new Proxy(
{},
{
get: (target, method: string) => {
if (['then', 'catch'].includes(method)) {
return Reflect.get(target, method);
}
return (...params: any[]) => request(method, params);
},
},
) as T;
const proxy: any = {};
for (const method of Object.keys(schema)) {
proxy[method] = (...params: any[]) => request(method, params);
}

return proxy;
return proxy as T;
}
6 changes: 2 additions & 4 deletions yarn-project/foundation/src/json-rpc/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ describe('JsonRpc integration', () => {
await expect(() => client.fail()).rejects.toThrow('Test state failed');
});

it('fails if calls non-existing method in handler', async () => {
await expect(() => (client as TestState).forceClear()).rejects.toThrow(
'Unspecified method forceClear in client schema',
);
it('fails if calls non-existing method in handler', () => {
expect(() => (client as TestState).forceClear()).toThrow(/not a function/i);
});
});

Expand Down

0 comments on commit 93b1c45

Please sign in to comment.