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

Function dispatchWithProtocolPreference

deps/undici/src/lib/web/fetch/index.js:2194–2408  ·  view source on GitHub ↗
(body, allowH2)

Source from the content-addressed store, hash-verified

2192 return dispatchWithProtocolPreference(body)
2193
2194 function dispatchWithProtocolPreference (body, allowH2) {
2195 return new Promise((resolve, reject) => agent.dispatch(
2196 {
2197 path: hasTrailingQuestionMark ? `${path}?` : path,
2198 origin: url.origin,
2199 method: request.method,
2200 body: agent.isMockActive ? request.body && (request.body.source || request.body.stream) : body,
2201 // Preserve the serialized fetch body for MockAgent net-connect fallthroughs.
2202 __mockAgentBodyForDispatch: body,
2203 headers: request.headersList.entries,
2204 maxRedirections: 0,
2205 upgrade: request.mode === 'websocket' ? 'websocket' : undefined,
2206 ...(allowH2 === false ? { allowH2 } : null)
2207 },
2208 {
2209 body: null,
2210 abort: null,
2211
2212 onRequestStart (controller) {
2213 // TODO (fix): Do we need connection here?
2214 const { connection } = fetchParams.controller
2215
2216 // Set timingInfo’s final connection timing info to the result of calling clamp and coarsen
2217 // connection timing info with connection’s timing info, timingInfo’s post-redirect start
2218 // time, and fetchParams’s cross-origin isolated capability.
2219 // TODO: implement connection timing
2220 timingInfo.finalConnectionTimingInfo = clampAndCoarsenConnectionTimingInfo(undefined, timingInfo.postRedirectStartTime, fetchParams.crossOriginIsolatedCapability)
2221
2222 const abort = (reason) => controller.abort(reason)
2223
2224 if (connection.destroyed) {
2225 abort(new DOMException('The operation was aborted.', 'AbortError'))
2226 } else {
2227 fetchParams.controller.on('terminated', abort)
2228 this.abort = connection.abort = abort
2229 }
2230
2231 // Set timingInfo’s final network-request start time to the coarsened shared current time given
2232 // fetchParams’s cross-origin isolated capability.
2233 timingInfo.finalNetworkRequestStartTime = coarsenedSharedCurrentTime(fetchParams.crossOriginIsolatedCapability)
2234 },
2235
2236 onResponseStarted () {
2237 // Set timingInfo’s final network-response start time to the coarsened shared current
2238 // time given fetchParams’s cross-origin isolated capability, immediately after the
2239 // user agent’s HTTP parser receives the first byte of the response (e.g., frame header
2240 // bytes for HTTP/2 or response status line for HTTP/1.x).
2241 timingInfo.finalNetworkResponseStartTime = coarsenedSharedCurrentTime(fetchParams.crossOriginIsolatedCapability)
2242 },
2243
2244 onResponseStart (controller, status, headers, statusText) {
2245 if (status < 200) {
2246 return
2247 }
2248
2249 const rawHeaders = controller?.rawHeaders ?? []
2250 const headersList = new HeadersList()
2251 appendHeadersListFromResponseHeaders(headersList, headers, rawHeaders)

Callers 2

dispatchFunction · 0.70
onResponseErrorFunction · 0.70

Calls 1

dispatchMethod · 0.65

Tested by

no test coverage detected