Convert shell authentication IPC to injectable RequestChannel

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2022-07-06 11:42:46 -04:00
1 parent 78ead4f2b9
commit ea8c262d3c
20 files changed
+54 -110

No files matched your search

@@ -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);
};
},
+2
View File
@@ -71,6 +71,8 @@ export class TerminalApi extends WebSocketApi<TerminalEvents> {
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",
@@ -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);
@@ -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<any>, message?: unknown) => {
return ((channel, message) => {
sendToMain(channel.id, message);
};
}) as MessageToChannel;
},
injectionToken: messageToChannelInjectionToken,
@@ -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<any, any>;
return ipcRenderer.invoke(id, request);
};
},
injectionToken: requestFromChannelInjectionToken,
@@ -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 <T>(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);
};
},
});
@@ -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);
});