|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ArrayIsArray, |
| 5 | + DateNow, |
| 6 | + ObjectEntries, |
| 7 | + String, |
| 8 | + Symbol, |
| 9 | +} = primordials; |
| 10 | + |
| 11 | +const { |
| 12 | + kInspectorRequestId, |
| 13 | + getMonotonicTime, |
| 14 | + getNextRequestId, |
| 15 | +} = require('internal/inspector/network'); |
| 16 | +const dc = require('diagnostics_channel'); |
| 17 | +const { Network } = require('inspector'); |
| 18 | + |
| 19 | +const kResourceType = 'Other'; |
| 20 | +const kRequestUrl = Symbol('kRequestUrl'); |
| 21 | + |
| 22 | +// Convert a Headers object (Map<string, number | string | string[]>) to a plain object (Map<string, string>) |
| 23 | +const convertHeaderObject = (headers = {}) => { |
| 24 | + // The 'host' header that contains the host and port of the URL. |
| 25 | + let host; |
| 26 | + const dict = {}; |
| 27 | + for (const { 0: key, 1: value } of ObjectEntries(headers)) { |
| 28 | + if (key.toLowerCase() === 'host') { |
| 29 | + host = value; |
| 30 | + } |
| 31 | + if (typeof value === 'string') { |
| 32 | + dict[key] = value; |
| 33 | + } else if (ArrayIsArray(value)) { |
| 34 | + if (key.toLowerCase() === 'cookie') dict[key] = value.join('; '); |
| 35 | + // ChromeDevTools frontend treats 'set-cookie' as a special case |
| 36 | + // https://github.com/ChromeDevTools/devtools-frontend/blob/4275917f84266ef40613db3c1784a25f902ea74e/front_end/core/sdk/NetworkRequest.ts#L1368 |
| 37 | + else if (key.toLowerCase() === 'set-cookie') dict[key] = value.join('\n'); |
| 38 | + else dict[key] = value.join(', '); |
| 39 | + } else { |
| 40 | + dict[key] = String(value); |
| 41 | + } |
| 42 | + } |
| 43 | + return [host, dict]; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * When a client request starts, emit Network.requestWillBeSent event. |
| 48 | + * https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-requestWillBeSent |
| 49 | + * @param {{ request: import('http').ClientRequest }} event |
| 50 | + */ |
| 51 | +function onClientRequestStart({ request }) { |
| 52 | + request[kInspectorRequestId] = getNextRequestId(); |
| 53 | + |
| 54 | + const { 0: host, 1: headers } = convertHeaderObject(request.getHeaders()); |
| 55 | + const url = `${request.protocol}//${host}${request.path}`; |
| 56 | + request[kRequestUrl] = url; |
| 57 | + |
| 58 | + Network.requestWillBeSent({ |
| 59 | + requestId: request[kInspectorRequestId], |
| 60 | + timestamp: getMonotonicTime(), |
| 61 | + wallTime: DateNow(), |
| 62 | + request: { |
| 63 | + url, |
| 64 | + method: request.method, |
| 65 | + headers, |
| 66 | + }, |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * When a client request errors, emit Network.loadingFailed event. |
| 72 | + * https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-loadingFailed |
| 73 | + * @param {{ request: import('http').ClientRequest, error: any }} event |
| 74 | + */ |
| 75 | +function onClientRequestError({ request, error }) { |
| 76 | + if (typeof request[kInspectorRequestId] !== 'string') { |
| 77 | + return; |
| 78 | + } |
| 79 | + Network.loadingFailed({ |
| 80 | + requestId: request[kInspectorRequestId], |
| 81 | + timestamp: getMonotonicTime(), |
| 82 | + type: kResourceType, |
| 83 | + errorText: error.message, |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * When response headers are received, emit Network.responseReceived event. |
| 89 | + * https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-responseReceived |
| 90 | + * @param {{ request: import('http').ClientRequest, error: any }} event |
| 91 | + */ |
| 92 | +function onClientResponseFinish({ request, response }) { |
| 93 | + if (typeof request[kInspectorRequestId] !== 'string') { |
| 94 | + return; |
| 95 | + } |
| 96 | + Network.responseReceived({ |
| 97 | + requestId: request[kInspectorRequestId], |
| 98 | + timestamp: getMonotonicTime(), |
| 99 | + type: kResourceType, |
| 100 | + response: { |
| 101 | + url: request[kRequestUrl], |
| 102 | + status: response.statusCode, |
| 103 | + statusText: response.statusMessage ?? '', |
| 104 | + headers: convertHeaderObject(response.headers)[1], |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + // Wait until the response body is consumed by user code. |
| 109 | + response.once('end', () => { |
| 110 | + Network.loadingFinished({ |
| 111 | + requestId: request[kInspectorRequestId], |
| 112 | + timestamp: getMonotonicTime(), |
| 113 | + }); |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +function enable() { |
| 118 | + dc.subscribe('http.client.request.start', onClientRequestStart); |
| 119 | + dc.subscribe('http.client.request.error', onClientRequestError); |
| 120 | + dc.subscribe('http.client.response.finish', onClientResponseFinish); |
| 121 | +} |
| 122 | + |
| 123 | +function disable() { |
| 124 | + dc.unsubscribe('http.client.request.start', onClientRequestStart); |
| 125 | + dc.unsubscribe('http.client.request.error', onClientRequestError); |
| 126 | + dc.unsubscribe('http.client.response.finish', onClientResponseFinish); |
| 127 | +} |
| 128 | + |
| 129 | +module.exports = { |
| 130 | + enable, |
| 131 | + disable, |
| 132 | +}; |
0 commit comments