-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
146 lines (117 loc) · 3.63 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
'use strict';
var utils = require('./utils');
var cluster = require('cluster');
module.exports = function (allowWrapper) {
var currentProcess = (cluster.isWorker) ? cluster.worker.process : process;
if (allowWrapper) {
global.setImmediate = wrapCallbackFirst(global, 'setImmediate');
process.nextTick = wrapCallbackFirst(currentProcess, 'nextTick');
global.activeSetImmediateAndNextTick = { counter: 0 };
}
return {
dump: function () {
var handles = {};
var requests = {};
var setImmediates = [];
var nextTicks = [];
currentProcess._getActiveHandles().forEach(function (h) {
if (!h) {
return;
}
// skip stdio and pipe between worker and master
if (isStdIO(h) || isWorkerPipe(h)) {
return;
}
var obj = {
type: h.constructor.name
};
if (obj.type === 'Server') {
utils.extractServer(obj, h);
} else if (obj.type === 'Socket') {
utils.extractSocket(obj, h);
} else if (obj.type === 'Timer') {
utils.extractTimer(obj, h);
} else if (obj.type === 'ChildProcess') {
utils.extractChildProcess(obj, h);
} else if (obj.type === 'MessagePort') {
utils.extractMessagePort(obj, h);
}
// create array if this is the first item of this type
if (!handles[obj.type]) {
handles[obj.type] = [];
}
handles[obj.type].push(obj);
});
currentProcess._getActiveRequests().forEach(function (r) {
if (!r) {
return;
}
// skip stdio
if (isStdIO(r)) {
return;
}
var obj = {
type: r.constructor.name
};
if (obj.type === 'TCPConnectWrap') {
utils.extractTCPWrap(obj, r);
}
// create array if this is the first item of this type
if (!requests[obj.type]) {
requests[obj.type] = [];
}
requests[obj.type].push(obj);
});
for (var key in global.activeSetImmediateAndNextTick) {
var item = global.activeSetImmediateAndNextTick[key];
if (item.type === 'setImmediate') {
setImmediates.push(item);
} else if (item.type === 'nextTick') {
nextTicks.push(item);
}
}
return {
handles: handles,
requests: requests,
setImmediates: setImmediates,
nextTicks: nextTicks
};
}
};
};
function isStdIO (obj) {
if ((obj.constructor.name === 'WriteStream' || obj.constructor.name === 'WriteWrap') && (obj._isStdio || (obj.handle && obj.handle.owner && obj.handle.owner._isStdio))) {
return true;
}
return false;
}
function isWorkerPipe (obj) {
if (obj.constructor.name === 'Pipe' && process.channel === obj) {
return true;
}
}
function wrapCallbackFirst (mod, name) {
var orig = mod[name];
return function () {
// clone arguments so we can inject our own callback
var args = [];
for (var n = 0; n < arguments.length; n++) {
args[n] = arguments[n];
}
// inject our own callback
var userCallback = args[0];
global.activeSetImmediateAndNextTick[global.activeSetImmediateAndNextTick.counter] = {
type: name,
name: userCallback.name || 'anonymous'
};
(function (index) {
args[0] = function () {
delete global.activeSetImmediateAndNextTick[index];
// call the original callback
return userCallback.apply(this, arguments);
};
})(global.activeSetImmediateAndNextTick.counter);
global.activeSetImmediateAndNextTick.counter++;
return orig.apply(mod, args);
};
}