From 4d3204766b1a609ee0be7dbc6eebff4ef60ce9a7 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 13 Nov 2020 15:45:57 -0500 Subject: [PATCH] add retry cap to handleStaticFile to prevent an infitite loop (#1373) * add retry cap to handleStaticFile to prevent an infitite loop * 'yarn -> yarn run' for node_module dependencies Signed-off-by: Sebastian Malton --- package.json | 36 ++++++++++++++++++------------------ src/main/router.ts | 13 ++++++++++--- webpack.renderer.ts | 2 +- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index cc50882d12..141a128d4a 100644 --- a/package.json +++ b/package.json @@ -11,35 +11,35 @@ "email": "info@k8slens.dev" }, "scripts": { - "dev": "concurrently -k \"yarn dev-run -C\" yarn:dev:*", + "dev": "concurrently -k \"yarn run dev-run -C\" yarn:dev:*", "dev-build": "concurrently yarn:compile:*", "dev-run": "nodemon --watch static/build/main.js --exec \"electron --inspect .\"", - "dev:main": "yarn compile:main --watch", - "dev:renderer": "yarn webpack-dev-server --config webpack.renderer.ts", - "dev:extension-types": "yarn compile:extension-types --watch", + "dev:main": "yarn run compile:main --watch", + "dev:renderer": "yarn run webpack-dev-server --config webpack.renderer.ts", + "dev:extension-types": "yarn run compile:extension-types --watch", "compile": "env NODE_ENV=production concurrently yarn:compile:*", - "compile:main": "yarn webpack --config webpack.main.ts", - "compile:renderer": "yarn webpack --config webpack.renderer.ts", - "compile:i18n": "yarn lingui compile", - "compile:extension-types": "yarn rollup --config src/extensions/rollup.config.js", - "npm:fix-package-version": "yarn ts-node build/set_npm_version.ts", - "build:linux": "yarn compile && electron-builder --linux --dir -c.productName=Lens", - "build:mac": "yarn compile && electron-builder --mac --dir -c.productName=Lens", - "build:win": "yarn compile && electron-builder --win --dir -c.productName=Lens", + "compile:main": "yarn run webpack --config webpack.main.ts", + "compile:renderer": "yarn run webpack --config webpack.renderer.ts", + "compile:i18n": "yarn run lingui compile", + "compile:extension-types": "yarn run rollup --config src/extensions/rollup.config.js", + "npm:fix-package-version": "yarn run ts-node build/set_npm_version.ts", + "build:linux": "yarn run compile && electron-builder --linux --dir -c.productName=Lens", + "build:mac": "yarn run compile && electron-builder --mac --dir -c.productName=Lens", + "build:win": "yarn run compile && electron-builder --win --dir -c.productName=Lens", "test": "jest --env=jsdom src $@", "integration": "jest --coverage integration $@", - "dist": "yarn compile && electron-builder --publish onTag", - "dist:win": "yarn compile && electron-builder --publish onTag --x64 --ia32", - "dist:dir": "yarn dist --dir -c.compression=store -c.mac.identity=null", + "dist": "yarn run compile && electron-builder --publish onTag", + "dist:win": "yarn run compile && electron-builder --publish onTag --x64 --ia32", + "dist:dir": "yarn run dist --dir -c.compression=store -c.mac.identity=null", "postinstall": "patch-package", - "i18n:extract": "yarn lingui extract", + "i18n:extract": "yarn run lingui extract", "download-bins": "concurrently yarn:download:*", "download:kubectl": "yarn run ts-node build/download_kubectl.ts", "download:helm": "yarn run ts-node build/download_helm.ts", "build:tray-icons": "yarn run ts-node build/build_tray_icon.ts", - "lint": "yarn eslint $@ --ext js,ts,tsx --max-warnings=0 src/", + "lint": "yarn run eslint $@ --ext js,ts,tsx --max-warnings=0 src/", "mkdocs-serve-local": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -it -p 8000:8000 -v ${PWD}:/docs mkdocs-serve-local:latest", - "typedocs-extensions-api": "yarn typedoc --ignoreCompilerErrors --readme docs/extensions/typedoc-readme.md.tpl --name @k8slens/extensions --out docs/extensions/api --mode library --excludePrivate --hideBreadcrumbs --includes src/ src/extensions/extension-api.ts" + "typedocs-extensions-api": "yarn run typedoc --ignoreCompilerErrors --readme docs/extensions/typedoc-readme.md.tpl --name @k8slens/extensions --out docs/extensions/api --mode library --excludePrivate --hideBreadcrumbs --includes src/ src/extensions/extension-api.ts" }, "config": { "bundledKubectlVersion": "1.17.11", diff --git a/src/main/router.ts b/src/main/router.ts index a5323bf150..230c93f09e 100644 --- a/src/main/router.ts +++ b/src/main/router.ts @@ -6,6 +6,7 @@ import { readFile } from "fs-extra" import { Cluster } from "./cluster" import { apiPrefix, appName, publicPath, isDevelopment, webpackDevServerPort } from "../common/vars"; import { helmRoute, kubeconfigRoute, metricsRoute, portForwardRoute, resourceApplierRoute, watchRoute } from "./routes"; +import logger from "./logger" export interface RouterRequestOpts { req: http.IncomingMessage; @@ -94,7 +95,7 @@ export class Router { 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); try { const filename = path.basename(req.url); @@ -112,7 +113,13 @@ export class Router { res.write(data); res.end(); } 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 this.router.add( { method: 'get', path: '/{path*}' }, - ({ params, response, path, raw: { req }}: LensApiRequest) => { + ({ params, response, path, raw: { req } }: LensApiRequest) => { this.handleStaticFile(params.path, response, req); }); diff --git a/webpack.renderer.ts b/webpack.renderer.ts index ea2b5f622c..62adc31bb5 100755 --- a/webpack.renderer.ts +++ b/webpack.renderer.ts @@ -188,7 +188,7 @@ export function webpackLensRenderer({ showVars = true } = {}): webpack.Configura isDevelopment && new webpack.HotModuleReplacementPlugin(), isDevelopment && new ReactRefreshWebpackPlugin(), - + ].filter(Boolean), } }