-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inspector: report loadingFinished until the response data is consumed
The `Network.loadingFinished` should be deferred until the response is complete and the data is fully consumed. Also, report correct request url with the specified port by retrieving the host from the request headers.
- Loading branch information
1 parent
c94a9db
commit 8980bb1
Showing
5 changed files
with
408 additions
and
299 deletions.
There are no files selected for viewing
This file contains 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,31 @@ | ||
'use strict'; | ||
|
||
const { | ||
NumberMAX_SAFE_INTEGER, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { now } = require('internal/perf/utils'); | ||
const kInspectorRequestId = Symbol('kInspectorRequestId'); | ||
|
||
/** | ||
* Return a monotonically increasing time in seconds since an arbitrary point in the past. | ||
* @returns {number} | ||
*/ | ||
function getMonotonicTime() { | ||
return now() / 1000; | ||
} | ||
|
||
let requestId = 0; | ||
function getNextRequestId() { | ||
if (requestId === NumberMAX_SAFE_INTEGER) { | ||
requestId = 0; | ||
} | ||
return `node-network-event-${++requestId}`; | ||
}; | ||
|
||
module.exports = { | ||
kInspectorRequestId, | ||
getMonotonicTime, | ||
getNextRequestId, | ||
}; |
This file contains 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,132 @@ | ||
'use strict'; | ||
|
||
const { | ||
ArrayIsArray, | ||
DateNow, | ||
ObjectEntries, | ||
String, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { | ||
kInspectorRequestId, | ||
getMonotonicTime, | ||
getNextRequestId, | ||
} = require('internal/inspector/network'); | ||
const dc = require('diagnostics_channel'); | ||
const { Network } = require('inspector'); | ||
|
||
const kResourceType = 'Other'; | ||
const kRequestUrl = Symbol('kRequestUrl'); | ||
|
||
// Convert a Headers object (Map<string, number | string | string[]>) to a plain object (Map<string, string>) | ||
const convertHeaderObject = (headers = {}) => { | ||
// The 'host' header that contains the host and port of the URL. | ||
let host; | ||
const dict = {}; | ||
for (const { 0: key, 1: value } of ObjectEntries(headers)) { | ||
if (key.toLowerCase() === 'host') { | ||
host = value; | ||
} | ||
if (typeof value === 'string') { | ||
dict[key] = value; | ||
} else if (ArrayIsArray(value)) { | ||
if (key.toLowerCase() === 'cookie') dict[key] = value.join('; '); | ||
// ChromeDevTools frontend treats 'set-cookie' as a special case | ||
// https://github.com/ChromeDevTools/devtools-frontend/blob/4275917f84266ef40613db3c1784a25f902ea74e/front_end/core/sdk/NetworkRequest.ts#L1368 | ||
else if (key.toLowerCase() === 'set-cookie') dict[key] = value.join('\n'); | ||
else dict[key] = value.join(', '); | ||
} else { | ||
dict[key] = String(value); | ||
} | ||
} | ||
return [host, dict]; | ||
}; | ||
|
||
/** | ||
* When a client request starts, emit Network.requestWillBeSent event. | ||
* https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-requestWillBeSent | ||
* @param {{ request: import('http').ClientRequest }} event | ||
*/ | ||
function onClientRequestStart({ request }) { | ||
request[kInspectorRequestId] = getNextRequestId(); | ||
|
||
const { 0: host, 1: headers } = convertHeaderObject(request.getHeaders()); | ||
const url = `${request.protocol}//${host}${request.path}`; | ||
request[kRequestUrl] = url; | ||
|
||
Network.requestWillBeSent({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
wallTime: DateNow(), | ||
request: { | ||
url, | ||
method: request.method, | ||
headers, | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* When a client request errors, emit Network.loadingFailed event. | ||
* https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-loadingFailed | ||
* @param {{ request: import('http').ClientRequest, error: any }} event | ||
*/ | ||
function onClientRequestError({ request, error }) { | ||
if (typeof request[kInspectorRequestId] !== 'string') { | ||
return; | ||
} | ||
Network.loadingFailed({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
type: kResourceType, | ||
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: import('http').ClientRequest, error: any }} event | ||
*/ | ||
function onClientResponseFinish({ request, response }) { | ||
if (typeof request[kInspectorRequestId] !== 'string') { | ||
return; | ||
} | ||
Network.responseReceived({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
type: kResourceType, | ||
response: { | ||
url: request[kRequestUrl], | ||
status: response.statusCode, | ||
statusText: response.statusMessage ?? '', | ||
headers: convertHeaderObject(response.headers)[1], | ||
}, | ||
}); | ||
|
||
// Wait until the response body is consumed by user code. | ||
response.once('end', () => { | ||
Network.loadingFinished({ | ||
requestId: request[kInspectorRequestId], | ||
timestamp: getMonotonicTime(), | ||
}); | ||
}); | ||
} | ||
|
||
function enable() { | ||
dc.subscribe('http.client.request.start', onClientRequestStart); | ||
dc.subscribe('http.client.request.error', onClientRequestError); | ||
dc.subscribe('http.client.response.finish', onClientResponseFinish); | ||
} | ||
|
||
function disable() { | ||
dc.unsubscribe('http.client.request.start', onClientRequestStart); | ||
dc.unsubscribe('http.client.request.error', onClientRequestError); | ||
dc.unsubscribe('http.client.response.finish', onClientResponseFinish); | ||
} | ||
|
||
module.exports = { | ||
enable, | ||
disable, | ||
}; |
This file contains 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.