mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Switch to using abstraction for joining paths to control explicit side effect
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
5 files changed
+32
-14
No files matched your search
@@ -16,9 +16,9 @@ import registerChannelInjectable from "./register-channel/register-channel.injec
|
||||
import { getAppPaths } from "./get-app-paths";
|
||||
import getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.injectable";
|
||||
import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable";
|
||||
import path from "path";
|
||||
import appNameInjectable from "./app-name/app-name.injectable";
|
||||
import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable";
|
||||
import joinPathsInjectable from "../../common/path/join-paths.injectable";
|
||||
|
||||
const appPathsInjectable = getInjectable({
|
||||
id: "app-paths",
|
||||
@@ -55,10 +55,11 @@ const setupPathForUserData = async (di: DiContainerForSetup) => {
|
||||
const setElectronAppPath = await di.inject(setElectronAppPathInjectable);
|
||||
const appName = await di.inject(appNameInjectable);
|
||||
const getAppPath = await di.inject(getElectronAppPathInjectable);
|
||||
const joinPaths = await di.inject(joinPathsInjectable);
|
||||
|
||||
const appDataPath = getAppPath("appData");
|
||||
|
||||
setElectronAppPath("userData", path.join(appDataPath, appName));
|
||||
setElectronAppPath("userData", joinPaths(appDataPath, appName));
|
||||
};
|
||||
|
||||
// Todo: this kludge is here only until we have a proper place to setup integration testing.
|
||||
|
||||
@@ -13,20 +13,23 @@ 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";
|
||||
import type { JoinPaths } from "../../common/path/join-paths.injectable";
|
||||
import joinPathsInjectable from "../../common/path/join-paths.injectable";
|
||||
|
||||
interface ProductionDependencies {
|
||||
readFileBuffer: (path: string) => Promise<Buffer>;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
joinPaths: JoinPaths;
|
||||
}
|
||||
|
||||
const handleStaticFileInProduction =
|
||||
({ readFileBuffer, getAbsolutePath }: ProductionDependencies) =>
|
||||
({ readFileBuffer, getAbsolutePath, joinPaths }: ProductionDependencies) =>
|
||||
async ({ params }: LensApiRequest) => {
|
||||
const staticPath = getAbsolutePath(__static);
|
||||
let filePath = params.path;
|
||||
|
||||
for (let retryCount = 0; retryCount < 5; retryCount += 1) {
|
||||
const asset = path.join(staticPath, filePath);
|
||||
const asset = joinPaths(staticPath, filePath);
|
||||
const normalizedFilePath = getAbsolutePath(asset);
|
||||
|
||||
if (!normalizedFilePath.startsWith(staticPath)) {
|
||||
@@ -82,13 +85,14 @@ const staticFileRouteInjectable = getInjectable({
|
||||
const isDevelopment = di.inject(isDevelopmentInjectable);
|
||||
const readFileBuffer = di.inject(readFileBufferInjectable);
|
||||
const getAbsolutePath = di.inject(getAbsolutePathInjectable);
|
||||
const joinPaths = di.inject(joinPathsInjectable);
|
||||
|
||||
return {
|
||||
method: "get",
|
||||
path: `/{path*}`,
|
||||
handler: isDevelopment
|
||||
? handleStaticFileInDevelopment({ proxy: httpProxy.createProxy() })
|
||||
: handleStaticFileInProduction({ readFileBuffer, getAbsolutePath }),
|
||||
: handleStaticFileInProduction({ readFileBuffer, getAbsolutePath, joinPaths }),
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
Reference in new issue
Block a user