mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Clean up versioning and packageJson uses (#6196)
This commit is contained in:
1 parent
cfe35ded90
commit
67ac1692b2
116 files changed
+1154
-644
No files matched your search
@@ -6,7 +6,7 @@ import type { SupportedFileExtension } from "../router/router-content-types";
|
||||
import { contentTypes } from "../router/router-content-types";
|
||||
import logger from "../logger";
|
||||
import { getRouteInjectable } from "../router/router.injectable";
|
||||
import { appName, publicPath } from "../../common/vars";
|
||||
import { publicPath } from "../../common/vars";
|
||||
import path from "path";
|
||||
import isDevelopmentInjectable from "../../common/vars/is-development.injectable";
|
||||
import httpProxy from "http-proxy";
|
||||
@@ -19,66 +19,78 @@ import { webpackDevServerPort } from "../../../webpack/vars";
|
||||
import type { LensApiRequest, RouteResponse } from "../router/route";
|
||||
import { route } from "../router/route";
|
||||
import staticFilesDirectoryInjectable from "../../common/vars/static-files-directory.injectable";
|
||||
import appNameInjectable from "../../common/vars/app-name.injectable";
|
||||
|
||||
interface ProductionDependencies {
|
||||
readFileBuffer: (path: string) => Promise<Buffer>;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
joinPaths: JoinPaths;
|
||||
staticFilesDirectory: string;
|
||||
appName: string;
|
||||
}
|
||||
|
||||
const handleStaticFileInProduction =
|
||||
({ readFileBuffer, getAbsolutePath, joinPaths, staticFilesDirectory }: ProductionDependencies) =>
|
||||
async ({ params }: LensApiRequest<"/{path*}">): Promise<RouteResponse<Buffer>> => {
|
||||
let filePath = params.path;
|
||||
const handleStaticFileInProduction = ({
|
||||
readFileBuffer,
|
||||
getAbsolutePath,
|
||||
joinPaths,
|
||||
staticFilesDirectory,
|
||||
appName,
|
||||
}: ProductionDependencies) => (
|
||||
async ({ params }: LensApiRequest<"/{path*}">): Promise<RouteResponse<Buffer>> => {
|
||||
let filePath = params.path;
|
||||
|
||||
for (let retryCount = 0; retryCount < 5; retryCount += 1) {
|
||||
const asset = joinPaths(staticFilesDirectory, filePath);
|
||||
const normalizedFilePath = getAbsolutePath(asset);
|
||||
for (let retryCount = 0; retryCount < 5; retryCount += 1) {
|
||||
const asset = joinPaths(staticFilesDirectory, filePath);
|
||||
const normalizedFilePath = getAbsolutePath(asset);
|
||||
|
||||
if (!normalizedFilePath.startsWith(staticFilesDirectory)) {
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
|
||||
try {
|
||||
const fileExtension = path
|
||||
.extname(asset)
|
||||
.slice(1) as SupportedFileExtension;
|
||||
|
||||
const contentType = contentTypes[fileExtension] || contentTypes.txt;
|
||||
|
||||
return { response: await readFileBuffer(asset), contentType };
|
||||
} catch (err) {
|
||||
if (retryCount > 5) {
|
||||
logger.error("handleStaticFile:", String(err));
|
||||
|
||||
if (!normalizedFilePath.startsWith(staticFilesDirectory)) {
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
|
||||
try {
|
||||
const fileExtension = path
|
||||
.extname(asset)
|
||||
.slice(1) as SupportedFileExtension;
|
||||
|
||||
const contentType = contentTypes[fileExtension] || contentTypes.txt;
|
||||
|
||||
return { response: await readFileBuffer(asset), contentType };
|
||||
} catch (err) {
|
||||
if (retryCount > 5) {
|
||||
logger.error("handleStaticFile:", String(err));
|
||||
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
|
||||
filePath = `${publicPath}/${appName}.html`;
|
||||
}
|
||||
filePath = `${publicPath}/${appName}.html`;
|
||||
}
|
||||
}
|
||||
|
||||
return { statusCode: 404 };
|
||||
};
|
||||
return { statusCode: 404 };
|
||||
}
|
||||
);
|
||||
|
||||
interface DevelopmentDependencies {
|
||||
proxy: httpProxy;
|
||||
appName: string;
|
||||
}
|
||||
|
||||
const handleStaticFileInDevelopment =
|
||||
({ proxy }: DevelopmentDependencies) =>
|
||||
({ raw: { req, res }}: LensApiRequest<"/{path*}">): RouteResponse<Buffer> => {
|
||||
if (req.url === "/" || !req.url?.startsWith("/build/")) {
|
||||
req.url = `${publicPath}/${appName}.html`;
|
||||
}
|
||||
const handleStaticFileInDevelopment = ({
|
||||
proxy,
|
||||
appName,
|
||||
}: DevelopmentDependencies) => (
|
||||
({ raw: { req, res }}: LensApiRequest<"/{path*}">): RouteResponse<Buffer> => {
|
||||
if (req.url === "/" || !req.url?.startsWith("/build/")) {
|
||||
req.url = `${publicPath}/${appName}.html`;
|
||||
}
|
||||
|
||||
proxy.web(req, res, {
|
||||
target: `http://127.0.0.1:${webpackDevServerPort}`,
|
||||
});
|
||||
proxy.web(req, res, {
|
||||
target: `http://127.0.0.1:${webpackDevServerPort}`,
|
||||
});
|
||||
|
||||
return { proxy };
|
||||
};
|
||||
return { proxy };
|
||||
}
|
||||
);
|
||||
|
||||
const staticFileRouteInjectable = getRouteInjectable({
|
||||
id: "static-file-route",
|
||||
@@ -89,6 +101,7 @@ const staticFileRouteInjectable = getRouteInjectable({
|
||||
const getAbsolutePath = di.inject(getAbsolutePathInjectable);
|
||||
const joinPaths = di.inject(joinPathsInjectable);
|
||||
const staticFilesDirectory = di.inject(staticFilesDirectoryInjectable);
|
||||
const appName = di.inject(appNameInjectable);
|
||||
|
||||
return route({
|
||||
method: "get",
|
||||
@@ -97,12 +110,14 @@ const staticFileRouteInjectable = getRouteInjectable({
|
||||
isDevelopment
|
||||
? handleStaticFileInDevelopment({
|
||||
proxy: httpProxy.createProxy(),
|
||||
appName,
|
||||
})
|
||||
: handleStaticFileInProduction({
|
||||
readFileBuffer,
|
||||
getAbsolutePath,
|
||||
joinPaths,
|
||||
staticFilesDirectory,
|
||||
appName,
|
||||
}),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -3,20 +3,24 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getRouteInjectable } from "../../router/router.injectable";
|
||||
import { getAppVersion } from "../../../common/utils";
|
||||
import { route } from "../../router/route";
|
||||
import buildVersionInjectable from "../../vars/build-version/build-version.injectable";
|
||||
|
||||
const getVersionRouteInjectable = getRouteInjectable({
|
||||
id: "get-version-route",
|
||||
|
||||
instantiate: () => route({
|
||||
method: "get",
|
||||
path: `/version`,
|
||||
})(() => ({
|
||||
response: {
|
||||
version: getAppVersion(),
|
||||
},
|
||||
})),
|
||||
instantiate: (di) => {
|
||||
const buildVersion = di.inject(buildVersionInjectable);
|
||||
|
||||
return route({
|
||||
method: "get",
|
||||
path: `/version`,
|
||||
})(() => ({
|
||||
response: {
|
||||
version: buildVersion.get(),
|
||||
},
|
||||
}));
|
||||
},
|
||||
});
|
||||
|
||||
export default getVersionRouteInjectable;
|
||||
Reference in new issue
Block a user