mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Switch to using abstraction for getting absolute path to control explicit side effect
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
1 parent
5ca3082572
commit
911481ea0f
11 files changed
+50
-29
No files matched your search
@@ -6,6 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
|
||||
import directoryForLensLocalStorageInjectable from "../../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
|
||||
import { initIpcMainHandlers } from "./init-ipc-main-handlers";
|
||||
import getAppMenuItemsInjectable from "../../menu/get-app-menu-items.injectable";
|
||||
import getAbsolutePathInjectable from "../../../common/path/get-absolute-path.injectable";
|
||||
|
||||
const initIpcMainHandlersInjectable = getInjectable({
|
||||
id: "init-ipc-main-handlers",
|
||||
@@ -13,6 +14,7 @@ const initIpcMainHandlersInjectable = getInjectable({
|
||||
instantiate: (di) => initIpcMainHandlers({
|
||||
getAppMenuItems: () => di.inject(getAppMenuItemsInjectable)(),
|
||||
directoryForLensLocalStorage: di.inject(directoryForLensLocalStorageInjectable),
|
||||
getAbsolutePath: di.inject(getAbsolutePathInjectable),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import { catalogEntityRegistry } from "../../catalog";
|
||||
import { pushCatalogToRenderer } from "../../catalog-pusher";
|
||||
import { ClusterManager } from "../../cluster-manager";
|
||||
import { ResourceApplier } from "../../resource-applier";
|
||||
import path from "path";
|
||||
import { remove } from "fs-extra";
|
||||
import { onLocationChange, handleWindowAction } from "../../ipc/window";
|
||||
import { openFilePickingDialogChannel } from "../../../common/ipc/dialog";
|
||||
@@ -23,13 +22,15 @@ import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppM
|
||||
import { getNativeColorTheme } from "../../native-theme";
|
||||
import { getNativeThemeChannel } from "../../../common/ipc/native-theme";
|
||||
import type { MenuItemsOpts } from "../../menu/get-app-menu-items.injectable";
|
||||
import type { GetAbsolutePath } from "../../../common/path/get-absolute-path.injectable";
|
||||
|
||||
interface Dependencies {
|
||||
getAppMenuItems: () => MenuItemsOpts[];
|
||||
directoryForLensLocalStorage: string;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
}
|
||||
|
||||
export const initIpcMainHandlers = ({ getAppMenuItems, directoryForLensLocalStorage }: Dependencies) => () => {
|
||||
export const initIpcMainHandlers = ({ getAppMenuItems, directoryForLensLocalStorage, getAbsolutePath }: Dependencies) => () => {
|
||||
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
||||
return ClusterStore.getInstance()
|
||||
.getById(clusterId)
|
||||
@@ -85,7 +86,7 @@ export const initIpcMainHandlers = ({ getAppMenuItems, directoryForLensLocalStor
|
||||
|
||||
try {
|
||||
// remove the local storage file
|
||||
const localStorageFilePath = path.resolve(directoryForLensLocalStorage, `${cluster.id}.json`);
|
||||
const localStorageFilePath = getAbsolutePath(directoryForLensLocalStorage, `${cluster.id}.json`);
|
||||
|
||||
await remove(localStorageFilePath);
|
||||
} catch {
|
||||
|
||||
@@ -7,7 +7,6 @@ import Call from "@hapi/call";
|
||||
import type http from "http";
|
||||
import type httpProxy from "http-proxy";
|
||||
import { toPairs } from "lodash/fp";
|
||||
import path from "path";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import type { LensApiResultContentType } from "./router-content-types";
|
||||
import { contentTypes } from "./router-content-types";
|
||||
@@ -51,7 +50,6 @@ interface Dependencies {
|
||||
|
||||
export class Router {
|
||||
protected router = new Call.Router();
|
||||
protected static rootPath = path.resolve(__static);
|
||||
|
||||
constructor(routes: Route<unknown>[], private dependencies: Dependencies) {
|
||||
routes.forEach(route => {
|
||||
|
||||
@@ -12,20 +12,22 @@ import path from "path";
|
||||
import isDevelopmentInjectable from "../../common/vars/is-development.injectable";
|
||||
import httpProxy from "http-proxy";
|
||||
import readFileBufferInjectable from "../../common/fs/read-file-buffer.injectable";
|
||||
import getAbsolutePathInjectable, { GetAbsolutePath } from "../../common/path/get-absolute-path.injectable";
|
||||
|
||||
interface ProductionDependencies {
|
||||
readFileBuffer: (path: string) => Promise<Buffer>;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
}
|
||||
|
||||
const handleStaticFileInProduction =
|
||||
({ readFileBuffer }: ProductionDependencies) =>
|
||||
({ readFileBuffer, getAbsolutePath }: ProductionDependencies) =>
|
||||
async ({ params }: LensApiRequest) => {
|
||||
const staticPath = path.resolve(__static);
|
||||
const staticPath = getAbsolutePath(__static);
|
||||
let filePath = params.path;
|
||||
|
||||
for (let retryCount = 0; retryCount < 5; retryCount += 1) {
|
||||
const asset = path.join(staticPath, filePath);
|
||||
const normalizedFilePath = path.resolve(asset);
|
||||
const normalizedFilePath = getAbsolutePath(asset);
|
||||
|
||||
if (!normalizedFilePath.startsWith(staticPath)) {
|
||||
return { statusCode: 404 };
|
||||
@@ -79,13 +81,14 @@ const staticFileRouteInjectable = getInjectable({
|
||||
instantiate: (di): Route<Buffer> => {
|
||||
const isDevelopment = di.inject(isDevelopmentInjectable);
|
||||
const readFileBuffer = di.inject(readFileBufferInjectable);
|
||||
const getAbsolutePath = di.inject(getAbsolutePathInjectable);
|
||||
|
||||
return {
|
||||
method: "get",
|
||||
path: `/{path*}`,
|
||||
handler: isDevelopment
|
||||
? handleStaticFileInDevelopment({ proxy: httpProxy.createProxy() })
|
||||
: handleStaticFileInProduction({ readFileBuffer }),
|
||||
: handleStaticFileInProduction({ readFileBuffer, getAbsolutePath }),
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
Reference in new issue
Block a user