Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert changes that cause slow proxying #758

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/utils/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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();
Expand Down
40 changes: 4 additions & 36 deletions src/msha/middlewares/request.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -104,36 +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],
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
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}`);
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,
Expand Down Expand Up @@ -243,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;
Expand All @@ -258,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);
}
}

Expand Down Expand Up @@ -329,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);
}
}