@@ -29,6 +29,10 @@ export interface Har extends HarFormat.Har {
29
29
log : HarLog ;
30
30
}
31
31
32
+ export interface HarGenerationOptions {
33
+ bodySizeLimit : number ;
34
+ }
35
+
32
36
interface HarLog extends HarFormat . Log {
33
37
// Custom field to expose failed TLS connections
34
38
_tlsErrors : HarTlsErrorEntry [ ] ;
@@ -66,15 +70,18 @@ export type HarTlsErrorEntry = {
66
70
clientPort : number ;
67
71
}
68
72
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 > {
70
77
const [ exchanges , otherEvents ] = _ . partition ( events , e => e . isHttp ( ) ) as [
71
78
HttpExchange [ ] , CollectedEvent [ ]
72
79
] ;
73
80
74
81
const errors = otherEvents . filter ( e => e . isTlsFailure ( ) ) as FailedTlsConnection [ ] ;
75
82
76
83
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 ) ) ) ;
78
85
const errorEntries = errors . map ( generateHarTlsError ) ;
79
86
80
87
return {
@@ -110,21 +117,26 @@ function asHtkHeaders(headers: HarFormat.Header[]) {
110
117
111
118
export function generateHarRequest (
112
119
request : HtkRequest ,
113
- waitForDecoding ?: false
120
+ waitForDecoding : false ,
121
+ options : HarGenerationOptions
114
122
) : ExtendedHarRequest ;
115
123
export function generateHarRequest (
116
124
request : HtkRequest ,
117
- waitForDecoding : true
125
+ waitForDecoding : true ,
126
+ options : HarGenerationOptions
118
127
) : ExtendedHarRequest | ObservablePromise < ExtendedHarRequest > ;
119
128
export function generateHarRequest (
120
129
request : HtkRequest ,
121
- waitForDecoding = false
130
+ waitForDecoding : boolean ,
131
+ options : HarGenerationOptions
122
132
) : ExtendedHarRequest | ObservablePromise < ExtendedHarRequest > {
123
133
if ( waitForDecoding && (
124
134
! request . body . decodedPromise . state ||
125
135
request . body . decodedPromise . state === 'pending'
126
136
) ) {
127
- return request . body . decodedPromise . then ( ( ) => generateHarRequest ( request ) ) ;
137
+ return request . body . decodedPromise . then ( ( ) =>
138
+ generateHarRequest ( request , false , options )
139
+ ) ;
128
140
}
129
141
130
142
const requestEntry : ExtendedHarRequest = {
@@ -144,9 +156,11 @@ export function generateHarRequest(
144
156
} ;
145
157
146
158
if ( request . body . decoded ) {
147
- if ( request . body . decoded . byteLength > HAR_BODY_SIZE_LIMIT ) {
159
+ if ( request . body . decoded . byteLength > options . bodySizeLimit ) {
148
160
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`;
150
164
} else {
151
165
try {
152
166
requestEntry . postData = generateHarPostBody (
@@ -235,7 +249,10 @@ function generateHarParamsFromParsedQuery(query: querystring.ParsedUrlQuery): Ha
235
249
} ) ) ;
236
250
}
237
251
238
- async function generateHarResponse ( exchange : HttpExchange ) : Promise < HarFormat . Response > {
252
+ async function generateHarResponse (
253
+ exchange : HttpExchange ,
254
+ options : HarGenerationOptions
255
+ ) : Promise < HarFormat . Response > {
239
256
const { request, response } = exchange ;
240
257
241
258
if ( ! response || response === 'aborted' ) {
@@ -256,7 +273,7 @@ async function generateHarResponse(exchange: HttpExchange): Promise<HarFormat.Re
256
273
257
274
let responseContent : { text : string , encoding ?: string } | { } ;
258
275
try {
259
- if ( ! decoded || decoded . byteLength > HAR_BODY_SIZE_LIMIT ) {
276
+ if ( ! decoded || decoded . byteLength > options . bodySizeLimit ) {
260
277
// If no body or the body is too large, don't include it
261
278
responseContent = { } ;
262
279
} else {
@@ -312,7 +329,10 @@ function getSourcesAsHarPages(exchanges: HttpExchange[]): HarFormat.Page[] {
312
329
} ) ;
313
330
}
314
331
315
- async function generateHarEntry ( exchange : HttpExchange ) : Promise < HarEntry > {
332
+ async function generateHarEntry (
333
+ exchange : HttpExchange ,
334
+ options : HarGenerationOptions
335
+ ) : Promise < HarEntry > {
316
336
const { timingEvents } = exchange ;
317
337
318
338
const startTime = 'startTime' in timingEvents
@@ -336,8 +356,8 @@ async function generateHarEntry(exchange: HttpExchange): Promise<HarEntry> {
336
356
pageref : exchange . request . source . summary ,
337
357
startedDateTime : dateFns . format ( startTime ) ,
338
358
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 ) ,
341
361
cache : { } ,
342
362
timings : {
343
363
blocked : - 1 ,
0 commit comments