forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkFormatter.ts
More file actions
275 lines (247 loc) · 8.17 KB
/
NetworkFormatter.ts
File metadata and controls
275 lines (247 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
* */
import {isUtf8} from 'node:buffer';
import type {HTTPRequest, HTTPResponse} from '../third_party/index.js';
const BODY_CONTEXT_SIZE_LIMIT = 10000;
export interface NetworkFormatterOptions {
requestId?: number | string;
selectedInDevToolsUI?: boolean;
requestIdResolver?: (request: HTTPRequest) => number | string;
fetchData?: boolean;
requestFilePath?: string;
responseFilePath?: string;
saveFile?: (
data: Uint8Array<ArrayBufferLike>,
filename: string,
) => Promise<{filename: string}>;
}
export class NetworkFormatter {
#request: HTTPRequest;
#options: NetworkFormatterOptions;
#requestBody?: string;
#responseBody?: string;
#requestBodyFilePath?: string;
#responseBodyFilePath?: string;
private constructor(request: HTTPRequest, options: NetworkFormatterOptions) {
this.#request = request;
this.#options = options;
}
static async from(
request: HTTPRequest,
options: NetworkFormatterOptions,
): Promise<NetworkFormatter> {
const instance = new NetworkFormatter(request, options);
if (options.fetchData) {
await instance.#loadDetailedData();
}
return instance;
}
async #loadDetailedData(): Promise<void> {
// Load Request Body
if (this.#request.hasPostData()) {
let data;
try {
data =
this.#request.postData() ?? (await this.#request.fetchPostData());
} catch {
// Ignore parsing errors
}
const requestBodyNotAvailableMessage =
'<Request body not available anymore>';
if (this.#options.requestFilePath) {
if (!this.#options.saveFile) {
throw new Error('saveFile is not provided');
}
if (data) {
await this.#options.saveFile(
Buffer.from(data),
this.#options.requestFilePath,
);
this.#requestBodyFilePath = this.#options.requestFilePath;
} else {
this.#requestBody = requestBodyNotAvailableMessage;
}
} else {
if (data) {
this.#requestBody = getSizeLimitedString(
data,
BODY_CONTEXT_SIZE_LIMIT,
);
} else {
this.#requestBody = requestBodyNotAvailableMessage;
}
}
}
// Load Response Body
const response = this.#request.response();
if (response) {
const responseBodyNotAvailableMessage =
'<Response body not available anymore>';
if (this.#options.responseFilePath) {
try {
const buffer = await response.buffer();
if (!this.#options.saveFile) {
throw new Error('saveFile is not provided');
}
await this.#options.saveFile(buffer, this.#options.responseFilePath);
this.#responseBodyFilePath = this.#options.responseFilePath;
} catch {
// Flatten error handling for buffer() failure and save failure
}
if (!this.#responseBodyFilePath) {
this.#responseBody = responseBodyNotAvailableMessage;
}
} else {
this.#responseBody = await this.#getFormattedResponseBody(
response,
BODY_CONTEXT_SIZE_LIMIT,
);
}
}
}
toString(): string {
// TODO truncate the URL
return `reqid=${this.#options.requestId} ${this.#request.method()} ${this.#request.url()} ${this.#getStatusFromRequest(this.#request)}${this.#options.selectedInDevToolsUI ? ` [selected in the DevTools Network panel]` : ''}`;
}
toStringDetailed(): string {
const response: string[] = [];
response.push(`## Request ${this.#request.url()}`);
response.push(`Status: ${this.#getStatusFromRequest(this.#request)}`);
response.push(`### Request Headers`);
for (const line of this.#getFormattedHeaderValue(this.#request.headers())) {
response.push(line);
}
if (this.#requestBody) {
response.push(`### Request Body`);
response.push(this.#requestBody);
} else if (this.#requestBodyFilePath) {
response.push(`### Request Body`);
response.push(`Saved to ${this.#requestBodyFilePath}.`);
}
const httpResponse = this.#request.response();
if (httpResponse) {
response.push(`### Response Headers`);
for (const line of this.#getFormattedHeaderValue(
httpResponse.headers(),
)) {
response.push(line);
}
}
if (this.#responseBody) {
response.push(`### Response Body`);
response.push(this.#responseBody);
} else if (this.#responseBodyFilePath) {
response.push(`### Response Body`);
response.push(`Saved to ${this.#responseBodyFilePath}.`);
}
const httpFailure = this.#request.failure();
if (httpFailure) {
response.push(`### Request failed with`);
response.push(httpFailure.errorText);
}
const redirectChain = this.#request.redirectChain();
if (redirectChain.length) {
response.push(`### Redirect chain`);
let indent = 0;
for (const request of [...redirectChain].reverse()) {
const id = this.#options.requestIdResolver
? this.#options.requestIdResolver(request)
: undefined;
// We create a temporary synchronous instance just for toString
const formatter = new NetworkFormatter(request, {
requestId: id,
saveFile: this.#options.saveFile,
});
response.push(`${' '.repeat(indent)}${formatter.toString()}`);
indent++;
}
}
return response.join('\n');
}
toJSON(): object {
return {
requestId: this.#options.requestId,
method: this.#request.method(),
url: this.#request.url(),
status: this.#getStatusFromRequest(this.#request),
selectedInDevToolsUI: this.#options.selectedInDevToolsUI,
};
}
toJSONDetailed(): object {
const redirectChain = this.#request.redirectChain();
const formattedRedirectChain = [...redirectChain].reverse().map(request => {
const id = this.#options.requestIdResolver
? this.#options.requestIdResolver(request)
: undefined;
const formatter = new NetworkFormatter(request, {
requestId: id,
saveFile: this.#options.saveFile,
});
return formatter.toJSON();
});
return {
...this.toJSON(),
requestHeaders: this.#request.headers(),
requestBody: this.#requestBody,
requestBodyFilePath: this.#requestBodyFilePath,
responseHeaders: this.#request.response()?.headers(),
responseBody: this.#responseBody,
responseBodyFilePath: this.#responseBodyFilePath,
failure: this.#request.failure()?.errorText,
redirectChain: formattedRedirectChain.length
? formattedRedirectChain
: undefined,
};
}
#getStatusFromRequest(request: HTTPRequest): string {
const httpResponse = request.response();
const failure = request.failure();
let status: string;
if (httpResponse) {
const responseStatus = httpResponse.status();
status =
responseStatus >= 200 && responseStatus <= 299
? `[success - ${responseStatus}]`
: `[failed - ${responseStatus}]`;
} else if (failure) {
status = `[failed - ${failure.errorText}]`;
} else {
status = '[pending]';
}
return status;
}
#getFormattedHeaderValue(headers: Record<string, string>): string[] {
const response: string[] = [];
for (const [name, value] of Object.entries(headers)) {
response.push(`- ${name}:${value}`);
}
return response;
}
async #getFormattedResponseBody(
httpResponse: HTTPResponse,
sizeLimit = BODY_CONTEXT_SIZE_LIMIT,
): Promise<string | undefined> {
try {
const responseBuffer = await httpResponse.buffer();
if (isUtf8(responseBuffer)) {
const responseAsTest = responseBuffer.toString('utf-8');
if (responseAsTest.length === 0) {
return '<empty response>';
}
return getSizeLimitedString(responseAsTest, sizeLimit);
}
return '<binary data>';
} catch {
return '<not available anymore>';
}
}
}
function getSizeLimitedString(text: string, sizeLimit: number) {
if (text.length > sizeLimit) {
return text.substring(0, sizeLimit) + '... <truncated>';
}
return text;
}