forked from koorchik/node-mole-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxify.js
47 lines (44 loc) · 1.75 KB
/
proxify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function proxify(moleClient) {
const initProxy = proxifyOwnMethod(moleClient.init.bind(moleClient));
const shutdownProxy = proxifyOwnMethod(moleClient.shutdown.bind(moleClient));
const callMethodProxy = proxifyOwnMethod(moleClient.callMethod.bind(moleClient));
const notifyProxy = proxifyOwnMethod(moleClient.notify.bind(moleClient));
const pingProxy = proxifyOwnMethod(moleClient.ping.bind(moleClient));
return new Proxy(moleClient, {
get(target, methodName) {
switch (methodName) {
case 'init':
return initProxy;
case 'shutdown':
return shutdownProxy;
case 'callMethod':
return callMethodProxy;
case 'notify':
return notifyProxy;
case 'ping':
return pingProxy;
case 'options.requestTimeout':
return target.requestTimeout;
case 'options.pingTimeout':
return target.pingTimeout;
case 'then':
// without this you will not be able to return client from an async function.
// V8 will see then method and will decide that client is a promise
return;
default:
return (...params) => target.callMethod.call(target, methodName, params);
}
}
});
}
function proxifyOwnMethod(ownMethod) {
return new Proxy(ownMethod, {
get(target, methodName) {
return (...params) => target.call(null, methodName, params);
},
apply(target, _, args) {
return target.apply(null, args);
}
});
}
module.exports = proxify;