export function doesRequestPathMatchRoute()

in src/msha/routes-engine/route-processor.ts [8:66]


export function doesRequestPathMatchRoute(
  requestPath: string,
  routeRule: SWAConfigFileRoute | undefined,
  requestMethod: string | undefined | null,
  methods: string[] | undefined | null,
  authStatus: number
) {
  logger.silly(`check if request match route`);

  const route = routeRule?.route;
  const hasRouteRuleHasWildcard = route?.includes("*");
  logger.silly(` - route: ${chalk.yellow(route)}`);
  logger.silly(` - wildcard: ${chalk.yellow(hasRouteRuleHasWildcard)}`);

  // Do not match auth requests besides /.auth/login/<idp>
  // /.auth/login/<idp> must match a non wildcard rule
  // no allowed role can be listed for a rule with route /.auth/login/<idp>
  if (
    (authStatus != AUTH_STATUS.NoAuth && authStatus != AUTH_STATUS.HostNameAuthLogin) ||
    (authStatus == AUTH_STATUS.HostNameAuthLogin && (hasRouteRuleHasWildcard || routeRule?.allowedRoles?.length))
  ) {
    logger.silly(` - authStatus: ${chalk.yellow(authStatus)}`);
    logger.silly(` - allowedRoles: ${chalk.yellow(routeRule?.allowedRoles)}`);
    logger.silly(` - match: ${chalk.yellow(false)}`);
    return false;
  }

  // request method must match allowed methods if listed
  if (methods != null && !methods.includes(requestMethod!)) {
    logger.silly(` - methods: ${chalk.yellow(methods)}`);
    logger.silly(` - requestMethod: ${chalk.yellow(requestMethod)}`);
    logger.silly(` - match: ${chalk.yellow(false)}`);
    return false;
  }

  if (route === requestPath || (hasRouteRuleHasWildcard && doesRequestPathMatchWildcardRoute(requestPath, route))) {
    logger.silly(` - doesRequestPathMatchWildcardRoute: ${chalk.yellow(true)}`);
    return true;
  }

  // Since this is a file request, return now, since we are trying to get a match by appending /index.html doesn't apply here
  if (!route) {
    logger.silly(` - route: ${chalk.yellow(route || "<empty>")}`);
    logger.silly(` - match: ${chalk.yellow(false)}`);

    return false;
  }

  // If the request hasn't already matched the route, and the request is a non-file path,
  // try adding /index.html to the path to see if it then matches. This is especially handy
  // to match a request to the /{customPath}/* route
  const alternateRequestPath = getIndexHtml(requestPath);
  logger.silly(` - alternateRequestPath: ${chalk.yellow(alternateRequestPath)}`);

  return (
    routeRule?.route === alternateRequestPath ||
    (hasRouteRuleHasWildcard && doesRequestPathMatchWildcardRoute(alternateRequestPath, routeRule?.route))
  );
}