MCPcopy Create free account
hub / github.com/Effect-TS/effect / dispatchUpgrade

Function dispatchUpgrade

packages/platform/node/src/NodeHttpClient.ts:257–321  ·  view source on GitHub ↗
(
  dispatcher: Undici.Dispatcher,
  request: HttpClientRequest,
  signal: AbortSignal,
  options: Undici.Dispatcher.DispatchOptions
)

Source from the content-addressed store, hash-verified

255// Dispatch directly to support both upgrades and readable refusal bodies.
256// Dispatcher.request rejects upgrades; Dispatcher.upgrade rejects non-101 responses.
257const dispatchUpgrade = (
258 dispatcher: Undici.Dispatcher,
259 request: HttpClientRequest,
260 signal: AbortSignal,
261 options: Undici.Dispatcher.DispatchOptions
262) =>
263 Effect.callback<UndiciResponseSource, Error.HttpClientError>((resume) => {
264 let controller: Undici.Dispatcher.DispatchController | undefined
265 let body: UpgradeResponseBody | undefined
266 let reason: globalThis.Error | undefined
267 const abort = (error: globalThis.Error) => {
268 reason ??= error
269 if (body !== undefined) body.destroy(error)
270 else controller?.abort(error)
271 }
272 const onAbort = () => abort(signal.reason)
273 const fail = (cause: unknown) =>
274 resume(Effect.fail(
275 new Error.HttpClientError({
276 reason: new Error.TransportError({ request, cause })
277 })
278 ))
279 if (signal.aborted) onAbort()
280 else signal.addEventListener("abort", onAbort, { once: true })
281 const done = () => signal.removeEventListener("abort", onAbort)
282 try {
283 dispatcher.dispatch(options, {
284 onRequestStart(c) {
285 controller = c
286 if (reason !== undefined) c.abort(reason)
287 },
288 onRequestUpgrade(_, statusCode, headers, socket) {
289 done()
290 const body = new UpgradeResponseBody(undefined)
291 body.push(null)
292 // undici has already pushed back the bytes past the response head
293 resume(Effect.succeed({ statusCode, headers, body, upgraded: takeOver(socket, undefined, signal) }))
294 },
295 onResponseStart(c, statusCode, headers) {
296 if (statusCode < 200) return
297 body = new UpgradeResponseBody(c)
298 resume(Effect.succeed({ statusCode, headers, body }))
299 },
300 onResponseData(c, chunk) {
301 if (!body!.push(chunk)) c.pause()
302 },
303 onResponseEnd() {
304 done()
305 body!.push(null)
306 },
307 onResponseError(_, cause) {
308 done()
309 if (body === undefined) fail(cause)
310 else body.destroy(cause)
311 }
312 })
313 } catch (cause) {
314 done()

Callers 1

NodeHttpClient.tsFile · 0.85

Calls 7

dispatchMethod · 0.80
onAbortFunction · 0.70
doneFunction · 0.70
failFunction · 0.70
abortFunction · 0.70
addEventListenerMethod · 0.65
syncMethod · 0.45

Tested by

no test coverage detected