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