|
3 | 3 | const {
|
4 | 4 | ArrayPrototypeForEach,
|
5 | 5 | Date,
|
| 6 | + DateNow, |
6 | 7 | DatePrototypeGetDate,
|
7 | 8 | DatePrototypeGetFullYear,
|
8 | 9 | DatePrototypeGetHours,
|
@@ -98,6 +99,7 @@ function prepareExecution(options) {
|
98 | 99 | const mainEntry = patchProcessObject(expandArgv1);
|
99 | 100 | setupTraceCategoryState();
|
100 | 101 | setupInspectorHooks();
|
| 102 | + setupNetworkInspection(); |
101 | 103 | setupNavigator();
|
102 | 104 | setupWarningHandler();
|
103 | 105 | setupWebStorage();
|
@@ -438,6 +440,51 @@ function setupInspectorHooks() {
|
438 | 440 | }
|
439 | 441 | }
|
440 | 442 |
|
| 443 | +function setupNetworkInspection() { |
| 444 | + if (!internalBinding('config').hasInspector) { |
| 445 | + return; |
| 446 | + } |
| 447 | + const dc = require('diagnostics_channel'); |
| 448 | + const { |
| 449 | + dataReceived, |
| 450 | + loadingFinished, |
| 451 | + requestWillBeSent, |
| 452 | + responseReceived, |
| 453 | + } = internalBinding('inspector'); |
| 454 | + |
| 455 | + let requestId = 0; |
| 456 | + const getNextRequestId = () => `node-network-event-${++requestId}`; |
| 457 | + |
| 458 | + dc.subscribe('http.client.request.start', ({ request }) => { |
| 459 | + const url = `${request.protocol}//${request.host}${request.path}`; |
| 460 | + const wallTime = DateNow(); |
| 461 | + const timestamp = wallTime / 1000; |
| 462 | + request._inspectorRequestId = getNextRequestId(); |
| 463 | + requestWillBeSent(request._inspectorRequestId, url, request.method, timestamp, wallTime); |
| 464 | + }); |
| 465 | + dc.subscribe('http.client.response.finish', ({ request, response }) => { |
| 466 | + responseReceived(request._inspectorRequestId, DateNow() / 1000); |
| 467 | + let responseString = ''; |
| 468 | + const onData = (chunk) => { |
| 469 | + dataReceived(request._inspectorRequestId, DateNow() / 1000, chunk.length); |
| 470 | + responseString += chunk.toString(); |
| 471 | + }; |
| 472 | + response.on('data', onData); |
| 473 | + response.on('end', () => { |
| 474 | + loadingFinished(request._inspectorRequestId, responseString, DateNow() / 1000, responseString.length); |
| 475 | + response.removeListener('data', onData); |
| 476 | + }); |
| 477 | + }); |
| 478 | + |
| 479 | + dc.subscribe('undici:request:create', ({ request }) => { |
| 480 | + const url = `${request.origin}${request.path}`; |
| 481 | + const wallTime = DateNow(); |
| 482 | + const timestamp = wallTime / 1000; |
| 483 | + request._inspectorRequestId = getNextRequestId(); |
| 484 | + requestWillBeSent(request._inspectorRequestId, url, request.method, timestamp, wallTime); |
| 485 | + }); |
| 486 | +} |
| 487 | + |
441 | 488 | // In general deprecations are initialized wherever the APIs are implemented,
|
442 | 489 | // this is used to deprecate APIs implemented in C++ where the deprecation
|
443 | 490 | // utilities are not easily accessible.
|
|
0 commit comments