forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspector_network_tracking.js
More file actions
71 lines (63 loc) · 1.61 KB
/
inspector_network_tracking.js
File metadata and controls
71 lines (63 loc) · 1.61 KB
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
'use strict';
const {
DateNow,
} = primordials;
let dc;
let Network;
let requestId = 0;
const getNextRequestId = () => `node-network-event-${++requestId}`;
function onClientRequestStart({ request }) {
const url = `${request.protocol}//${request.host}${request.path}`;
const wallTime = DateNow();
const timestamp = wallTime / 1000;
request._inspectorRequestId = getNextRequestId();
Network.requestWillBeSent({
requestId: request._inspectorRequestId,
timestamp,
wallTime,
request: {
url,
method: request.method,
headers: request.getHeaders(),
},
});
}
function onClientResponseFinish({ request, response }) {
if (typeof request._inspectorRequestId !== 'string') {
return;
}
const url = `${request.protocol}//${request.host}${request.path}`;
const timestamp = DateNow() / 1000;
Network.responseReceived({
requestId: request._inspectorRequestId,
timestamp,
type: 'Other',
response: {
url,
status: response.statusCode,
headers: response.headers,
},
});
Network.loadingFinished({
requestId: request._inspectorRequestId,
timestamp,
});
}
function enable() {
if (!dc) {
dc = require('diagnostics_channel');
}
if (!Network) {
Network = require('inspector').Network;
}
dc.subscribe('http.client.request.start', onClientRequestStart);
dc.subscribe('http.client.response.finish', onClientResponseFinish);
}
function disable() {
dc.unsubscribe('http.client.request.start', onClientRequestStart);
dc.unsubscribe('http.client.response.finish', onClientResponseFinish);
}
module.exports = {
enable,
disable,
};