From 5ed4d11241b5bc82b099cc54be9e5c4e834d8b44 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Mon, 9 Dec 2024 18:36:32 -0300 Subject: [PATCH] chore: Remove Proxy from json rpc client 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. --- .../json-rpc/client/safe_json_rpc_client.ts | 18 +++++------------- .../src/json-rpc/test/integration.test.ts | 6 ++---- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/yarn-project/foundation/src/json-rpc/client/safe_json_rpc_client.ts b/yarn-project/foundation/src/json-rpc/client/safe_json_rpc_client.ts index 2de143063b6..c1bd31c4c12 100644 --- a/yarn-project/foundation/src/json-rpc/client/safe_json_rpc_client.ts +++ b/yarn-project/foundation/src/json-rpc/client/safe_json_rpc_client.ts @@ -44,18 +44,10 @@ export function createSafeJsonRpcClient( 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; } diff --git a/yarn-project/foundation/src/json-rpc/test/integration.test.ts b/yarn-project/foundation/src/json-rpc/test/integration.test.ts index b48d0f8d7a5..d1611d0acde 100644 --- a/yarn-project/foundation/src/json-rpc/test/integration.test.ts +++ b/yarn-project/foundation/src/json-rpc/test/integration.test.ts @@ -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); }); });