MCPcopy
hub / github.com/freshframework/fresh / handler

Method handler

packages/fresh/src/app.ts:391–496  ·  view source on GitHub ↗

* Create handler function for `Deno.serve` or to be used in * testing.

()

Source from the content-addressed store, hash-verified

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 }

Callers 15

listenMethod · 0.95
app_test.tsxFile · 0.80
getMethod · 0.80
postMethod · 0.80
patchMethod · 0.80
putMethod · 0.80
deleteMethod · 0.80
headMethod · 0.80
optionsMethod · 0.80
requestMethod · 0.80
createServerFunction · 0.80
websocket_test.tsFile · 0.80

Calls 8

matchMethod · 0.95
getAllowedMethodsMethod · 0.95
applyCommandsFunction · 0.90
defaultOptionsHandlerFunction · 0.85
nextFunction · 0.85
DEFAULT_ERROR_HANDLERFunction · 0.85
handlerFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected