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

Function toWebHandlerRuntime

packages/platform/src/HttpApp.ts:219–249  ·  view source on GitHub ↗
(runtime: Runtime.Runtime<R>)

Source from the content-addressed store, hash-verified

217 * @category conversions
218 */
219export const toWebHandlerRuntime = <R>(runtime: Runtime.Runtime<R>) => {
220 const httpRuntime: Types.Mutable<Runtime.Runtime<R>> = Runtime.make(runtime)
221 const run = Runtime.runFork(httpRuntime)
222 return <E>(self: Default<E, R | Scope.Scope>, middleware?: HttpMiddleware | undefined) => {
223 const resolveSymbol = Symbol.for("@effect/platform/HttpApp/resolve")
224 const httpApp = toHandled(self, (request, response) => {
225 response = unsafeEjectStreamScope(response)
226 ;(request as any)[resolveSymbol](
227 ServerResponse.toWeb(response, { withoutBody: request.method === "HEAD", runtime })
228 )
229 return Effect.void
230 }, middleware)
231 return (request: Request, context?: Context.Context<never> | undefined): Promise<Response> =>
232 new Promise((resolve) => {
233 const contextMap = new Map<string, any>(runtime.context.unsafeMap)
234 if (Context.isContext(context)) {
235 for (const [key, value] of context.unsafeMap) {
236 contextMap.set(key, value)
237 }
238 }
239 const httpServerRequest = ServerRequest.fromWeb(request)
240 contextMap.set(ServerRequest.HttpServerRequest.key, httpServerRequest)
241 ;(httpServerRequest as any)[resolveSymbol] = resolve
242 httpRuntime.context = Context.unsafeMake(contextMap)
243 const fiber = run(httpApp as any)
244 request.signal?.addEventListener("abort", () => {
245 fiber.unsafeInterruptAsFork(ServerError.clientAbortFiberId)
246 }, { once: true })
247 })
248 }
249}
250
251/**
252 * @since 1.0.0

Callers 2

HttpApp.tsFile · 0.85
toWebHandlerLayerWithFunction · 0.85

Calls 7

toHandledFunction · 0.85
unsafeEjectStreamScopeFunction · 0.85
runFunction · 0.70
makeMethod · 0.65
setMethod · 0.65
addEventListenerMethod · 0.65
unsafeInterruptAsForkMethod · 0.65

Tested by

no test coverage detected