Skip to content

Commit 536f25a

Browse files
committed
Don't apply body size limit to individually exported exchanges
1 parent b603149 commit 536f25a

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/components/view/http/http-export-card.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ const ExportSnippetEditor = observer((p: {
132132
exportOption: SnippetOption
133133
}) => {
134134
const { target, client, link, description } = p.exportOption;
135-
const harRequest = generateHarRequest(p.exchange.request);
135+
const harRequest = generateHarRequest(p.exchange.request, false, {
136+
bodySizeLimit: Infinity
137+
});
136138
const harSnippetBase = simplifyHarForSnippetExport(harRequest);
137139

138140
let snippet: string;
@@ -182,7 +184,9 @@ const ExportSnippetEditor = observer((p: {
182184

183185
const exportHar = async (exchange: HttpExchange) => {
184186
const harContent = JSON.stringify(
185-
await generateHar([exchange])
187+
await generateHar([exchange], {
188+
bodySizeLimit: Infinity
189+
})
186190
);
187191
const filename = `${
188192
exchange.request.method

src/model/http/har.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export interface Har extends HarFormat.Har {
2929
log: HarLog;
3030
}
3131

32+
export interface HarGenerationOptions {
33+
bodySizeLimit: number;
34+
}
35+
3236
interface HarLog extends HarFormat.Log {
3337
// Custom field to expose failed TLS connections
3438
_tlsErrors: HarTlsErrorEntry[];
@@ -66,15 +70,18 @@ export type HarTlsErrorEntry = {
6670
clientPort: number;
6771
}
6872

69-
export async function generateHar(events: CollectedEvent[]): Promise<Har> {
73+
export async function generateHar(
74+
events: CollectedEvent[],
75+
options: HarGenerationOptions = { bodySizeLimit: HAR_BODY_SIZE_LIMIT }
76+
): Promise<Har> {
7077
const [exchanges, otherEvents] = _.partition(events, e => e.isHttp()) as [
7178
HttpExchange[], CollectedEvent[]
7279
];
7380

7481
const errors = otherEvents.filter(e => e.isTlsFailure()) as FailedTlsConnection[];
7582

7683
const sourcePages = getSourcesAsHarPages(exchanges);
77-
const entries = await Promise.all(exchanges.map(generateHarEntry));
84+
const entries = await Promise.all(exchanges.map(e => generateHarEntry(e, options)));
7885
const errorEntries = errors.map(generateHarTlsError);
7986

8087
return {
@@ -110,21 +117,26 @@ function asHtkHeaders(headers: HarFormat.Header[]) {
110117

111118
export function generateHarRequest(
112119
request: HtkRequest,
113-
waitForDecoding?: false
120+
waitForDecoding: false,
121+
options: HarGenerationOptions
114122
): ExtendedHarRequest;
115123
export function generateHarRequest(
116124
request: HtkRequest,
117-
waitForDecoding: true
125+
waitForDecoding: true,
126+
options: HarGenerationOptions
118127
): ExtendedHarRequest | ObservablePromise<ExtendedHarRequest>;
119128
export function generateHarRequest(
120129
request: HtkRequest,
121-
waitForDecoding = false
130+
waitForDecoding: boolean,
131+
options: HarGenerationOptions
122132
): ExtendedHarRequest | ObservablePromise<ExtendedHarRequest> {
123133
if (waitForDecoding && (
124134
!request.body.decodedPromise.state ||
125135
request.body.decodedPromise.state === 'pending'
126136
)) {
127-
return request.body.decodedPromise.then(() => generateHarRequest(request));
137+
return request.body.decodedPromise.then(() =>
138+
generateHarRequest(request, false, options)
139+
);
128140
}
129141

130142
const requestEntry: ExtendedHarRequest = {
@@ -144,9 +156,11 @@ export function generateHarRequest(
144156
};
145157

146158
if (request.body.decoded) {
147-
if (request.body.decoded.byteLength > HAR_BODY_SIZE_LIMIT) {
159+
if (request.body.decoded.byteLength > options.bodySizeLimit) {
148160
requestEntry._requestBodyStatus = 'discarded:too-large';
149-
requestEntry.comment = `Body discarded during HAR generation: longer than limit of ${HAR_BODY_SIZE_LIMIT} bytes`;
161+
requestEntry.comment = `Body discarded during HAR generation: longer than limit of ${
162+
options.bodySizeLimit
163+
} bytes`;
150164
} else {
151165
try {
152166
requestEntry.postData = generateHarPostBody(
@@ -235,7 +249,10 @@ function generateHarParamsFromParsedQuery(query: querystring.ParsedUrlQuery): Ha
235249
}));
236250
}
237251

238-
async function generateHarResponse(exchange: HttpExchange): Promise<HarFormat.Response> {
252+
async function generateHarResponse(
253+
exchange: HttpExchange,
254+
options: HarGenerationOptions
255+
): Promise<HarFormat.Response> {
239256
const { request, response } = exchange;
240257

241258
if (!response || response === 'aborted') {
@@ -256,7 +273,7 @@ async function generateHarResponse(exchange: HttpExchange): Promise<HarFormat.Re
256273

257274
let responseContent: { text: string, encoding?: string } | {};
258275
try {
259-
if (!decoded || decoded.byteLength > HAR_BODY_SIZE_LIMIT) {
276+
if (!decoded || decoded.byteLength > options.bodySizeLimit) {
260277
// If no body or the body is too large, don't include it
261278
responseContent = {};
262279
} else {
@@ -312,7 +329,10 @@ function getSourcesAsHarPages(exchanges: HttpExchange[]): HarFormat.Page[] {
312329
});
313330
}
314331

315-
async function generateHarEntry(exchange: HttpExchange): Promise<HarEntry> {
332+
async function generateHarEntry(
333+
exchange: HttpExchange,
334+
options: HarGenerationOptions
335+
): Promise<HarEntry> {
316336
const { timingEvents } = exchange;
317337

318338
const startTime = 'startTime' in timingEvents
@@ -336,8 +356,8 @@ async function generateHarEntry(exchange: HttpExchange): Promise<HarEntry> {
336356
pageref: exchange.request.source.summary,
337357
startedDateTime: dateFns.format(startTime),
338358
time: totalDuration,
339-
request: await generateHarRequest(exchange.request, true),
340-
response: await generateHarResponse(exchange),
359+
request: await generateHarRequest(exchange.request, true, options),
360+
response: await generateHarResponse(exchange, options),
341361
cache: {},
342362
timings: {
343363
blocked: -1,

0 commit comments

Comments
 (0)