Fix type errors from testing in dev mode

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-01-10 15:18:08 -05:00
1 parent dba4f59644
commit 8136e23ac6
9 files changed
+33 -72

No files matched your search

@@ -3,10 +3,13 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { IncomingMessage } from "http";
import getClusterByIdInjectable from "../../common/cluster-store/get-by-id.injectable";
import type { Cluster } from "../../common/cluster/cluster";
import { getClusterIdFromHost } from "../../common/utils";
import { apiKubePrefix } from "../../common/vars";
import type { GetClusterForRequest } from "./lens-proxy";
export type GetClusterForRequest = (req: IncomingMessage) => Cluster | undefined;
const getClusterForRequestInjectable = getInjectable({
id: "get-cluster-for-request",
+3 -3
View File
@@ -20,12 +20,12 @@ import type { SelfSignedCert } from "selfsigned";
import type { GetClusterForRequest } from "../cluster/get-cluster-for-request.injectable";
export type ServerIncomingMessage = SetRequired<http.IncomingMessage, "url" | "method">;
export type LensProxyApiRequest = (args: ProxyApiRequestArgs) => void | Promise<void>;
export type ProxyRequestHandler = (args: ProxyApiRequestArgs) => void | Promise<void>;
interface Dependencies {
getClusterForRequest: GetClusterForRequest;
shellApiRequest: LensProxyApiRequest;
kubeApiUpgradeRequest: LensProxyApiRequest;
shellApiRequest: ProxyRequestHandler;
kubeApiUpgradeRequest: ProxyRequestHandler;
emitAppEvent: EmitAppEvent;
readonly router: Router;
readonly proxy: httpProxy;
@@ -7,40 +7,36 @@ import { URL } from "url";
import { WebSocketServer } from "ws";
import loggerInjectable from "../../../common/logger.injectable";
import shellApiAuthenticatorInjectable from "../../../features/terminal/main/shell-api-authenticator.injectable";
import clusterManagerInjectable from "../../cluster/manager.injectable";
import openShellSessionInjectable from "../../shell-session/create-shell-session.injectable";
import getClusterForRequestInjectable from "../get-cluster-for-request.injectable";
import type { ProxyApiRequestArgs } from "./types";
import type { ProxyRequestHandler } from "../lens-proxy";
const shellApiRequestInjectable = getInjectable({
id: "shell-api-request",
instantiate: (di) => {
instantiate: (di): ProxyRequestHandler => {
const openShellSession = di.inject(openShellSessionInjectable);
const clusterManager = di.inject(clusterManagerInjectable);
const getClusterForRequest = di.inject(getClusterForRequestInjectable);
const logger = di.inject(loggerInjectable);
const shellApiAuthenticator = di.inject(shellApiAuthenticatorInjectable);
const getClusterForRequest = di.inject(getClusterForRequestInjectable);
return ({ req, socket, head }: ProxyApiRequestArgs): void => {
return ({ req, socket, head }) => {
const cluster = getClusterForRequest(req);
const { searchParams } = new URL(req.url);
const { searchParams } = new URL(req.url, `https://${req.headers.host}`);
const tabId = searchParams.get("id");
const nodeName = searchParams.get("node");
const shellToken = searchParams.get("shellToken");
if (!tabId || !cluster || !shellApiAuthenticator.authenticate(cluster.id, tabId, shellToken)) {
socket.write("Invalid shell request");
return void socket.end();
socket.end();
} else {
new WebSocketServer({ noServer: true })
.handleUpgrade(req, socket, head, (websocket) => {
openShellSession({ websocket, cluster, tabId, nodeName })
.catch(error => logger.error(`[SHELL-SESSION]: failed to open a ${nodeName ? "node" : "local"} shell`, error));
});
}
const ws = new WebSocketServer({ noServer: true });
ws.handleUpgrade(req, socket, head, (websocket) => {
openShellSession({ websocket, cluster, tabId, nodeName })
.catch(error => logger.error(`[SHELL-SESSION]: failed to open a ${nodeName ? "node" : "local"} shell`, error));
});
};
},
});