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

Function toWebHandlerWith

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

Source from the content-addressed store, hash-verified

230 * @since 4.0.0
231 */
232export const toWebHandlerWith = <Provided, R = never, ReqR = Exclude<R, Provided | Scope.Scope | HttpServerRequest>>(
233 context: Context.Context<Provided>
234) =>
235<E>(
236 self: Effect.Effect<HttpServerResponse, E, R>,
237 middleware?: HttpMiddleware | undefined
238): [ReqR] extends [never] ?
239 (request: Request, context?: Context.Context<never> | undefined) => Promise<globalThis.Response>
240 : (request: Request, context: Context.Context<ReqR>) => Promise<globalThis.Response> =>
241{
242 const resolveSymbol = Symbol.for("@effect/platform/HttpApp/resolve")
243 const httpApp = toHandled(self, (request, response) => {
244 response = scopeTransferToStream(response)
245 ;(request as any)[resolveSymbol](
246 Response.toWeb(response, { withoutBody: request.method === "HEAD", context })
247 )
248 return Effect.void
249 }, middleware)
250 return (request: Request, _context?: Context.Context<never> | undefined): Promise<globalThis.Response> =>
251 new Promise((resolve) => {
252 let reqContext = context
253 if (Context.isContext(_context)) {
254 for (const [key, value] of _context.mapUnsafe) {
255 reqContext = Context.addUnsafe(reqContext, key, value)
256 }
257 }
258 const httpServerRequest = Request.fromWeb(request)
259 reqContext = Context.add(reqContext, HttpServerRequest, httpServerRequest)
260 ;(httpServerRequest as any)[resolveSymbol] = resolve
261 const fiber = Effect.runForkWith(reqContext)(httpApp as any)
262 request.signal?.addEventListener("abort", () => {
263 fiber.interruptUnsafe(undefined, ClientAbort.annotation)
264 }, { once: true })
265 })
266}
267
268/**
269 * Converts an HTTP server effect into a Web `Request` handler using an empty base context.

Callers 2

HttpEffect.tsFile · 0.85
handlerFunction · 0.85

Calls 5

toHandledFunction · 0.85
scopeTransferToStreamFunction · 0.85
interruptUnsafeMethod · 0.80
addMethod · 0.65
addEventListenerMethod · 0.65

Tested by

no test coverage detected