Skip to content

inspector: add protocol methods retrieving sent/received data #58645

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 1 commit into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/api/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ This feature is only available with the `--experimental-network-inspection` flag
Broadcasts the `Network.dataReceived` event to connected frontends, or buffers the data if
`Network.streamResourceContent` command was not invoked for the given request yet.

Also enables `Network.getResponseBody` command to retrieve the response data.

### `inspector.Network.dataSent([params])`

<!-- YAML
added: REPLACEME
-->

* `params` {Object}

This feature is only available with the `--experimental-network-inspection` flag enabled.

Enables `Network.getRequestPostData` command to retrieve the request data.

### `inspector.Network.requestWillBeSent([params])`

<!-- YAML
Expand Down
1 change: 1 addition & 0 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const Network = {
responseReceived: (params) => broadcastToFrontend('Network.responseReceived', params),
loadingFinished: (params) => broadcastToFrontend('Network.loadingFinished', params),
loadingFailed: (params) => broadcastToFrontend('Network.loadingFailed', params),
dataSent: (params) => broadcastToFrontend('Network.dataSent', params),
dataReceived: (params) => broadcastToFrontend('Network.dataReceived', params),
};

Expand Down
21 changes: 21 additions & 0 deletions lib/internal/inspector/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
} = primordials;

const { now } = require('internal/perf/utils');
const { MIMEType } = require('internal/mime');
const kInspectorRequestId = Symbol('kInspectorRequestId');

// https://chromedevtools.github.io/devtools-protocol/1-3/Network/#type-ResourceType
Expand Down Expand Up @@ -46,9 +47,29 @@ function getNextRequestId() {
return `node-network-event-${++requestId}`;
};

function sniffMimeType(contentType) {
let mimeType;
let charset;
try {
const mimeTypeObj = new MIMEType(contentType);
mimeType = mimeTypeObj.essence || '';
charset = mimeTypeObj.params.get('charset') || '';
} catch {
mimeType = '';
charset = '';
}

return {
__proto__: null,
mimeType,
charset,
};
}

