From 1142576ea5b692328c1e09a6b26b429343108eba Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 31 Jul 2022 00:51:02 -0400 Subject: [PATCH] *Solve bridge loading race condition by using a blocking promised, resolved by a bridgeLoaded command from a bridge --- src/main/background.ts | 27 +++++++++++++++++++-------- src/main/bridge.ts | 3 +++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/background.ts b/src/main/background.ts index bcae0b4..32e0582 100644 --- a/src/main/background.ts +++ b/src/main/background.ts @@ -24,9 +24,15 @@ setTimeout(logLargeObjects, timer); let authStatus: KernelAuthStatus; let authStatusKnown = false; let authStatusResolve: DataFn; +let bridgeLoadedResolve: DataFn; let blockForBootloader = new Promise((resolve) => { authStatusResolve = resolve; }); +let blockForBridge = new Promise((resolve) => { + bridgeLoadedResolve = resolve; +}); + +tldEnum.list.push("localhost"); export function queryKernel(query: any): Promise { return new Promise((resolve) => { @@ -50,17 +56,16 @@ export function queryKernel(query: any): Promise { }); } function handleKernelMessage(event: MessageEvent) { - if (event.origin !== "http://kernel.skynet") { - return; - } let data = event.data.data; if (event.data.method === "kernelBridgeVersion") { - for (let [, port] of Object.entries(openPorts)) { - try { - (port as any).postMessage(event.data); - } catch {} - } + blockForBridge.then(() => { + for (let [, port] of Object.entries(openPorts)) { + try { + (port as any).postMessage(event.data); + } catch {} + } + }); return; } @@ -128,6 +133,11 @@ function handleBridgeMessage( data: any, domain: string ) { + if (data.method === "bridgeLoaded") { + bridgeLoadedResolve(); + return; + } + if (!("nonce" in data)) { return; } @@ -180,4 +190,5 @@ engine.registerContentProvider(new InternalProvider(engine)); // @ts-ignore let kernelFrame: HTMLIFrameElement = document.createElement("iframe"); kernelFrame.src = "http://kernel.skynet"; +kernelFrame.onload = init; document.body.appendChild(kernelFrame); diff --git a/src/main/bridge.ts b/src/main/bridge.ts index 04440e3..0623fe3 100644 --- a/src/main/bridge.ts +++ b/src/main/bridge.ts @@ -130,3 +130,6 @@ function handleMessage(event: MessageEvent) { // Everything else just gets ignored. } window.addEventListener("message", handleMessage); +port.postMessage({ + method: "bridgeLoaded", +});