refactor: move handleIncomingMessage to its own file
This commit is contained in:
parent
7d9c61e795
commit
aefc9755cd
|
@ -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();
|
||||
|
|
|
@ -15,6 +15,7 @@ import {
|
|||
setUserKey,
|
||||
} from "./vars.js";
|
||||
import { getStoredUserKey } from "./storage.js";
|
||||
import { handleIncomingMessage } from "./messages.js";
|
||||
|
||||
export function boot() {
|
||||
let userKey;
|
||||
|
|
|
@ -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;
|
||||
}
|
Reference in New Issue