@@ -110,14 +110,15 @@ export const withHar: WithHar = function <T extends typeof fetch>(
110110 // Make the request
111111 const response = await baseFetch ( input , options ) ;
112112
113- // Need to clone response to get both text and arrayBuffer
114- const responseClone = response . clone ( ) ;
113+ // Read from clones so HAR logging does not consume or reconstruct the real response.
114+ const responseTextClone = response . clone ( ) ;
115+ const responseBufferClone = response . clone ( ) ;
115116
116117 // Update firstByte time when we get the response
117118 entry . _timestamps . firstByte = process . hrtime ( ) ;
118119
119120 // Get the response body and update received time
120- const text = await response . text ( ) ;
121+ const text = response . body === null ? '' : await responseTextClone . text ( ) ;
121122 entry . _timestamps . received = process . hrtime ( ) ;
122123
123124 const harEntry = harEntryMap . get ( requestId ) ;
@@ -149,7 +150,7 @@ export const withHar: WithHar = function <T extends typeof fetch>(
149150 }
150151
151152 if ( harEntry . _compressed ) {
152- const rawBody = await responseClone . arrayBuffer ( ) ;
153+ const rawBody = await responseBufferClone . arrayBuffer ( ) ;
153154 harEntry . response . content . size = rawBody . byteLength ;
154155 } else {
155156 harEntry . response . content . size = text ? Buffer . byteLength ( text ) : - 1 ;
@@ -211,15 +212,7 @@ export const withHar: WithHar = function <T extends typeof fetch>(
211212 parent . pageref = entry . pageref ;
212213 } ) ;
213214
214- const Response =
215- defaults . Response || baseFetch . Response || global . Response || response . constructor ;
216- const responseCopy = new Response ( text , {
217- status : response . statusCode ,
218- statusText : response . statusText || '' ,
219- headers : response . headers ,
220- url : response . url ,
221- } ) ;
222- responseCopy . harEntry = entry ;
215+ response . harEntry = entry ;
223216
224217 if ( Array . isArray ( har ?. log ?. entries ) ) {
225218 har . log . entries . push ( ...parents , entry ) ;
@@ -232,6 +225,6 @@ export const withHar: WithHar = function <T extends typeof fetch>(
232225 onHarEntry ( entry ) ;
233226 }
234227
235- return responseCopy ;
228+ return response ;
236229 } as T ;
237230} ;
0 commit comments