1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

add retry cap to handleStaticFile to prevent an infitite loop

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2020-11-13 10:10:34 -05:00
parent 0fb859a22a
commit cd3c8c92ba
2 changed files with 11 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import { readFile } from "fs-extra"
import { Cluster } from "./cluster" import { Cluster } from "./cluster"
import { apiPrefix, appName, publicPath, isDevelopment, webpackDevServerPort } from "../common/vars"; import { apiPrefix, appName, publicPath, isDevelopment, webpackDevServerPort } from "../common/vars";
import { helmRoute, kubeconfigRoute, metricsRoute, portForwardRoute, resourceApplierRoute, watchRoute } from "./routes"; import { helmRoute, kubeconfigRoute, metricsRoute, portForwardRoute, resourceApplierRoute, watchRoute } from "./routes";
import logger from "./logger"
export interface RouterRequestOpts { export interface RouterRequestOpts {
req: http.IncomingMessage; req: http.IncomingMessage;
@ -94,7 +95,7 @@ export class Router {
return mimeTypes[path.extname(filename).slice(1)] || "text/plain" return mimeTypes[path.extname(filename).slice(1)] || "text/plain"
} }
async handleStaticFile(filePath: string, res: http.ServerResponse, req: http.IncomingMessage) { async handleStaticFile(filePath: string, res: http.ServerResponse, req: http.IncomingMessage, retryCount = 0) {
const asset = path.join(__static, filePath); const asset = path.join(__static, filePath);
try { try {
const filename = path.basename(req.url); const filename = path.basename(req.url);
@ -112,7 +113,13 @@ export class Router {
res.write(data); res.write(data);
res.end(); res.end();
} catch (err) { } catch (err) {
this.handleStaticFile(`${publicPath}/${appName}.html`, res, req); if (retryCount > 5) {
logger.error("handleStaticFile:", err.toString())
res.statusCode = 404
res.end()
return
}
this.handleStaticFile(`${publicPath}/${appName}.html`, res, req, Math.max(retryCount, 0) + 1);
} }
} }
@ -120,7 +127,7 @@ export class Router {
// Static assets // Static assets
this.router.add( this.router.add(
{ method: 'get', path: '/{path*}' }, { method: 'get', path: '/{path*}' },
({ params, response, path, raw: { req }}: LensApiRequest) => { ({ params, response, path, raw: { req } }: LensApiRequest) => {
this.handleStaticFile(params.path, response, req); this.handleStaticFile(params.path, response, req);
}); });

View File

@ -188,7 +188,7 @@ export function webpackLensRenderer({ showVars = true } = {}): webpack.Configura
isDevelopment && new webpack.HotModuleReplacementPlugin(), isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment && new ReactRefreshWebpackPlugin(), isDevelopment && new ReactRefreshWebpackPlugin(),
].filter(Boolean), ].filter(Boolean),
} }
} }