* Create handler function for `Deno.serve` or to be used in * testing.
()
| 389 | * testing. |
| 390 | */ |
| 391 | handler(): ( |
| 392 | request: Request, |
| 393 | info?: Deno.ServeHandlerInfo, |
| 394 | ) => Promise<Response> { |
| 395 | let buildCache = this.#getBuildCache(); |
| 396 | if (buildCache === null) { |
| 397 | if ( |
| 398 | this.config.mode === "production" && |
| 399 | DENO_DEPLOYMENT_ID !== undefined |
| 400 | ) { |
| 401 | throw new Error( |
| 402 | `Could not find _fresh directory. Maybe you forgot to run "deno task build" or maybe you're trying to run "main.ts" directly instead of "_fresh/server.js"?`, |
| 403 | ); |
| 404 | } else { |
| 405 | buildCache = new MockBuildCache([], this.config.mode); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | const router = new UrlPatternRouter<Middleware<State>>(); |
| 410 | |
| 411 | const { rootHandler } = applyCommands( |
| 412 | router, |
| 413 | this.#commands, |
| 414 | this.config.basePath, |
| 415 | this.#onError, |
| 416 | ); |
| 417 | |
| 418 | const trustProxy = this.config.trustProxy; |
| 419 | |
| 420 | return async ( |
| 421 | req: Request, |
| 422 | conn: Deno.ServeHandlerInfo = DEFAULT_CONN_INFO, |
| 423 | ) => { |
| 424 | const url = new URL(req.url); |
| 425 | // Prevent open redirect attacks |
| 426 | url.pathname = url.pathname.replace(/\/+/g, "/"); |
| 427 | |
| 428 | // Apply X-Forwarded-* headers when behind a reverse proxy |
| 429 | if (trustProxy) { |
| 430 | const proto = req.headers.get("x-forwarded-proto"); |
| 431 | if (proto) { |
| 432 | url.protocol = proto + ":"; |
| 433 | } |
| 434 | const host = req.headers.get("x-forwarded-host"); |
| 435 | if (host) { |
| 436 | url.host = host; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | const method = req.method.toUpperCase() as Method; |
| 441 | const matched = router.match(method, url); |
| 442 | let { params, pattern, item: handler, methodMatch } = matched; |
| 443 | |
| 444 | const span = trace.getActiveSpan(); |
| 445 | if (span && pattern) { |
| 446 | span.updateName(`${method} ${pattern}`); |
| 447 | span.setAttribute("http.route", pattern); |
| 448 | } |
no test coverage detected