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

Function toHttpApp

packages/platform/src/internal/httpRouter.ts:211–299  ·  view source on GitHub ↗
(
  self: Router.HttpRouter<E, R>
)

Source from the content-addressed store, hash-verified

209
210/** @internal */
211export const toHttpApp = <E, R>(
212 self: Router.HttpRouter<E, R>
213): Effect.Effect<App.Default<E | Error.RouteNotFound, R>> =>
214 Effect.map(FiberRef.get(currentRouterConfig), (config) => {
215 const router = FindMyWay.make<Router.Route<E, R>>(config)
216 const mounts = Chunk.toReadonlyArray(self.mounts).map(([path, app, options]) =>
217 [
218 path,
219 new RouteContextImpl(
220 new RouteImpl(
221 "*",
222 options?.includePrefix ? `${path}/*` as Router.PathInput : "/*",
223 app,
224 options?.includePrefix ? Option.none() : Option.some(path),
225 false
226 ),
227 {}
228 ),
229 options
230 ] as const
231 )
232 const mountsLen = mounts.length
233 Chunk.forEach(self.routes, (route) => {
234 if (route.method === "*") {
235 router.all(route.path, route)
236 } else {
237 router.on(route.method, route.path, route)
238 }
239 })
240 return Effect.withFiberRuntime<
241 ServerResponse.HttpServerResponse,
242 E | Error.RouteNotFound,
243 R | ServerRequest.HttpServerRequest
244 >((fiber) => {
245 const context = Context.unsafeMake(new Map(fiber.getFiberRef(FiberRef.currentContext).unsafeMap))
246 const request = Context.unsafeGet(context, ServerRequest.HttpServerRequest)
247 if (mountsLen > 0) {
248 const searchIndex = request.url.indexOf("?")
249 const pathname = searchIndex === -1 ? request.url : request.url.slice(0, searchIndex)
250
251 for (let i = 0; i < mountsLen; i++) {
252 const [path, routeContext, options] = mounts[i]
253 if (pathname === path || pathname.startsWith(path + "/")) {
254 context.unsafeMap.set(RouteContext.key, routeContext)
255 if (options?.includePrefix !== true) {
256 context.unsafeMap.set(ServerRequest.HttpServerRequest.key, sliceRequestUrl(request, path))
257 }
258 return Effect.locally(
259 Effect.flatMap(routeContext.route.handler, Respondable.toResponse) as App.Default<E, R>,
260 FiberRef.currentContext,
261 context
262 )
263 }
264 }
265 }
266
267 let result = router.find(request.method, request.url)
268 if (result === undefined && request.method === "HEAD") {

Callers 2

constructorMethod · 0.85
toWebHandlerFunction · 0.85

Calls 10

unsafeGetMethod · 0.80
sliceRequestUrlFunction · 0.70
mapMethod · 0.65
getMethod · 0.65
makeMethod · 0.65
getFiberRefMethod · 0.65
setMethod · 0.65
failMethod · 0.65
getOptionMethod · 0.65
attributeMethod · 0.65

Tested by

no test coverage detected