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

Function toWebHandlerWith

packages/effect/src/http/HttpEffect.ts:417–462  ·  view source on GitHub ↗
(
  context: Context.Context<Provided>
)

Source from the content-addressed store, hash-verified

415 * @since 4.0.0
416 */
417export const toWebHandlerWith = <Provided, R = never, ReqR = Exclude<R, Provided | Scope.Scope | HttpServerRequest>>(
418 context: Context.Context<Provided>
419) =>
420<E>(
421 self: Effect.Effect<HttpServerResponse, E, R>,
422 middleware?: HttpMiddleware | undefined
423): [ReqR] extends [never] ?
424 (request: Request, context?: Context.Context<never> | undefined) => Promise<globalThis.Response>
425 : (request: Request, context: Context.Context<ReqR>) => Promise<globalThis.Response> =>
426{
427 const resolveSymbol = Symbol.for("@effect/platform/HttpApp/resolve")
428 const httpApp = toHandled(self, (request, response) => {
429 const withoutBody = request.method === "HEAD"
430 if (!Response.omitsBody(response, withoutBody)) {
431 // The transferred request scope closes when an abort interrupts the body, even if it is no longer read.
432 response = scopeTransferToStream(response)
433 }
434 ;(request as any)[resolveSymbol](
435 Response.toWeb(response, { withoutBody, context, signal: (request.source as globalThis.Request).signal })
436 )
437 return Effect.void
438 }, middleware)
439 return (request: Request, _context?: Context.Context<never> | undefined): Promise<globalThis.Response> =>
440 new Promise((resolve) => {
441 let reqContext = context
442 if (Context.isContext(_context)) {
443 for (const [key, value] of _context.mapUnsafe) {
444 reqContext = Context.addUnsafe(reqContext, key, value)
445 }
446 }
447 const httpServerRequest = Request.fromWeb(request)
448 reqContext = Context.add(reqContext, HttpServerRequest, httpServerRequest)
449 ;(httpServerRequest as any)[resolveSymbol] = resolve
450 const fiber = Effect.runForkWith(reqContext)(httpApp as any)
451 const signal = request.signal
452 if (fiber.pollUnsafe() === undefined && signal) {
453 const onAbort = () => fiber.interruptUnsafe(undefined, ClientAbort.annotation)
454 if (signal.aborted) {
455 onAbort()
456 } else {
457 signal.addEventListener("abort", onAbort, { once: true })
458 fiber.addObserver(() => signal.removeEventListener("abort", onAbort))
459 }
460 }
461 })
462}
463
464/**
465 * Converts an HTTP server effect into a Web `Request` handler using an empty base context.

Callers 2

HttpEffect.tsFile · 0.85
toWebHandlerLayerWithFunction · 0.85

Calls 8

toHandledFunction · 0.85
scopeTransferToStreamFunction · 0.85
pollUnsafeMethod · 0.80
addObserverMethod · 0.80
onAbortFunction · 0.70
addMethod · 0.65
addEventListenerMethod · 0.65
removeEventListenerMethod · 0.65

Tested by

no test coverage detected