-
Notifications
You must be signed in to change notification settings - Fork 17
/
client-proxy.js
65 lines (50 loc) · 1.77 KB
/
client-proxy.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const handler = {
// set up the proxy get handler
get(target, propKey) {
// store the original func being called
const origFunc = target[propKey];
// IMPORTANT - we only want to intercept gRPC request functions!
// Validate this is a gRPC request func by checking the object for
// a requestSerialize() function
let grpcMethod = false;
for (const k in origFunc) {
if (k === 'requestSerialize' && typeof origFunc[k] === 'function') {
grpcMethod = true;
break;
}
}
// if this doesn't look like a gRPC request func, return the original func
if (!grpcMethod) {
return function (...args) {
return origFunc.call(target, ...args);
};
}
// insert the zipkin client interceptor into the call
return function (...args) {
let message, options, callback;
if (args.length >= 3) {
message = args[0];
options = args[1];
callback = args[2];
} else {
message = args[0] || undefined;
callback = args[1] || undefined;
}
if (!options) {
options = {};
}
if (!(options.interceptors && Array.isArray(options.interceptors))) {
options.interceptors = [];
}
options.interceptors = options.interceptors.concat(target.interceptors);
return origFunc.call(target, message, options, callback);
};
},
};
module.exports = (client) => {
client.interceptors = [];
client.use = fn => {
client.interceptors.push(fn);
};
return new Proxy(client, handler);
};