forked from aheckmann/gleak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 15b62d4
Showing
6 changed files
with
361 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.swp | ||
*.swo | ||
*.swu | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
test: | ||
@NODE_ENV=test ./node_modules/expresso/bin/expresso \ | ||
-I ./node_modules \ | ||
test/index.js | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Gleak | ||
Global variable leak detection for Node.js | ||
|
||
var gleak = require('gleak'); | ||
|
||
gleak.detect().forEach(function (name) { | ||
console.warn('found global leak: %s', name); | ||
}); | ||
|
||
Global variable leaks in javascript can bite you when you least | ||
expect it. Do something about it now and run this module after | ||
your tests, after HTTP requests, and after you brush your teeth. | ||
|
||
## Configurable: | ||
|
||
Gleak comes configured for Node.js and will ignore built-ins by default | ||
but you can configure it however your like: | ||
|
||
var gleak = require('gleak'); | ||
gleak.whitelist.push(app, db); | ||
|
||
`gleak.whitelist` is an array that holds all globals we want to ignore. | ||
Push to it or blow it away completely with your own list. | ||
|
||
gleak.whitelist = [dnode, cluster]; | ||
|
||
If you don't want anything fancy and want to quickly dump all | ||
global leaks to your console, just call `print()`. | ||
|
||
gleak.print(); // prints "Gleak!: leakedVarName" | ||
|
||
## Expressable | ||
|
||
We might want to print leaked variables to our console after each | ||
HTTP request. This is especially helpful during development. | ||
To accomplish this we can utilize the bundled [express](http://expressjs.com) middleware: | ||
|
||
var app = express.createServer(); | ||
app.use(gleak.middleware()); | ||
|
||
What if we want to output to a different stream than stderr? | ||
|
||
app.use(gleak.middleware(stream)); | ||
|
||
How about customized logging formats? | ||
|
||
app.use(gleak.middleware('\x1b[31mLeak!\x1b[0m %s')); | ||
|
||
Combining formats and streams? | ||
|
||
app.use(gleak.middleware(stream, '\x1b[31mLeak!\x1b[0m %s')); | ||
|
||
## Installable | ||
|
||
npm install gleak | ||
|
||
### Node version | ||
Compatible with Node >=v0.4 <0.5.0 | ||
|
||
## License | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2011 [Aaron Heckmann]([email protected]) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
|
||
/** | ||
* Gleak - detect global var leaks. | ||
*/ | ||
|
||
/** | ||
* Whitelisted globals. | ||
* | ||
* @api public | ||
*/ | ||
|
||
exports.whitelist = [ | ||
setTimeout | ||
, setInterval | ||
, clearTimeout | ||
, clearInterval | ||
, console | ||
, Buffer | ||
, process | ||
, global | ||
]; | ||
|
||
/** | ||
* Default format. | ||
*/ | ||
|
||
exports.format = '\x1b[31mGleak!:\x1b[0m %s'; | ||
|
||
/** | ||
* Detects global variable leaks. | ||
* | ||
* @api public | ||
*/ | ||
|
||
exports.detect = function detect () { | ||
var whitelist = exports.whitelist | ||
, ret = [] | ||
|
||
Object.keys(global).forEach(function (key) { | ||
var w = whitelist.length | ||
, bad = true | ||
|
||
while (w--) if (global[key] === whitelist[w]) { | ||
bad = false; | ||
break; | ||
} | ||
|
||
if (bad) ret.push(key); | ||
}); | ||
|
||
return ret; | ||
}; | ||
|
||
/** | ||
* Prints all gleaks to stderr. | ||
*/ | ||
|
||
exports.print = function print () { | ||
exports.detect().forEach(function (leak) { | ||
console.error(exports.format, leak); | ||
}); | ||
} | ||
|
||
/** | ||
* Express middleware. | ||
*/ | ||
|
||
exports.middleware = function gleakMiddleware (stream, format) { | ||
if (!format) { | ||
switch (typeof stream) { | ||
case 'string': | ||
format = stream; | ||
stream = process.stderr; | ||
break; | ||
case 'undefined': | ||
format = exports.format; | ||
stream = process.stderr; | ||
break; | ||
default: | ||
format = exports.format; | ||
} | ||
} | ||
|
||
var known = []; | ||
setTimeout(print, 1000); | ||
|
||
function print () { | ||
exports.detect().forEach(function (leak) { | ||
if (~known.indexOf(leak)) return; | ||
known.push(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(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"author": "Aaron Heckmann <[email protected]>", | ||
"name": "Gleak", | ||
"description": "Node global variable leak detector", | ||
"version": "0.0.0", | ||
"repository": { | ||
"url": "" | ||
}, | ||
"main": "./index.js", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"engines": { | ||
"node": "~v0.4.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"express": "~v2.0.0" | ||
, "expresso": "v0.7.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
|
||
var assert = require('assert') | ||
var express = require('express') | ||
var gleak = require('../index') | ||
|
||
exports['default format is correct'] = function () { | ||
assert.equal('\x1b[31mGleak!:\x1b[0m %s', gleak.format); | ||
} | ||
|
||
exports['whitelist is an array'] = function () { | ||
assert.ok(Array.isArray(gleak.whitelist)); | ||
} | ||
|
||
exports['setTimeout is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(setTimeout)); | ||
}; | ||
|
||
exports['setInterval is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(setInterval)); | ||
}; | ||
exports['clearTimeout is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(clearTimeout)); | ||
}; | ||
exports['clearInterval is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(clearInterval)); | ||
}; | ||
exports['console is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(console)); | ||
}; | ||
exports['Buffer is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(Buffer)); | ||
}; | ||
exports['process is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(process)); | ||
}; | ||
exports['global is a default'] = function () { | ||
assert.ok(~gleak.whitelist.indexOf(global)); | ||
}; | ||
|
||
exports['whitelist is mutable'] = function () { | ||
var i = gleak.whitelist.push(assert); | ||
assert.ok(~gleak.whitelist.indexOf(assert)); | ||
gleak.whitelist.splice(i-1, 1); | ||
assert.ok(!~gleak.whitelist.indexOf(assert)); | ||
} | ||
|
||
exports['gleak.detect is a function'] = function () { | ||
assert.ok('function' === typeof gleak.detect); | ||
} | ||
|
||
exports['detect()'] = function () { | ||
var found = gleak.detect(); | ||
assert.ok(Array.isArray(found)); | ||
assert.ok(0 === found.length); | ||
haha = "lol" | ||
assert.ok(1 === gleak.detect().length); | ||
assert.equal("haha", gleak.detect()[0]); | ||
} | ||
|
||
exports['print()'] = function () { | ||
var write = console.error; | ||
var times = 0; | ||
haha = "heh"; | ||
console.error = function (format, item) { | ||
assert.equal(gleak.format, format); | ||
assert.equal("haha", item); | ||
++times; | ||
} | ||
gleak.print(); | ||
console.error = write; | ||
assert.equal(1, times); | ||
} | ||
|
||
exports['test middleware'] = function (beforeExit) { | ||
|
||
var called = false; | ||
var req = {}; | ||
var res = { send: function (x) { assert.equal(x, 'yes'); called = true; }}; | ||
var m = gleak.middleware(); | ||
m(req, res, function(){}); | ||
assert.equal(res._gleak, true); | ||
res.send('yes'); | ||
assert.equal(true, called); | ||
|
||
// another leak | ||
meToo = 47; | ||
|
||
// mock stream | ||
function makeStream (tests) { | ||
return { | ||
i: 0 | ||
, write: function (data) { | ||
assert.equal(tests[this.i], data); | ||
++this.i; | ||
} | ||
} | ||
} | ||
|
||
var app = express.createServer(); | ||
|
||
var sout = [ | ||
'\x1b[31mGleak!:\x1b[0m haha\n' | ||
, '\x1b[31mGleak!:\x1b[0m meToo\n' | ||
]; | ||
var stream1 = makeStream(sout); | ||
|
||
app.get('/stream', gleak.middleware(stream1), function (req, res, next) { | ||
res.send('passed a stream'); | ||
}); | ||
|
||
var both = [ | ||
'yes : haha\n' | ||
, 'yes : meToo\n' | ||
]; | ||
var stream2 = makeStream(both); | ||
|
||
app.get('/formatstream', gleak.middleware(stream2, 'yes : %s'), function (req, res, next) { | ||
res.send('passed format and stream'); | ||
}); | ||
|
||
assert.response(app, | ||
{ url: '/stream' } | ||
, { status: 200 | ||
, body: 'passed a stream' }) | ||
|
||
assert.response(app, | ||
{ url: '/formatstream' } | ||
, { status: 200 | ||
, body: 'passed format and stream' }) | ||
|
||
beforeExit(function () { | ||
assert.equal(stream1.i, 2); | ||
assert.equal(stream2.i, 2); | ||
}); | ||
} | ||
|