module.exports = {
kInspectorRequestId,
kResourceType,
getMonotonicTime,
getNextRequestId,
sniffMimeType,
};
34 changes: 17 additions & 17 deletions lib/internal/inspector/network_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,43 @@ const {
kResourceType,
getMonotonicTime,
getNextRequestId,
sniffMimeType,
} = require('internal/inspector/network');
const dc = require('diagnostics_channel');
const { Network } = require('inspector');
const { MIMEType } = require('internal/mime');

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;
let charset;
let mimeType;
const dict = {};
for (const { 0: key, 1: value } of ObjectEntries(headers)) {
if (key.toLowerCase() === 'host') {
const lowerCasedKey = key.toLowerCase();
if (lowerCasedKey === 'host') {
host = value;
}
if (lowerCasedKey === 'content-type') {
const result = sniffMimeType(value);
charset = result.charset;
mimeType = result.mimeType;
}
if (typeof value === 'string') {
dict[key] = value;
} else if (ArrayIsArray(value)) {
if (key.toLowerCase() === 'cookie') dict[key] = value.join('; ');
if (lowerCasedKey === '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 if (lowerCasedKey === 'set-cookie') dict[key] = value.join('\n');
else dict[key] = value.join(', ');
} else {
dict[key] = String(value);
}
}
return [host, dict];
return [dict, host, charset, mimeType];
};

/**
Expand All @@ -52,14 +60,15 @@ const convertHeaderObject = (headers = {}) => {
function onClientRequestCreated({ request }) {
request[kInspectorRequestId] = getNextRequestId();

const { 0: host, 1: headers } = convertHeaderObject(request.getHeaders());
const { 0: headers, 1: host, 2: charset } = convertHeaderObject(request.getHeaders());
const url = `${request.protocol}//${host}${request.path}`;
request[kRequestUrl] = url;

Network.requestWillBeSent({
requestId: request[kInspectorRequestId],
timestamp: getMonotonicTime(),
wallTime: DateNow(),
charset,
request: {
url,
method: request.method,
Expand Down Expand Up @@ -95,16 +104,7 @@ function onClientResponseFinish({ request, response }) {
return;
}

let mimeType;
let charset;
try {
const mimeTypeObj = new MIMEType(response.headers['content-type']);
mimeType = mimeTypeObj.essence || '';
charset = mimeTypeObj.params.get('charset') || '';
} catch {
mimeType = '';
charset = '';
}
const { 0: headers, 2: charset, 3: mimeType } = convertHeaderObject(response.headers);

Network.responseReceived({
requestId: request[kInspectorRequestId],
Expand All @@ -114,7 +114,7 @@ function onClientResponseFinish({ request, response }) {
url: request[kRequestUrl],
status: response.statusCode,
statusText: response.statusMessage ?? '',
headers: convertHeaderObject(response.headers)[1],
headers,
mimeType,
charset,
},
Expand Down
37 changes: 19 additions & 18 deletions lib/internal/inspector/network_undici.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ArrayPrototypeFindIndex,
DateNow,
} = primordials;

Expand All @@ -10,10 +9,10 @@ const {
kResourceType,
getMonotonicTime,
getNextRequestId,
sniffMimeType,
} = require('internal/inspector/network');
const dc = require('diagnostics_channel');
const { Network } = require('inspector');
const { MIMEType } = require('internal/mime');

// Convert an undici request headers array to a plain object (Map<string, string>)
function requestHeadersArrayToDictionary(headers) {
Expand All @@ -29,21 +28,30 @@ function requestHeadersArrayToDictionary(headers) {
// Convert an undici response headers array to a plain object (Map<string, string>)
function responseHeadersArrayToDictionary(headers) {
const dict = {};
let charset;
let mimeType;
for (let idx = 0; idx < headers.length; idx += 2) {
const key = `${headers[idx]}`;
const lowerCasedKey = key.toLowerCase();
const value = `${headers[idx + 1]}`;
const prevValue = dict[key];

if (lowerCasedKey === 'content-type') {
const result = sniffMimeType(value);
charset = result.charset;
mimeType = result.mimeType;
}

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}`;
if (lowerCasedKey === 'set-cookie') dict[key] = `${prevValue}\n${value}`;
else dict[key] = `${prevValue}, ${value}`;
} else {
dict[key] = value;
}
}
return dict;
return [dict, charset, mimeType];
};

/**
Expand All @@ -54,10 +62,15 @@ function responseHeadersArrayToDictionary(headers) {
function onClientRequestStart({ request }) {
const url = `${request.origin}${request.path}`;
request[kInspectorRequestId] = getNextRequestId();

const headers = requestHeadersArrayToDictionary(request.headers);
const { charset } = sniffMimeType(headers);

Network.requestWillBeSent({
requestId: request[kInspectorRequestId],
timestamp: getMonotonicTime(),
wallTime: DateNow(),
charset,
request: {
url,
method: request.method,
Expand Down Expand Up @@ -94,19 +107,7 @@ function onClientResponseHeaders({ request, response }) {
return;
}

let mimeType;
let charset;
try {
const contentTypeKeyIndex =
ArrayPrototypeFindIndex(response.headers, (header) => header.toString().toLowerCase() === 'content-type');
const contentType = contentTypeKeyIndex !== -1 ? response.headers[contentTypeKeyIndex + 1].toString() : '';
const mimeTypeObj = new MIMEType(contentType);
mimeType = mimeTypeObj.essence || '';
charset = mimeTypeObj.params.get('charset') || '';
} catch {
mimeType = '';
charset = '';
}
const { 0: headers, 1: charset, 2: mimeType } = responseHeadersArrayToDictionary(response.headers);

const url = `${request.origin}${request.path}`;
Network.responseReceived({
Expand All @@ -118,7 +119,7 @@ function onClientResponseHeaders({ request, response }) {
url,
status: response.statusCode,
statusText: response.statusText,
headers: responseHeadersArrayToDictionary(response.headers),
headers,
mimeType,
charset,
},
Expand Down
Loading
Loading