mirror of
https://github.com/lensapp/lens.git
synced 2024-10-26 09:47:18 +03:00
chore: Simplify file route handling
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
33a9f3ea35
commit
a9f48ee93a
@ -6,7 +6,6 @@
|
||||
// App's common configuration for any process (main, renderer, build pipeline, etc.)
|
||||
import type { ThemeId } from "../renderer/themes/lens-theme";
|
||||
|
||||
export const publicPath = "/build/" as string;
|
||||
export const defaultThemeId: ThemeId = "lens-dark";
|
||||
export const defaultFontSize = 12;
|
||||
export const defaultTerminalFontFamily = "RobotoMono";
|
||||
|
@ -5,9 +5,9 @@
|
||||
import type { LensApiResult } from "./route";
|
||||
|
||||
export interface LensApiResultContentType {
|
||||
resultMapper: (result: LensApiResult<any>) => ({
|
||||
resultMapper: (result: LensApiResult<unknown>) => ({
|
||||
statusCode: number;
|
||||
content: any;
|
||||
content: unknown;
|
||||
headers: Record<string, string>;
|
||||
});
|
||||
}
|
||||
@ -22,9 +22,13 @@ const resultMapperFor =
|
||||
|
||||
export type SupportedFileExtension = "json" | "txt" | "html" | "css" | "gif" | "jpg" | "png" | "svg" | "js" | "woff2" | "ttf";
|
||||
|
||||
export const contentTypes: Record<SupportedFileExtension, LensApiResultContentType> = {
|
||||
export interface ContentTypes extends Record<SupportedFileExtension, LensApiResultContentType> {
|
||||
[key: string]: LensApiResultContentType | undefined;
|
||||
}
|
||||
|
||||
export const contentTypes: ContentTypes = {
|
||||
json: {
|
||||
resultMapper: (result) => {
|
||||
resultMapper: (result: LensApiResult<unknown>) => {
|
||||
const resultMapper = resultMapperFor("application/json");
|
||||
|
||||
const mappedResult = resultMapper(result);
|
||||
|
@ -4,8 +4,8 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import httpProxy from "http-proxy";
|
||||
import path from "path";
|
||||
import { webpackDevServerPort } from "../../../../webpack/vars";
|
||||
import { publicPath } from "../../../common/vars";
|
||||
import type { LensApiRequest, RouteResponse } from "../../router/route";
|
||||
|
||||
const devStaticFileRouteHandlerInjectable = getInjectable({
|
||||
@ -14,10 +14,14 @@ const devStaticFileRouteHandlerInjectable = getInjectable({
|
||||
const proxy = httpProxy.createProxy();
|
||||
const proxyTarget = `http://127.0.0.1:${webpackDevServerPort}`;
|
||||
|
||||
return async ({ raw: { req, res }}: LensApiRequest<"/{path*}">): Promise<RouteResponse<Buffer>> => {
|
||||
if (req.url === "/" || !req.url || !req.url.startsWith(publicPath)) {
|
||||
req.url = `${publicPath}/index.html`;
|
||||
}
|
||||
return async ({ raw: { req, res }, params }: LensApiRequest<"/{path*}">): Promise<RouteResponse<Buffer>> => {
|
||||
const filePath = (!params.path || params.path === "/")
|
||||
? "/build/index.html"
|
||||
: path.posix.extname(params.path)
|
||||
? params.path
|
||||
: "/build/index.html";
|
||||
|
||||
req.url = filePath;
|
||||
|
||||
proxy.web(req, res, { target: proxyTarget });
|
||||
|
||||
|
@ -8,10 +8,8 @@ import joinPathsInjectable from "../../../common/path/join-paths.injectable";
|
||||
import staticFilesDirectoryInjectable from "../../../common/vars/static-files-directory.injectable";
|
||||
import type { LensApiRequest } from "../../router/route";
|
||||
import path from "path";
|
||||
import type { SupportedFileExtension } from "../../router/router-content-types";
|
||||
import { contentTypes } from "../../router/router-content-types";
|
||||
import { loggerInjectionToken } from "@k8slens/logger";
|
||||
import { publicPath } from "../../../common/vars";
|
||||
import { prefixedLoggerInjectable } from "@k8slens/logger";
|
||||
|
||||
const prodStaticFileRouteHandlerInjectable = getInjectable({
|
||||
id: "prod-static-file-route-handler",
|
||||
@ -19,39 +17,31 @@ const prodStaticFileRouteHandlerInjectable = getInjectable({
|
||||
const readFileBuffer = di.inject(readFileBufferInjectable);
|
||||
const joinPaths = di.inject(joinPathsInjectable);
|
||||
const staticFilesDirectory = di.inject(staticFilesDirectoryInjectable);
|
||||
const logger = di.inject(loggerInjectionToken);
|
||||
const logger = di.inject(prefixedLoggerInjectable, "FILE-ROUTE");
|
||||
|
||||
return async ({ params }: LensApiRequest<"/{path*}">) => {
|
||||
let filePath = params.path;
|
||||
const filePath = (!params.path || params.path === "/")
|
||||
? "/build/index.html"
|
||||
: path.posix.extname(params.path)
|
||||
? params.path
|
||||
: "/build/index.html";
|
||||
|
||||
const assetFilePath = joinPaths(staticFilesDirectory, filePath);
|
||||
|
||||
for (let retryCount = 0; retryCount < 5; retryCount += 1) {
|
||||
const assetFilePath = joinPaths(staticFilesDirectory, filePath);
|
||||
|
||||
if (!assetFilePath.startsWith(staticFilesDirectory)) {
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
|
||||
try {
|
||||
const fileExtension = path
|
||||
.extname(assetFilePath)
|
||||
.slice(1) as SupportedFileExtension;
|
||||
|
||||
const contentType = contentTypes[fileExtension] || contentTypes.txt;
|
||||
|
||||
return { response: await readFileBuffer(assetFilePath), contentType };
|
||||
} catch (err) {
|
||||
if (retryCount > 5) {
|
||||
logger.error("handleStaticFile:", String(err));
|
||||
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
|
||||
filePath = `${publicPath}/index.html`;
|
||||
}
|
||||
if (!assetFilePath.startsWith(staticFilesDirectory)) {
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
|
||||
return { statusCode: 404 };
|
||||
const fileExtension = path.extname(assetFilePath).slice(1);
|
||||
const contentType = contentTypes[fileExtension] || contentTypes.txt;
|
||||
|
||||
try {
|
||||
return { response: await readFileBuffer(assetFilePath), contentType };
|
||||
} catch (err) {
|
||||
logger.error(`failed to find file "${filePath}"`, err);
|
||||
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
|
@ -12,13 +12,11 @@ export const mainDir = path.join(process.cwd(), "src", "main");
|
||||
export const buildDir = path.join(process.cwd(), "static", "build");
|
||||
export const extensionEntry = path.join(process.cwd(), "src", "extensions", "extension-api.ts");
|
||||
export const extensionOutDir = path.join(process.cwd(), "packages", "extensions", "dist");
|
||||
export const assetsFolderName = "assets";
|
||||
export const rendererDir = path.join(process.cwd(), "src", "renderer");
|
||||
export const appName = isDevelopment
|
||||
? `${packageInfo.productName}Dev`
|
||||
: packageInfo.productName;
|
||||
export const htmlTemplate = path.resolve(rendererDir, "template.html");
|
||||
export const publicPath = "/build/";
|
||||
export const sassCommonVars = path.resolve(rendererDir, "components/vars.scss");
|
||||
export const webpackDevServerPort = Number(process.env.WEBPACK_DEV_SERVER_PORT) || 9191;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user