forked from aheckmann/gleak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (155 loc) · 3.13 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Gleak - detect global var leaks.
* @api public
*/
module.exports = exports = function gleak () {
return new Gleak;
}
/**
* Version.
* @api public
*/
exports.version = '0.2.0';
/**
* Express middleware.
* @api public
*/
exports.middleware = function gleakMiddleware (stream, format) {
var g = new Gleak;
if (!format) {
switch (typeof stream) {
case 'string':
format = stream;
stream = process.stderr;
break;
case 'undefined':
format = g.format;
stream = process.stderr;
break;
default:
format = g.format;
}
}
setTimeout(print, 1000);
function print () {
g.detectNew().forEach(function (leak) {
stream.write(format.replace(/%s/, leak) + '\n');
});
}
return function gleakMiddleware (req, res, next) {
if (res._gleak) return next();
res._gleak = true;
var send = res.send;
res.send = function () {
res.send = send;
res.send.apply(res, arguments);
print();
}
next();
}
}
/**
* Gleak constructor
* @api private
*/
function Gleak () {
this.whitelist = this.whitelist.slice();
}
/**
* Whitelisted globals.
* @api public
*/
// v0.4.x
Gleak.prototype.whitelist = [
setTimeout
, setInterval
, clearTimeout
, clearInterval
, console
, Buffer
, process
, global
, GLOBAL
, root
];
// check for new globals in >= v0.5x
var version = process.version.replace(/^v/, '').split('.');
if ('0' === version[0] && version[1] > 4) {
Gleak.prototype.whitelist.push(
ArrayBuffer
, Int8Array
, Uint8Array
, Int16Array
, Uint16Array
, Int32Array
, Uint32Array
, Float32Array
, Float64Array
, DataView
, 'errno' // node >= v0.5.x hack
)
}
/**
* Default format.
* @api public
*/
Gleak.prototype.format = '\x1b[31mGleak!:\x1b[0m %s';
/**
* Detects global variable leaks.
* @api public
*/
Gleak.prototype.detect = function detect () {
var whitelist = this.whitelist
, ret = []
Object.keys(global).forEach(function (key) {
var w = whitelist.length
, bad = true
, white
while (w--) {
white = whitelist[w];
if (global[key] === white || 'string' === typeof white && key === white) {
bad = false;
break;
}
}
if (bad) ret.push(key);
});
return ret;
};
/**
* Return only new leaks since the last time `detectNew`
* was run.
* @api public
*/
Gleak.prototype.detectNew = function detectNew () {
var found = this.found || (this.found = []);
var ret = [];
this.detect().forEach(function (leak) {
if (~found.indexOf(leak)) return;
found.push(leak);
ret.push(leak);
});
return ret;
}
/**
* Prints all gleaks to stderr.
* @api public
*/
Gleak.prototype.print = function print () {
var format = this.format;
this.detect().forEach(function (leak) {
console.error(format, leak);
});
}
/**
* Add items to the whitelist disallowing duplicates.
* @api public
*/
Gleak.prototype.ignore = function ignore () {
var i = arguments.length;
while (i--) {
if (~this.whitelist.indexOf(arguments[i])) continue;
this.whitelist.push(arguments[i]);
}
return this;
}