*Solve bridge loading race condition by using a blocking promised, resolved by a bridgeLoaded command from a bridge

This commit is contained in:
Derrick Hammer 2022-07-31 00:51:02 -04:00
parent f394776a72
commit 1142576ea5
2 changed files with 22 additions and 8 deletions

View File

@ -24,9 +24,15 @@ setTimeout(logLargeObjects, timer);
let authStatus: KernelAuthStatus; let authStatus: KernelAuthStatus;
let authStatusKnown = false; let authStatusKnown = false;
let authStatusResolve: DataFn; let authStatusResolve: DataFn;
let bridgeLoadedResolve: DataFn;
let blockForBootloader = new Promise((resolve) => { let blockForBootloader = new Promise((resolve) => {
authStatusResolve = resolve; authStatusResolve = resolve;
}); });
let blockForBridge = new Promise((resolve) => {
bridgeLoadedResolve = resolve;
});
tldEnum.list.push("localhost");
export function queryKernel(query: any): Promise<any> { export function queryKernel(query: any): Promise<any> {
return new Promise((resolve) => { return new Promise((resolve) => {
@ -50,17 +56,16 @@ export function queryKernel(query: any): Promise<any> {
}); });
} }
function handleKernelMessage(event: MessageEvent) { function handleKernelMessage(event: MessageEvent) {
if (event.origin !== "http://kernel.skynet") {
return;
}
let data = event.data.data; let data = event.data.data;
if (event.data.method === "kernelBridgeVersion") { if (event.data.method === "kernelBridgeVersion") {
blockForBridge.then(() => {
for (let [, port] of Object.entries(openPorts)) { for (let [, port] of Object.entries(openPorts)) {
try { try {
(port as any).postMessage(event.data); (port as any).postMessage(event.data);
} catch {} } catch {}
} }
});
return; return;
} }
@ -128,6 +133,11 @@ function handleBridgeMessage(
data: any, data: any,
domain: string domain: string
) { ) {
if (data.method === "bridgeLoaded") {
bridgeLoadedResolve();
return;
}
if (!("nonce" in data)) { if (!("nonce" in data)) {
return; return;
} }
@ -180,4 +190,5 @@ engine.registerContentProvider(new InternalProvider(engine));
// @ts-ignore // @ts-ignore
let kernelFrame: HTMLIFrameElement = document.createElement("iframe"); let kernelFrame: HTMLIFrameElement = document.createElement("iframe");
kernelFrame.src = "http://kernel.skynet"; kernelFrame.src = "http://kernel.skynet";
kernelFrame.onload = init;
document.body.appendChild(kernelFrame); document.body.appendChild(kernelFrame);

View File

@ -130,3 +130,6 @@ function handleMessage(event: MessageEvent) {
// Everything else just gets ignored. // Everything else just gets ignored.
} }
window.addEventListener("message", handleMessage); window.addEventListener("message", handleMessage);
port.postMessage({
method: "bridgeLoaded",
});