diff --git a/src/common/utils/channel/message-channel-injection-token.ts b/src/common/utils/channel/message-channel-injection-token.ts index 3141acedf3..c81ac08d6b 100644 --- a/src/common/utils/channel/message-channel-injection-token.ts +++ b/src/common/utils/channel/message-channel-injection-token.ts @@ -4,9 +4,9 @@ */ import { getInjectionToken } from "@ogre-tools/injectable"; -import type { JsonValue } from "type-fest"; +import type { IpcValue } from "./allowed-types"; -export interface MessageChannel { +export interface MessageChannel { id: string; _messageSignature?: Message; } diff --git a/src/common/utils/channel/message-to-channel-injection-token.ts b/src/common/utils/channel/message-to-channel-injection-token.ts index 8c5f03b9ee..10898af43a 100644 --- a/src/common/utils/channel/message-to-channel-injection-token.ts +++ b/src/common/utils/channel/message-to-channel-injection-token.ts @@ -4,20 +4,16 @@ */ import { getInjectionToken } from "@ogre-tools/injectable"; import type { SetRequired } from "type-fest"; +import type { IpcValue } from "./allowed-types"; import type { MessageChannel } from "./message-channel-injection-token"; export interface MessageToChannel { - , TMessage extends void>( - channel: TChannel, - ): void; - - >( - channel: TChannel, - message: SetRequired["_messageSignature"], - ): void; + >(channel: Channel): void; + < + Channel extends MessageChannel, + Message = SetRequired["_messageSignature"], + >(channel: Channel, message: Message): void; } - -export const messageToChannelInjectionToken = - getInjectionToken({ - id: "message-to-message-channel", - }); +export const messageToChannelInjectionToken = getInjectionToken({ + id: "message-to-message-channel", +}); diff --git a/src/common/utils/sync-box/sync-box-injection-token.ts b/src/common/utils/sync-box/sync-box-injection-token.ts index 8db80243d3..58cb94a100 100644 --- a/src/common/utils/sync-box/sync-box-injection-token.ts +++ b/src/common/utils/sync-box/sync-box-injection-token.ts @@ -4,6 +4,7 @@ */ import { getInjectionToken } from "@ogre-tools/injectable"; import type { IComputedValue } from "mobx"; + export interface SyncBox { id: string; value: IComputedValue; diff --git a/src/common/utils/tentative-parse-json.ts b/src/common/utils/tentative-parse-json.ts deleted file mode 100644 index a0cb089a74..0000000000 --- a/src/common/utils/tentative-parse-json.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { pipeline } from "@ogre-tools/fp"; -import { defaultTo } from "lodash/fp"; -import { withErrorSuppression } from "./with-error-suppression/with-error-suppression"; - -export const tentativeParseJson = (toBeParsed: any) => pipeline( - toBeParsed, - withErrorSuppression(JSON.parse), - defaultTo(toBeParsed), -); - - diff --git a/src/common/utils/tentative-stringify-json.ts b/src/common/utils/tentative-stringify-json.ts deleted file mode 100644 index dc7206be7c..0000000000 --- a/src/common/utils/tentative-stringify-json.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { pipeline } from "@ogre-tools/fp"; -import { defaultTo } from "lodash/fp"; -import { withErrorSuppression } from "./with-error-suppression/with-error-suppression"; - -export const tentativeStringifyJson = (toBeParsed: any) => pipeline( - toBeParsed, - withErrorSuppression(JSON.stringify), - defaultTo(toBeParsed), -); - - diff --git a/src/main/lens-proxy/lens-proxy-port.injectable.ts b/src/main/lens-proxy/lens-proxy-port.injectable.ts index 771b61e8a3..957d5a64e1 100644 --- a/src/main/lens-proxy/lens-proxy-port.injectable.ts +++ b/src/main/lens-proxy/lens-proxy-port.injectable.ts @@ -22,7 +22,7 @@ const lensProxyPortInjectable = getInjectable({ }, set: (port: number) => { - if (port) { + if (portNumber) { throw new Error( "Tried to set port number for LensProxy when it has already been set.", ); diff --git a/src/main/lens-proxy/lens-proxy.ts b/src/main/lens-proxy/lens-proxy.ts index f9ea49753f..5d88fae341 100644 --- a/src/main/lens-proxy/lens-proxy.ts +++ b/src/main/lens-proxy/lens-proxy.ts @@ -86,7 +86,7 @@ export class LensProxy { const reqHandler = isInternal ? dependencies.shellApiRequest : dependencies.kubeApiUpgradeRequest; (async () => reqHandler({ req, socket, head, cluster }))() - .catch(error => logger.error("[LENS-PROXY]: failed to handle proxy upgrade", error)); + .catch(error => logger.error(`[LENS-PROXY]: failed to handle proxy upgrade: ${error}`)); } }); } diff --git a/src/main/lens-proxy/proxy-functions/shell/api-request.injectable.ts b/src/main/lens-proxy/proxy-functions/shell/api-request.injectable.ts index b84b9ed761..b18ae65805 100644 --- a/src/main/lens-proxy/proxy-functions/shell/api-request.injectable.ts +++ b/src/main/lens-proxy/proxy-functions/shell/api-request.injectable.ts @@ -21,7 +21,7 @@ const shellApiRequestInjectable = getInjectable({ return ({ req, socket, head }) => { const cluster = getClusterForRequest(req); - const { searchParams } = new URL(req.url); + const { searchParams } = new URL(req.url, "https://127.0.0.1"); const nodeName = searchParams.get("node") || undefined; const shellToken = searchParams.get("shellToken"); const tabId = searchParams.get("id"); diff --git a/src/main/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts b/src/main/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts index 6b7fa9b8df..464085ecc4 100644 --- a/src/main/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts +++ b/src/main/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts @@ -6,8 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable"; import type { IpcMainEvent } from "electron"; import ipcMainInjectable from "../ipc-main/ipc-main.injectable"; import { enlistMessageChannelListenerInjectionToken } from "../../../../common/utils/channel/enlist-message-channel-listener-injection-token"; -import { pipeline } from "@ogre-tools/fp"; -import { tentativeParseJson } from "../../../../common/utils/tentative-parse-json"; +import { toJS } from "../../../../renderer/utils"; const enlistMessageChannelListenerInjectable = getInjectable({ id: "enlist-message-channel-listener-for-main", @@ -16,13 +15,7 @@ const enlistMessageChannelListenerInjectable = getInjectable({ const ipcMain = di.inject(ipcMainInjectable); return ({ channel, handler }) => { - const nativeOnCallback = (_: IpcMainEvent, message: unknown) => { - pipeline( - message, - tentativeParseJson, - handler, - ); - }; + const nativeOnCallback = (_: IpcMainEvent, message: unknown) => toJS(handler(message)); ipcMain.on(channel.id, nativeOnCallback); diff --git a/src/main/utils/channel/channel-listeners/enlist-request-channel-listener.injectable.ts b/src/main/utils/channel/channel-listeners/enlist-request-channel-listener.injectable.ts index 6f118288f3..cb9de49f01 100644 --- a/src/main/utils/channel/channel-listeners/enlist-request-channel-listener.injectable.ts +++ b/src/main/utils/channel/channel-listeners/enlist-request-channel-listener.injectable.ts @@ -6,9 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable"; import type { IpcMainInvokeEvent } from "electron"; import ipcMainInjectable from "../ipc-main/ipc-main.injectable"; import { enlistRequestChannelListenerInjectionToken } from "../../../../common/utils/channel/enlist-request-channel-listener-injection-token"; -import { pipeline } from "@ogre-tools/fp"; -import { tentativeParseJson } from "../../../../common/utils/tentative-parse-json"; -import { tentativeStringifyJson } from "../../../../common/utils/tentative-stringify-json"; +import { toJS } from "../../../../renderer/utils"; const enlistRequestChannelListenerInjectable = getInjectable({ id: "enlist-request-channel-listener-for-main", @@ -17,8 +15,7 @@ const enlistRequestChannelListenerInjectable = getInjectable({ const ipcMain = di.inject(ipcMainInjectable); return ({ channel, handler }) => { - const nativeHandleCallback = (_: IpcMainInvokeEvent, request: unknown) => - pipeline(request, tentativeParseJson, handler, tentativeStringifyJson); + const nativeHandleCallback = (_: IpcMainInvokeEvent, request: unknown) => toJS(handler(request)); ipcMain.handle(channel.id, nativeHandleCallback); diff --git a/src/main/utils/channel/message-to-channel.injectable.ts b/src/main/utils/channel/message-to-channel.injectable.ts index cbcdc2badd..f45bf5cde0 100644 --- a/src/main/utils/channel/message-to-channel.injectable.ts +++ b/src/main/utils/channel/message-to-channel.injectable.ts @@ -4,9 +4,8 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import { messageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token"; -import type { MessageChannel } from "../../../common/utils/channel/message-channel-injection-token"; -import { tentativeStringifyJson } from "../../../common/utils/tentative-stringify-json"; import getVisibleWindowsInjectable from "../../start-main-application/lens-window/get-visible-windows.injectable"; +import type { MessageToChannel } from "../../../common/utils/channel/message-to-channel-injection-token"; const messageToChannelInjectable = getInjectable({ id: "message-to-channel", @@ -14,15 +13,14 @@ const messageToChannelInjectable = getInjectable({ instantiate: (di) => { const getVisibleWindows = di.inject(getVisibleWindowsInjectable); - // TODO: Figure out way to improve typing in internals - // Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly. - return (channel: MessageChannel, message?: unknown) => { - const stringifiedMessage = tentativeStringifyJson(message); - - getVisibleWindows().forEach((lensWindow) => - lensWindow.send({ channel: channel.id, data: stringifiedMessage ? [stringifiedMessage] : [] }), - ); - }; + return ((channel, message) => { + for (const window of getVisibleWindows()) { + window.send({ + channel: channel.id, + data: message !== undefined ? [message] : [], + }); + } + }) as MessageToChannel; }, injectionToken: messageToChannelInjectionToken, diff --git a/src/renderer/api/create-terminal-api.injectable.ts b/src/renderer/api/create-terminal-api.injectable.ts index 255fa396f3..80b1727d0b 100644 --- a/src/renderer/api/create-terminal-api.injectable.ts +++ b/src/renderer/api/create-terminal-api.injectable.ts @@ -5,6 +5,7 @@ import { getInjectable } from "@ogre-tools/injectable"; import assert from "assert"; import hostedClusterIdInjectable from "../cluster-frame-context/hosted-cluster-id.injectable"; +import getShellAuthTokenInjectable from "../../common/shell-authentication/get-auth-token.injectable"; import type { TerminalApiQuery } from "./terminal-api"; import { TerminalApi } from "./terminal-api"; @@ -14,12 +15,14 @@ const createTerminalApiInjectable = getInjectable({ id: "create-terminal-api", instantiate: (di): CreateTerminalApi => { const hostedClusterId = di.inject(hostedClusterIdInjectable); + const getShellAuthToken = di.inject(getShellAuthTokenInjectable); return (query) => { assert(hostedClusterId, "Can only create terminal APIs within a cluster frame"); return new TerminalApi({ hostedClusterId, + getShellAuthToken, }, query); }; }, diff --git a/src/renderer/api/terminal-api.ts b/src/renderer/api/terminal-api.ts index 0d68526e22..855ce1503f 100644 --- a/src/renderer/api/terminal-api.ts +++ b/src/renderer/api/terminal-api.ts @@ -71,6 +71,8 @@ export class TerminalApi extends WebSocketApi { clusterId: this.dependencies.hostedClusterId, tabId: this.query.id, }); + + console.log("authTokenArray", authTokenArray); const { hostname, protocol, port } = location; const socketUrl = url.format({ protocol: protocol.includes("https") ? "wss" : "ws", diff --git a/src/renderer/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts b/src/renderer/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts index 6d76e35340..8ecd68ceab 100644 --- a/src/renderer/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts +++ b/src/renderer/utils/channel/channel-listeners/enlist-message-channel-listener.injectable.ts @@ -6,8 +6,6 @@ import ipcRendererInjectable from "../ipc-renderer.injectable"; import { getInjectable } from "@ogre-tools/injectable"; import type { IpcRendererEvent } from "electron"; import { enlistMessageChannelListenerInjectionToken } from "../../../../common/utils/channel/enlist-message-channel-listener-injection-token"; -import { tentativeParseJson } from "../../../../common/utils/tentative-parse-json"; -import { pipeline } from "@ogre-tools/fp"; const enlistMessageChannelListenerInjectable = getInjectable({ id: "enlist-message-channel-listener-for-renderer", @@ -16,13 +14,7 @@ const enlistMessageChannelListenerInjectable = getInjectable({ const ipcRenderer = di.inject(ipcRendererInjectable); return ({ channel, handler }) => { - const nativeCallback = (_: IpcRendererEvent, message: unknown) => { - pipeline( - message, - tentativeParseJson, - handler, - ); - }; + const nativeCallback = (_: IpcRendererEvent, message: unknown) => handler(message); ipcRenderer.on(channel.id, nativeCallback); diff --git a/src/renderer/utils/channel/message-to-channel.injectable.ts b/src/renderer/utils/channel/message-to-channel.injectable.ts index 3e493fd322..4738cb05e3 100644 --- a/src/renderer/utils/channel/message-to-channel.injectable.ts +++ b/src/renderer/utils/channel/message-to-channel.injectable.ts @@ -3,9 +3,9 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; +import type { MessageToChannel } from "../../../common/utils/channel/message-to-channel-injection-token"; import { messageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token"; import sendToMainInjectable from "./send-to-main.injectable"; -import type { MessageChannel } from "../../../common/utils/channel/message-channel-injection-token"; const messageToChannelInjectable = getInjectable({ id: "message-to-channel", @@ -13,11 +13,9 @@ const messageToChannelInjectable = getInjectable({ instantiate: (di) => { const sendToMain = di.inject(sendToMainInjectable); - // TODO: Figure out way to improve typing in internals - // Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly. - return (channel: MessageChannel, message?: unknown) => { + return ((channel, message) => { sendToMain(channel.id, message); - }; + }) as MessageToChannel; }, injectionToken: messageToChannelInjectionToken, diff --git a/src/renderer/utils/channel/request-from-channel.injectable.ts b/src/renderer/utils/channel/request-from-channel.injectable.ts index f77287a2ed..a01fe71ec7 100644 --- a/src/renderer/utils/channel/request-from-channel.injectable.ts +++ b/src/renderer/utils/channel/request-from-channel.injectable.ts @@ -5,9 +5,7 @@ import { getInjectable } from "@ogre-tools/injectable"; import ipcRendererInjectable from "./ipc-renderer.injectable"; import { requestFromChannelInjectionToken } from "../../../common/utils/channel/request-from-channel-injection-token"; -import { pipeline } from "@ogre-tools/fp"; -import { tentativeStringifyJson } from "../../../common/utils/tentative-stringify-json"; -import { tentativeParseJson } from "../../../common/utils/tentative-parse-json"; +import type { RequestChannel } from "../../../common/utils/channel/request-channel-injection-token"; const requestFromChannelInjectable = getInjectable({ id: "request-from-channel", @@ -15,13 +13,11 @@ const requestFromChannelInjectable = getInjectable({ instantiate: (di) => { const ipcRenderer = di.inject(ipcRendererInjectable); - return async (channel, ...[request]) => - await pipeline( - request, - tentativeStringifyJson, - (req) => ipcRenderer.invoke(channel.id, req), - tentativeParseJson, - ); + return async (channel, ...[request]) => { + const { id } = channel as unknown as RequestChannel; + + return ipcRenderer.invoke(id, request); + }; }, injectionToken: requestFromChannelInjectionToken, diff --git a/src/renderer/utils/channel/send-to-main.injectable.ts b/src/renderer/utils/channel/send-to-main.injectable.ts index 0811a78798..eb05ee598e 100644 --- a/src/renderer/utils/channel/send-to-main.injectable.ts +++ b/src/renderer/utils/channel/send-to-main.injectable.ts @@ -3,9 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import type { JsonValue } from "type-fest"; import ipcRendererInjectable from "./ipc-renderer.injectable"; -import { tentativeStringifyJson } from "../../../common/utils/tentative-stringify-json"; const sendToMainInjectable = getInjectable({ id: "send-to-main", @@ -13,11 +11,8 @@ const sendToMainInjectable = getInjectable({ instantiate: (di) => { const ipcRenderer = di.inject(ipcRendererInjectable); - // TODO: Figure out way to improve typing in internals - return (channelId: string, message: JsonValue extends T ? T : never ) => { - const stringifiedMessage = tentativeStringifyJson(message); - - ipcRenderer.send(channelId, ...(stringifiedMessage ? [stringifiedMessage] : [])); + return (channelId: string, message: unknown) => { + ipcRenderer.send(channelId, message); }; }, }); diff --git a/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts b/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts index d49ea9dd9c..00899bb7d5 100644 --- a/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts +++ b/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts @@ -20,6 +20,11 @@ const provideInitialValuesForSyncBoxesInjectable = getInjectable({ run: async () => { const initialValues = await requestFromChannel(syncBoxInitialValueChannel); + console.log({ + initialValues, + type: typeof initialValues, + }); + initialValues.forEach(({ id, value }) => { setSyncBoxState(id, value); }); diff --git a/src/test-utils/channel-fakes/override-messaging-from-main-to-window.ts b/src/test-utils/channel-fakes/override-messaging-from-main-to-window.ts index 6ade9b7f0d..171ac804f1 100644 --- a/src/test-utils/channel-fakes/override-messaging-from-main-to-window.ts +++ b/src/test-utils/channel-fakes/override-messaging-from-main-to-window.ts @@ -9,7 +9,6 @@ import type { SendToViewArgs } from "../../main/start-main-application/lens-wind import enlistMessageChannelListenerInjectableInRenderer from "../../renderer/utils/channel/channel-listeners/enlist-message-channel-listener.injectable"; import type { DiContainer } from "@ogre-tools/injectable"; import assert from "assert"; -import { tentativeParseJson } from "../../common/utils/tentative-parse-json"; export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => { const messageChannelListenerFakesForRenderer = new Map< @@ -48,9 +47,7 @@ export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => { ); } - const message = tentativeParseJson(data[0]); - - listeners.forEach((listener) => listener.handler(message)); + listeners.forEach((listener) => listener.handler(data[0])); }, ); diff --git a/src/test-utils/channel-fakes/override-requesting-from-window-to-main.ts b/src/test-utils/channel-fakes/override-requesting-from-window-to-main.ts index d0c3183075..125af6647b 100644 --- a/src/test-utils/channel-fakes/override-requesting-from-window-to-main.ts +++ b/src/test-utils/channel-fakes/override-requesting-from-window-to-main.ts @@ -44,11 +44,12 @@ export const overrideRequestingFromWindowToMain = (mainDi: DiContainer) => { requestFromChannelInjectable, () => async (channel, ...[request]) => { - const requestListener = requestChannelListenerFakesForMain.get(channel.id); + const { id } = channel as unknown as RequestChannel; + const requestListener = requestChannelListenerFakesForMain.get(id); if (!requestListener) { throw new Error( - `Tried to get value from channel "${channel.id}", but no listeners were registered`, + `Tried to get value from channel "${id}", but no listeners were registered`, ); }