MCPcopy Create free account
hub / github.com/nodejs/node / handleReply

Function handleReply

deps/undici/src/lib/mock/mock-utils.js:362–403  ·  view source on GitHub ↗
(mockDispatches, _data = data)

Source from the content-addressed store, hash-verified

360 }
361
362 function handleReply (mockDispatches, _data = data) {
363 // Don't send response if the request was aborted
364 if (aborted) {
365 return
366 }
367
368 // fetch's HeadersList is a 1D string array
369 const optsHeaders = Array.isArray(opts.headers)
370 ? buildHeadersFromArray(opts.headers)
371 : opts.headers
372 const body = typeof _data === 'function'
373 ? _data({ ...opts, headers: optsHeaders })
374 : _data
375
376 // util.types.isPromise is likely needed for jest.
377 if (isPromise(body)) {
378 // If handleReply is asynchronous, throwing an error
379 // in the callback will reject the promise, rather than
380 // synchronously throw the error, which breaks some tests.
381 // Rather, we wait for the callback to resolve if it is a
382 // promise, and then re-run handleReply with the new body.
383 return body.then((newData) => handleReply(mockDispatches, newData))
384 }
385
386 // Check again if aborted after async body resolution
387 if (aborted) {
388 return
389 }
390
391 const responseData = getResponseData(body)
392 const responseHeaders = generateKeyValues(headers)
393 const responseTrailers = generateKeyValues(trailers)
394
395 // Update the controller with response data
396 controller.rawHeaders = responseHeaders
397 controller.rawTrailers = responseTrailers
398
399 handler.onResponseStart?.(controller, statusCode, parseHeaders(responseHeaders), getStatusText(statusCode))
400 handler.onResponseData?.(controller, Buffer.from(responseData))
401 handler.onResponseEnd?.(controller, parseHeaders(responseTrailers))
402 deleteMockDispatch(mockDispatches, key)
403 }
404
405 return true
406}

Callers 1

mockDispatchFunction · 0.85

Calls 11

buildHeadersFromArrayFunction · 0.85
getResponseDataFunction · 0.85
generateKeyValuesFunction · 0.85
getStatusTextFunction · 0.85
deleteMockDispatchFunction · 0.85
onResponseStartMethod · 0.65
onResponseDataMethod · 0.65
onResponseEndMethod · 0.65
parseHeadersFunction · 0.50
thenMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected