-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.js
105 lines (86 loc) · 2.79 KB
/
utils.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
var Utils = {
extractServer: function (result, server) {
try {
var a = server.address();
} catch (e) {
return;
}
if (a) {
result.address = a.address;
result.port = a.port;
}
result.listeners = Utils.extractListeners(server, 'connection');
},
extractSocket: function (result, socket) {
if (socket.localAddress) {
result.localAddress = socket.localAddress;
result.localPort = socket.localPort;
result.remoteAddress = socket.remoteAddress;
result.remotePort = socket.remotePort;
result.remoteFamily = socket.remoteFamily;
} else if (socket._handle && socket._handle.fd !== null) {
result.fd = socket._handle.fd;
} else {
result.info = 'unknown socket';
}
if (socket._httpMessage) {
result.method = socket._httpMessage.method;
result.path = socket._httpMessage.path;
result.headers = "function" === typeof socket._httpMessage.getHeaders ? socket._httpMessage.getHeaders() : socket._httpMessage._headers;
}
result.listeners = Utils.extractListeners(socket, 'connect');
},
extractListeners: function (obj, listenerName) {
var listerners = [];
if (!obj.listeners || typeof obj.listeners !== 'function') {
return listerners;
}
var connectListeners = obj.listeners(listenerName);
if (connectListeners && connectListeners.length) {
connectListeners.forEach(function (fn) {
listerners.push({
name: fn.name || '(anonymous)'
});
});
}
return listerners;
},
extractTimer: function (result, timer) {
var type = 'setTimeout';
var entryPoint = null;
// node >= 6
if (timer._list && timer._list._idleNext) {
entryPoint = timer._list._idleNext;
} else if (timer._idleNext) { // backward compatibilty with node 4
entryPoint = timer._idleNext;
}
if (!entryPoint) {
return;
}
if (entryPoint._repeat !== null) {
type = 'setInterval';
}
result.startAfter = entryPoint._idleStart;
result.name = entryPoint._onTimeout &&
entryPoint._onTimeout.name !== '' ? entryPoint._onTimeout.name : 'anonymous';
result.type = type;
result.msecs = timer._list ? timer._list.msecs : timer.msecs;
},
extractChildProcess: function (result, child) {
result.args = child.spawnargs;
result.spawnfile = child.spawnfile;
result.pid = child.pid;
result.connected = child.connected;
result.killed = child.killed;
},
extractMessagePort: function (result, messagePort) {
result.listeners = Object.keys(messagePort._events);
},
extractTCPWrap: function (result, tcp) {
result.address = tcp.address;
result.port = tcp.port;
result.localAddress = tcp.localAddress;
result.localPort = tcp.localPort;
}
};
module.exports = Utils;