-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-hooks.js
49 lines (38 loc) · 1.07 KB
/
install-hooks.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
var getStackFrames = require('./get-stack-frames'),
Module = require('module'),
loader = Module._load;
var hooked;
module.exports = exports = function installHooks() {
if (hooked) return;
hooked = true;
Module._load = function(request, parent) {
var mod = loader.call(this, request, parent);
if (request == 'request') return hookedRequest(mod);
return mod;
};
function hookedRequest(realRequest) {
function request() {
var req = realRequest.apply(this, arguments),
stack = getStackFrames(__filename);
req.once('request', function(req) {
req.__init = stack;
});
return req;
}
['get','head','options','post','put','patch','del','delete'].forEach(function(method) {
request[method] = function() {
var req = realRequest[method].apply(realRequest, arguments),
stack = getStackFrames(__filename);
req.once('request', function(req) {
req.__init = stack;
});
return req;
}
});
// copy the rest of the stuff
for (var k in realRequest) {
if (!request[k]) request[k] = realRequest[k];
}
return request;
}
}