-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshim.js
48 lines (41 loc) · 1.43 KB
/
shim.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
'use strict';
var shimmer = require('shimmer');
function slice(args) {
/**
* Usefully nerfed version of slice for use in instrumentation. Way faster
* than using [].slice.call, and maybe putting it in here (instead of the
* same module context where it will be used) will make it faster by
* defeating inlining.
*
* http://jsperf.com/array-slice-call-arguments-2
*
* for untrustworthy benchmark numbers. Only useful for copying whole
* arrays, and really only meant to be used with the arguments arraylike.
*
* Also putting this comment inside the function in an effort to defeat
* inlining.
*/
var length = args.length, array = [], i;
for (i = 0; i < length; i++) array[i] = args[i];
return array;
}
module.exports = function patchRedis(ns) {
var redis = require('redis');
var proto = redis && redis.RedisClient && redis.RedisClient.prototype;
shimmer.wrap(proto, 'send_command', function (send_command) {
return function wrapped() {
var args = slice(arguments);
var last = args.length - 1;
var callback = args[last];
var tail = callback;
if (typeof callback === 'function') {
args[last] = ns.bind(callback);
}
else if (Array.isArray(tail) && typeof tail[tail.length - 1] === 'function') {
last = tail.length - 1;
tail[last] = ns.bind(tail[last]);
}
return send_command.apply(this, args);
};
});
};