-
-
Notifications
You must be signed in to change notification settings - Fork 32k
inspector: add undici http tracking support #56488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
'use strict'; | ||
|
||
const { | ||
DateNow, | ||
} = primordials; | ||
|
||
const { | ||
kInspectorRequestId, | ||
kResourceType, | ||
getMonotonicTime, | ||
getNextRequestId, | ||
} = require('internal/inspector/network'); | ||
const dc = require('diagnostics_channel'); | ||
const { Network } = require('inspector'); | ||
|
||
// Convert an undici request headers array to a plain object (Map<string, string>) | ||
function requestHeadersArrayToDictionary(headers) { | ||
const dict = {}; | ||
for (let idx = 0; idx < headers.length; idx += 2) { | ||
const key = `${headers[idx]}`; | ||
const value = `${headers[idx + 1]}`; | ||
dict[key] = value; | ||
} | ||
return dict; | ||
}; | ||
|
||
// Convert an undici response headers array to a plain object (Map<string, string>) | ||
function responseHeadersArrayToDictionary(headers) { | ||
const dict = {}; | ||
for (let idx = 0; idx < headers.length; idx += 2) { | ||
const key = `${headers[idx]}`; | ||
const value = `${headers[idx + 1]}`; | ||
const prevValue = dict[key]; | ||
|
||
if (typeof prevValue === 'string') { | ||
// ChromeDevTools frontend treats 'set-cookie' as a special case | ||
// https://github.com/ChromeDevTools/devtools-frontend/blob/4275917f84266ef40613db3c1784a25f902ea74e/front_end/core/sdk/NetworkRequest.ts#L1368 | ||
if (key.toLowerCase() === 'set-cookie') dict[key] = `${prevValue}\n${value}`; | ||
else dict[key] = `${prevValue}, ${value}`; | ||
} else { | ||
dict[key] = value; | ||
} | ||
} | ||
return dict; | ||
}; | ||
|
||
/** | ||
* When a client request starts, emit Network.requestWillBeSent event. | ||
* https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-requestWillBeSent | ||
* @param {{ request: undici.Request }} event | ||
*/ | ||
function onClientRequestStart({ request }) { | ||
const url = `${request.origin}${request.path}`; | ||
request[kInspectorRequestId] = getNextRequestId(); | ||
Network.requestWillBeSent({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
wallTime: DateNow(), | ||
request: { | ||
url, | ||
method: request.method, | ||
headers: requestHeadersArrayToDictionary(request.headers), | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* When a client request errors, emit Network.loadingFailed event. | ||
* https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-loadingFailed | ||
* @param {{ request: undici.Request, error: any }} event | ||
*/ | ||
function onClientRequestError({ request, error }) { | ||
if (typeof request[kInspectorRequestId] !== 'string') { | ||
return; | ||
} | ||
Network.loadingFailed({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
// TODO(legendecas): distinguish between `undici.request` and `undici.fetch`. | ||
type: kResourceType.Fetch, | ||
errorText: error.message, | ||
}); | ||
} | ||
|
||
/** | ||
* When response headers are received, emit Network.responseReceived event. | ||
* https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-responseReceived | ||
* @param {{ request: undici.Request, response: undici.Response }} event | ||
*/ | ||
function onClientResponseHeaders({ request, response }) { | ||
if (typeof request[kInspectorRequestId] !== 'string') { | ||
return; | ||
} | ||
const url = `${request.origin}${request.path}`; | ||
Network.responseReceived({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
// TODO(legendecas): distinguish between `undici.request` and `undici.fetch`. | ||
type: kResourceType.Fetch, | ||
response: { | ||
url, | ||
status: response.statusCode, | ||
statusText: response.statusText, | ||
headers: responseHeadersArrayToDictionary(response.headers), | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* When a response is completed, emit Network.loadingFinished event. | ||
* https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-loadingFinished | ||
* @param {{ request: undici.Request, response: undici.Response }} event | ||
*/ | ||
function onClientResponseFinish({ request }) { | ||
if (typeof request[kInspectorRequestId] !== 'string') { | ||
return; | ||
} | ||
Network.loadingFinished({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
}); | ||
} | ||
|
||
function enable() { | ||
dc.subscribe('undici:request:create', onClientRequestStart); | ||
dc.subscribe('undici:request:error', onClientRequestError); | ||
dc.subscribe('undici:request:headers', onClientResponseHeaders); | ||
dc.subscribe('undici:request:trailers', onClientResponseFinish); | ||
} | ||
|
||
function disable() { | ||
dc.subscribe('undici:request:create', onClientRequestStart); | ||
dc.subscribe('undici:request:error', onClientRequestError); | ||
dc.subscribe('undici:request:headers', onClientResponseHeaders); | ||
dc.subscribe('undici:request:trailers', onClientResponseFinish); | ||
} | ||
|
||
module.exports = { | ||
enable, | ||
disable, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.