Skip to content

Commit 93b1c45

Browse files
authored
chore: Remove Proxy from json rpc client (#10554)
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.
1 parent 7441767 commit 93b1c45

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

yarn-project/foundation/src/json-rpc/client/safe_json_rpc_client.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,10 @@ export function createSafeJsonRpcClient<T extends object>(
4444
return (schema as ApiSchema)[methodName].returnType().parse(res.result);
4545
};
4646

47-
// Intercept any RPC methods with a proxy
48-
const proxy = new Proxy(
49-
{},
50-
{
51-
get: (target, method: string) => {
52-
if (['then', 'catch'].includes(method)) {
53-
return Reflect.get(target, method);
54-
}
55-
return (...params: any[]) => request(method, params);
56-
},
57-
},
58-
) as T;
47+
const proxy: any = {};
48+
for (const method of Object.keys(schema)) {
49+
proxy[method] = (...params: any[]) => request(method, params);
50+
}
5951

60-
return proxy;
52+
return proxy as T;
6153
}

yarn-project/foundation/src/json-rpc/test/integration.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ describe('JsonRpc integration', () => {
118118
await expect(() => client.fail()).rejects.toThrow('Test state failed');
119119
});
120120

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

0 commit comments

Comments
 (0)