From f73ae9ff9f3a735c6118a967fb568354ae31d1f4 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Tue, 24 Oct 2023 06:49:53 +0100 Subject: [PATCH] fix: revert changes that cause slow proxying --- src/core/utils/net.ts | 4 +- src/msha/middlewares/request.middleware.ts | 44 ++-------------------- 2 files changed, 6 insertions(+), 42 deletions(-) diff --git a/src/core/utils/net.ts b/src/core/utils/net.ts index 1af48591..8dfe694d 100644 --- a/src/core/utils/net.ts +++ b/src/core/utils/net.ts @@ -103,7 +103,7 @@ export async function validateDevServerConfig(url: string | undefined, timeout: const resolvedPortNumber = await isAcceptingTcpConnections({ port, host: hostname }); if (resolvedPortNumber !== 0) { const spinner = ora(); - let waitOnOneOfResources = hostname === "localhost" ? [`tcp:127.0.0.1:${port}`, `tcp:localhost:${port}`] : [`tcp:${hostname}:${port}`]; + const waitOnOneOfResources = hostname === "localhost" ? [`tcp:127.0.0.1:${port}`, `tcp:[::1]:${port}`] : [`tcp:${hostname}:${port}`]; spinner.start(`Waiting for ${chalk.green(url)} to be ready`); let promises = waitOnOneOfResources.map((resource) => { @@ -130,7 +130,7 @@ export async function validateDevServerConfig(url: string | undefined, timeout: try { await Promise.any(promises); - spinner.succeed(`${url} validated successfully`); + spinner.succeed(`Connected to ${url} successfully`); spinner.clear(); } catch { spinner.fail(); diff --git a/src/msha/middlewares/request.middleware.ts b/src/msha/middlewares/request.middleware.ts index cf4b6bd4..0f05fe53 100644 --- a/src/msha/middlewares/request.middleware.ts +++ b/src/msha/middlewares/request.middleware.ts @@ -9,8 +9,6 @@ import serveStatic from "serve-static"; import { DEFAULT_CONFIG } from "../../config"; import { findSWAConfigFile, logger, logRequest } from "../../core"; import { AUTH_STATUS, CUSTOM_URL_SCHEME, IS_APP_DEV_SERVER, SWA_PUBLIC_DIR } from "../../core/constants"; -import { parseUrl } from "../../core/utils/net"; -import waitOn from "wait-on"; import { getAuthBlockResponse, handleAuthRequest, isAuthRequest, isLoginRequest, isLogoutRequest } from "../handlers/auth.handler"; import { isDataApiRequest } from "../handlers/dab.handler"; import { handleErrorPage } from "../handlers/error-page.handler"; @@ -69,7 +67,7 @@ export async function handleUserConfig(appLocation: string | undefined): Promise * @param proxyApp An `http-proxy` instance. * @param target The root folder of the static app (ie. `output_location`). Or, the HTTP host target, if connecting to a dev server, or */ -async function serveStaticOrProxyResponse(req: http.IncomingMessage, res: http.ServerResponse, proxyApp: httpProxy, target: string | undefined) { +function serveStaticOrProxyResponse(req: http.IncomingMessage, res: http.ServerResponse, proxyApp: httpProxy, target: string | undefined) { if ([301, 302].includes(res.statusCode)) { res.end(); return; @@ -104,40 +102,6 @@ async function serveStaticOrProxyResponse(req: http.IncomingMessage, res: http.S let { protocol, hostname, port } = parseUrl(target); - if (hostname === "localhost") { - let waitOnOneOfResources = [`tcp:127.0.0.1:${port}`, `tcp:localhost:${port}`]; - let promises = waitOnOneOfResources.map((resource) => { - return waitOn({ - resources: [resource], - delay: 1000, // initial delay in ms, default 0 - interval: 100, // poll interval in ms, default 250ms - simultaneous: 1, // limit to 1 connection per resource at a time - timeout: 60000, // timeout in ms, default Infinity - tcpTimeout: 1000, // tcp timeout in ms, default 300ms - window: 1000, // stabilization time in ms, default 750ms - strictSSL: false, - verbose: false, // force disable verbose logs even if SWA_CLI_DEBUG is enabled - }) - .then(() => { - logger.silly(`Connected to ${resource} successfully`); - return resource; - }) - .catch((err) => { - logger.silly(`Could not connect to ${resource}`); - logger.warn(err.message); - throw err; - }); - }); - - try { - const availableUrl = await Promise.any(promises); - logger.silly(`${target} validated successfully`); - target = protocol + "//" + availableUrl.slice(4); - } catch { - logger.error(`Could not connect to "${target}". Is the server up and running?`); - } - } - proxyApp.web( req, res, @@ -247,7 +211,7 @@ export async function requestMiddleware( if (isWebsocketRequest(req)) { logger.silly(`websocket request detected`); - return await serveStaticOrProxyResponse(req, res, proxyApp, DEFAULT_CONFIG.outputLocation); + return serveStaticOrProxyResponse(req, res, proxyApp, DEFAULT_CONFIG.outputLocation); } let target = DEFAULT_CONFIG.outputLocation; @@ -262,7 +226,7 @@ export async function requestMiddleware( logger.silly(` - ${statusCodeToServe} code detected. Exit`); handleErrorPage(req, res, statusCodeToServe, userConfig?.responseOverrides); - return await serveStaticOrProxyResponse(req, res, proxyApp, target); + return serveStaticOrProxyResponse(req, res, proxyApp, target); } } @@ -333,6 +297,6 @@ export async function requestMiddleware( logger.silly(` - url: ${chalk.yellow(req.url)}`); logger.silly(` - target: ${chalk.yellow(target)}`); - await serveStaticOrProxyResponse(req, res, proxyApp, target); + serveStaticOrProxyResponse(req, res, proxyApp, target); } }