From aefc9755cd2cb6c08464ec16d0e5efe804026ce5 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 18 Jul 2023 15:53:23 -0400 Subject: [PATCH] refactor: move handleIncomingMessage to its own file --- src/main/bootloader.ts | 73 +-------------------------------- src/main/bootloader/kernel.ts | 1 + src/main/bootloader/messages.ts | 72 ++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 72 deletions(-) create mode 100644 src/main/bootloader/messages.ts diff --git a/src/main/bootloader.ts b/src/main/bootloader.ts index a7b133c..5c6e5ce 100644 --- a/src/main/bootloader.ts +++ b/src/main/bootloader.ts @@ -1,6 +1,5 @@ import { boot } from "./bootloader/kernel.js"; -import exchangeCommunicationKeys from "./bootloader/messages/exchangeCommunicationKeys.js"; -import setLoginKey from "./bootloader/messages/setLoginKey.js"; +import { handleIncomingMessage } from "./bootloader/messages.js"; declare var browser: any; // tsc @@ -10,76 +9,6 @@ header.textContent = "Something went wrong! You should not be visiting this page, this page should only be accessed via an invisible iframe."; document.body.appendChild(header); -const kernelMessageHandlers = { - exchangeCommunicationKeys, - setLoginKey, -}; - -async function handleIncomingMessage(event: MessageEvent) { - if (event.source === null) { - return; - } - if (event.source === window) { - return; - } - - if (!("nonce" in event.data)) { - (event.source as WindowProxy).postMessage( - { - nonce: "N/A", - method: "response", - err: "message sent to kernel with no nonce", - }, - event.origin, - ); - return; - } - - if (!("method" in event.data)) { - (event.source as WindowProxy).postMessage( - { - nonce: event.data.nonce, - method: "response", - err: "message sent to kernel with no method", - }, - event.origin, - ); - return; - } - - if (event.data.method in kernelMessageHandlers) { - let response; - - try { - response = await kernelMessageHandlers[event.data.method]( - event.data.data, - ); - } catch (e: any) { - response = { err: (e as Error).message }; - } - - (event.source as WindowProxy).postMessage( - { - nonce: event.data.nonce, - data: response, - }, - event.origin, - ); - return; - } - - (event.source as WindowProxy).postMessage( - { - nonce: event.data.nonce, - method: "response", - err: - "unrecognized method (user may need to log in): " + event.data.method, - }, - event.origin, - ); - return; -} - window.addEventListener("message", handleIncomingMessage); boot(); diff --git a/src/main/bootloader/kernel.ts b/src/main/bootloader/kernel.ts index da51986..cc87b78 100644 --- a/src/main/bootloader/kernel.ts +++ b/src/main/bootloader/kernel.ts @@ -15,6 +15,7 @@ import { setUserKey, } from "./vars.js"; import { getStoredUserKey } from "./storage.js"; +import { handleIncomingMessage } from "./messages.js"; export function boot() { let userKey; diff --git a/src/main/bootloader/messages.ts b/src/main/bootloader/messages.ts new file mode 100644 index 0000000..129ff60 --- /dev/null +++ b/src/main/bootloader/messages.ts @@ -0,0 +1,72 @@ +import exchangeCommunicationKeys from "./messages/exchangeCommunicationKeys.js"; +import setLoginKey from "./messages/setLoginKey.js"; + +const kernelMessageHandlers = { + exchangeCommunicationKeys, + setLoginKey, +}; + +export async function handleIncomingMessage(event: MessageEvent) { + if (event.source === null) { + return; + } + if (event.source === window) { + return; + } + + if (!("nonce" in event.data)) { + (event.source as WindowProxy).postMessage( + { + nonce: "N/A", + method: "response", + err: "message sent to kernel with no nonce", + }, + event.origin, + ); + return; + } + + if (!("method" in event.data)) { + (event.source as WindowProxy).postMessage( + { + nonce: event.data.nonce, + method: "response", + err: "message sent to kernel with no method", + }, + event.origin, + ); + return; + } + + if (event.data.method in kernelMessageHandlers) { + let response; + + try { + response = await kernelMessageHandlers[event.data.method]( + event.data.data, + ); + } catch (e: any) { + response = { err: (e as Error).message }; + } + + (event.source as WindowProxy).postMessage( + { + nonce: event.data.nonce, + data: response, + }, + event.origin, + ); + return; + } + + (event.source as WindowProxy).postMessage( + { + nonce: event.data.nonce, + method: "response", + err: + "unrecognized method (user may need to log in): " + event.data.method, + }, + event.origin, + ); + return; +}