export async function requestMiddleware()

in src/msha/middlewares/request.middleware.ts [229:335]


export async function requestMiddleware(
  req: http.IncomingMessage,
  res: http.ServerResponse,
  proxyApp: httpProxy,
  userConfig: SWAConfigFile | undefined
) {
  if (!req.url) {
    return;
  }

  logger.silly(``);
  logger.silly(`--------------------------------------------------------`);
  logger.silly(`------------------- processing route -------------------`);
  logger.silly(`--------------------------------------------------------`);
  logger.silly(`processing ${chalk.yellow(req.url)}`);

  if (isWebsocketRequest(req)) {
    logger.silly(`websocket request detected`);
    return await serveStaticOrProxyResponse(req, res, proxyApp, DEFAULT_CONFIG.outputLocation);
  }

  let target = DEFAULT_CONFIG.outputLocation;

  logger.silly(`checking for matching route`);
  const matchingRouteRule = tryGetMatchingRoute(req, userConfig);
  if (matchingRouteRule) {
    logger.silly({ matchingRouteRule });

    const statusCodeToServe = parseInt(`${matchingRouteRule?.statusCode}`, 10);
    if ([404, 403, 401].includes(statusCodeToServe)) {
      logger.silly(` - ${statusCodeToServe} code detected. Exit`);

      handleErrorPage(req, res, statusCodeToServe, userConfig?.responseOverrides);
      return await serveStaticOrProxyResponse(req, res, proxyApp, target);
    }
  }

  let authStatus = AUTH_STATUS.NoAuth;
  const isAuthReq = isAuthRequest(req);

  logger.silly(`checking auth request`);
  if (isAuthReq) {
    logger.silly(` - auth request detected`);
    return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
  } else {
    logger.silly(` - not an auth request`);
  }

  logger.silly(`checking function request`);
  const isFunctionReq = isFunctionRequest(req, matchingRouteRule?.rewrite);
  if (!isFunctionReq) {
    logger.silly(` - not a function request`);
  }

  logger.silly(`checking data-api request`);
  const isDataApiReq = isDataApiRequest(req, matchingRouteRule?.rewrite);
  if (!isDataApiReq) {
    logger.silly(` - not a data Api request`);
  }

  if (!isRequestMethodValid(req, isFunctionReq, isAuthReq, isDataApiReq)) {
    res.statusCode = 405;
    return res.end();
  }

  logger.silly(`checking for query params`);

  const { urlPathnameWithoutQueryParams, url, urlPathnameWithQueryParams } = parseQueryParams(req, matchingRouteRule);

  logger.silly(`checking rewrite auth login request`);
  if (urlPathnameWithQueryParams && isLoginRequest(urlPathnameWithoutQueryParams)) {
    logger.silly(` - auth login dectected`);

    authStatus = AUTH_STATUS.HostNameAuthLogin;
    req.url = url.toString();
    return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
  }

  logger.silly(`checking rewrite auth logout request`);
  if (urlPathnameWithQueryParams && isLogoutRequest(urlPathnameWithoutQueryParams)) {
    logger.silly(` - auth logout dectected`);

    authStatus = AUTH_STATUS.HostNameAuthLogout;
    req.url = url.toString();
    return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
  }

  if (!isRouteRequiringUserRolesCheck(req, matchingRouteRule, isFunctionReq, authStatus)) {
    handleErrorPage(req, res, 401, userConfig?.responseOverrides);
    return await serveStaticOrProxyResponse(req, res, proxyApp, target);
  }

  if (authStatus != AUTH_STATUS.NoAuth && (authStatus != AUTH_STATUS.HostNameAuthLogin || !urlPathnameWithQueryParams)) {
    if (authStatus == AUTH_STATUS.HostNameAuthLogin && matchingRouteRule) {
      return getAuthBlockResponse(req, res, userConfig, matchingRouteRule);
    }

    return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
  }

  if (!getResponse(req, res, matchingRouteRule, userConfig, isFunctionReq, isDataApiReq)) {
    logger.silly(` - url: ${chalk.yellow(req.url)}`);
    logger.silly(` - target: ${chalk.yellow(target)}`);

    await serveStaticOrProxyResponse(req, res, proxyApp, target);
  }
}