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

Function asHttpEffect

packages/effect/src/unstable/http/HttpRouter.ts:191–239  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

189 middleware.add(middleware_ as any)
190 }),
191 asHttpEffect() {
192 let handler = Effect.withFiber<HttpServerResponse.HttpServerResponse, unknown>((fiber) => {
193 let context = fiber.context
194 const request = Context.getUnsafe(context, HttpServerRequest.HttpServerRequest)
195 let result = router.find(request.method, request.url)
196 if (result === undefined && request.method === "HEAD") {
197 result = router.find("GET", request.url)
198 }
199 if (result === undefined) {
200 return Effect.fail(
201 new HttpServerError.HttpServerError({
202 reason: new HttpServerError.RouteNotFound({ request })
203 })
204 )
205 }
206 const route = result.handler
207 if (Option.isSome(route.prefix)) {
208 context = Context.add(
209 context,
210 HttpServerRequest.HttpServerRequest,
211 sliceRequestUrl(request, route.prefix.value)
212 )
213 }
214 context = Context.add(context, HttpServerRequest.ParsedSearchParams, result.searchParams)
215 context = Context.add(context, RouteContext, {
216 route,
217 params: result.params
218 })
219
220 const span = Context.getOrUndefined(context, Tracer.ParentSpan)
221 if (span && span._tag === "Span") {
222 span.attribute("http.route", route.path)
223 }
224 return Effect.updateContext(
225 (route.uninterruptible ?
226 route.handler :
227 Effect.interruptible(route.handler)) as Effect.Effect<
228 HttpServerResponse.HttpServerResponse,
229 unknown
230 >,
231 () => context
232 )
233 })
234 if (middleware.size === 0) return handler
235 for (const fn of Arr.reverse(middleware)) {
236 handler = fn(handler as any)
237 }
238 return handler
239 }
240 })
241})
242

Callers

nothing calls this directly

Calls 7

sliceRequestUrlFunction · 0.85
getUnsafeMethod · 0.80
addMethod · 0.65
attributeMethod · 0.65
fnFunction · 0.50
findMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected