*Major refactor to store global state behind getter and setters to be shared between files
This commit is contained in:
parent
0274210bfb
commit
c9b0daf053
|
@ -7,39 +7,48 @@ import ServerProvider from "../contentProviders/serverProvider.js";
|
||||||
import { init } from "libkernel";
|
import { init } from "libkernel";
|
||||||
import IpfsProvider from "../contentProviders/ipfsProvider.js";
|
import IpfsProvider from "../contentProviders/ipfsProvider.js";
|
||||||
import { ready as dnsReady } from "@lumeweb/kernel-dns-client";
|
import { ready as dnsReady } from "@lumeweb/kernel-dns-client";
|
||||||
|
import {
|
||||||
declare var browser: any; // tsc
|
addQuery,
|
||||||
let queriesNonce = 1;
|
authStatusResolve,
|
||||||
let queries: any = {};
|
blockForBootloader,
|
||||||
let portsNonce = 0;
|
blockForBridge,
|
||||||
let openPorts = {} as any;
|
bridgeLoadedResolve,
|
||||||
let timer = 20000;
|
clearOpenPorts,
|
||||||
|
deleteOpenPort,
|
||||||
|
deleteQuery,
|
||||||
|
getAuthStatus,
|
||||||
|
getAuthStatusKnown,
|
||||||
|
getKernelIframe,
|
||||||
|
getOpenPorts,
|
||||||
|
getPortsNonce,
|
||||||
|
getQueries,
|
||||||
|
getQueriesNonce,
|
||||||
|
getQuery,
|
||||||
|
getTimer,
|
||||||
|
increasePortsNonce,
|
||||||
|
increaseQueriesNonce,
|
||||||
|
kernelFrame,
|
||||||
|
setAuthStatus,
|
||||||
|
setAuthStatusKnown,
|
||||||
|
setKernelIframe,
|
||||||
|
setOpenPort,
|
||||||
|
setTimer,
|
||||||
|
} from "./vars.js";
|
||||||
|
import browser from "@lumeweb/webextension-polyfill";
|
||||||
|
|
||||||
function logLargeObjects() {
|
function logLargeObjects() {
|
||||||
let queriesLen = Object.keys(queries).length;
|
let queriesLen = Object.keys(getQueries()).length;
|
||||||
let portsLen = Object.keys(openPorts).length;
|
let portsLen = Object.keys(getOpenPorts()).length;
|
||||||
if (queriesLen > 500) {
|
if (queriesLen > 500) {
|
||||||
console.error("queries appears to be leaking:", queriesLen);
|
console.error("queries appears to be leaking:", queriesLen);
|
||||||
}
|
}
|
||||||
if (portsLen > 50) {
|
if (portsLen > 50) {
|
||||||
console.error("ports appears to be leaking:", portsLen);
|
console.error("ports appears to be leaking:", portsLen);
|
||||||
}
|
}
|
||||||
timer *= 1.25;
|
setTimer(getTimer() * 1.25);
|
||||||
setTimeout(logLargeObjects, timer);
|
setTimeout(logLargeObjects, getTimer());
|
||||||
}
|
}
|
||||||
setTimeout(logLargeObjects, timer);
|
setTimeout(logLargeObjects, getTimer());
|
||||||
|
|
||||||
export 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;
|
|
||||||
});
|
|
||||||
let kernelFrame: HTMLIFrameElement;
|
|
||||||
|
|
||||||
export function queryKernel(query: any): Promise<any> {
|
export function queryKernel(query: any): Promise<any> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
@ -48,10 +57,10 @@ export function queryKernel(query: any): Promise<any> {
|
||||||
};
|
};
|
||||||
|
|
||||||
blockForBootloader.then(() => {
|
blockForBootloader.then(() => {
|
||||||
let nonce = queriesNonce;
|
let nonce = getQueriesNonce();
|
||||||
queriesNonce += 1;
|
increaseQueriesNonce();
|
||||||
query.nonce = nonce;
|
query.nonce = nonce;
|
||||||
queries[nonce] = receiveResponse;
|
addQuery(nonce, receiveResponse);
|
||||||
if (kernelFrame.contentWindow !== null) {
|
if (kernelFrame.contentWindow !== null) {
|
||||||
kernelFrame.contentWindow.postMessage(query, "http://kernel.skynet");
|
kernelFrame.contentWindow.postMessage(query, "http://kernel.skynet");
|
||||||
} else {
|
} else {
|
||||||
|
@ -67,7 +76,7 @@ function handleKernelMessage(event: MessageEvent) {
|
||||||
|
|
||||||
if (event.data.method === "kernelBridgeVersion") {
|
if (event.data.method === "kernelBridgeVersion") {
|
||||||
blockForBridge.then(() => {
|
blockForBridge.then(() => {
|
||||||
for (let [, port] of Object.entries(openPorts)) {
|
for (let [, port] of Object.entries(getOpenPorts())) {
|
||||||
try {
|
try {
|
||||||
(port as any).postMessage(event.data);
|
(port as any).postMessage(event.data);
|
||||||
} catch {}
|
} catch {}
|
||||||
|
@ -91,17 +100,17 @@ function handleKernelMessage(event: MessageEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.data.method === "kernelAuthStatus") {
|
if (event.data.method === "kernelAuthStatus") {
|
||||||
authStatus = data;
|
setAuthStatus(data);
|
||||||
if (authStatusKnown === false) {
|
if (getAuthStatusKnown() === false) {
|
||||||
authStatusResolve();
|
authStatusResolve();
|
||||||
authStatusKnown = true;
|
setAuthStatusKnown(true);
|
||||||
console.log("bootloader is now initialized");
|
console.log("bootloader is now initialized");
|
||||||
if (authStatus.loginComplete !== true) {
|
if (getAuthStatus().loginComplete !== true) {
|
||||||
console.log("user is not logged in: waiting until login is confirmed");
|
console.log("user is not logged in: waiting until login is confirmed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let [, port] of Object.entries(openPorts)) {
|
for (let [, port] of Object.entries(getOpenPorts())) {
|
||||||
try {
|
try {
|
||||||
(port as any).postMessage(event.data);
|
(port as any).postMessage(event.data);
|
||||||
} catch {}
|
} catch {}
|
||||||
|
@ -110,24 +119,24 @@ function handleKernelMessage(event: MessageEvent) {
|
||||||
if (data.logoutComplete === true) {
|
if (data.logoutComplete === true) {
|
||||||
console.log("received logout signal, clearing all ports");
|
console.log("received logout signal, clearing all ports");
|
||||||
|
|
||||||
for (let [, port] of Object.entries(openPorts)) {
|
for (let [, port] of Object.entries(getOpenPorts())) {
|
||||||
try {
|
try {
|
||||||
(port as any).disconnect();
|
(port as any).disconnect();
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
openPorts = {};
|
clearOpenPorts();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(event.data.nonce in queries)) {
|
if (!(event.data.nonce in getQueries())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let receiveResult = queries[event.data.nonce];
|
let receiveResult = getQuery(event.data.nonce);
|
||||||
if (event.data.method === "response") {
|
if (event.data.method === "response") {
|
||||||
delete queries[event.data.nonce];
|
deleteQuery(event.data.nonce);
|
||||||
}
|
}
|
||||||
|
|
||||||
receiveResult(event.data);
|
receiveResult(event.data);
|
||||||
|
@ -154,22 +163,22 @@ function handleBridgeMessage(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.method !== "queryUpdate") {
|
if (data.method !== "queryUpdate") {
|
||||||
queries[data.nonce] = (response: any) => {
|
addQuery(data.nonce, (response: any) => {
|
||||||
if (portNonce in openPorts) {
|
if (portNonce in getOpenPorts()) {
|
||||||
port.postMessage(response);
|
port.postMessage(response);
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
data["domain"] = domain;
|
data["domain"] = domain;
|
||||||
}
|
}
|
||||||
kernelFrame.contentWindow!.postMessage(data, "http://kernel.skynet");
|
kernelFrame.contentWindow!.postMessage(data, "http://kernel.skynet");
|
||||||
}
|
}
|
||||||
function bridgeListener(port: any) {
|
function bridgeListener(port: any) {
|
||||||
let portNonce = portsNonce;
|
let portNonce = getPortsNonce();
|
||||||
portsNonce++;
|
increasePortsNonce();
|
||||||
openPorts[portNonce] = port;
|
setOpenPort(portNonce, port);
|
||||||
|
|
||||||
port.onDisconnect.addListener(() => {
|
port.onDisconnect.addListener(() => {
|
||||||
delete openPorts[portNonce];
|
deleteOpenPort(portNonce);
|
||||||
});
|
});
|
||||||
|
|
||||||
let domain = new URL(port.sender.url).hostname;
|
let domain = new URL(port.sender.url).hostname;
|
||||||
|
@ -181,7 +190,7 @@ function bridgeListener(port: any) {
|
||||||
blockForBootloader.then(() => {
|
blockForBootloader.then(() => {
|
||||||
port.postMessage({
|
port.postMessage({
|
||||||
method: "kernelAuthStatus",
|
method: "kernelAuthStatus",
|
||||||
data: authStatus,
|
data: getAuthStatus(),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -198,10 +207,10 @@ async function boot() {
|
||||||
engine.registerContentProvider(new IpfsProvider(engine));
|
engine.registerContentProvider(new IpfsProvider(engine));
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
kernelFrame = document.createElement("iframe");
|
setKernelIframe(document.createElement("iframe"));
|
||||||
kernelFrame.src = "http://kernel.skynet";
|
getKernelIframe().src = "http://kernel.skynet";
|
||||||
kernelFrame.onload = init;
|
getKernelIframe().onload = init;
|
||||||
document.body.appendChild(kernelFrame);
|
document.body.appendChild(getKernelIframe());
|
||||||
|
|
||||||
await dnsReady();
|
await dnsReady();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,95 @@
|
||||||
|
import type { DataFn, KernelAuthStatus } from "libskynet";
|
||||||
|
|
||||||
|
export let queriesNonce = 1;
|
||||||
|
export let queries: any = {};
|
||||||
|
export let portsNonce = 0;
|
||||||
|
export let openPorts = {} as any;
|
||||||
|
export let timer = 20000;
|
||||||
|
|
||||||
|
let authStatus: KernelAuthStatus;
|
||||||
|
let authStatusKnown = false;
|
||||||
|
export let authStatusResolve: DataFn;
|
||||||
|
export let bridgeLoadedResolve: DataFn;
|
||||||
|
export let blockForBootloader = new Promise((resolve) => {
|
||||||
|
authStatusResolve = resolve;
|
||||||
|
});
|
||||||
|
export let blockForBridge = new Promise((resolve) => {
|
||||||
|
bridgeLoadedResolve = resolve;
|
||||||
|
});
|
||||||
|
export let kernelFrame: HTMLIFrameElement;
|
||||||
|
|
||||||
|
export function getAuthStatusKnown() {
|
||||||
|
return authStatusKnown;
|
||||||
|
}
|
||||||
|
export function setAuthStatusKnown(status: boolean) {
|
||||||
|
authStatusKnown = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAuthStatus(): KernelAuthStatus {
|
||||||
|
return authStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setAuthStatus(status: KernelAuthStatus) {
|
||||||
|
authStatus = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getQueriesNonce(): number {
|
||||||
|
return queriesNonce;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function increaseQueriesNonce() {
|
||||||
|
queriesNonce++;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getTimer(): number {
|
||||||
|
return timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setTimer(interval: number) {
|
||||||
|
timer = interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getOpenPorts() {
|
||||||
|
return openPorts;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setOpenPort(port: number, value: any) {
|
||||||
|
openPorts[port] = value;
|
||||||
|
}
|
||||||
|
export function deleteOpenPort(port: number) {
|
||||||
|
delete openPorts[port];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPortsNonce(): number {
|
||||||
|
return portsNonce;
|
||||||
|
}
|
||||||
|
export function increasePortsNonce() {
|
||||||
|
portsNonce++;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function addQuery(nonce: any, func: Function) {
|
||||||
|
queries[nonce] = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getQueries() {
|
||||||
|
return queries;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getQuery(nonce: any) {
|
||||||
|
return queries[nonce];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deleteQuery(nonce: any) {
|
||||||
|
delete queries[nonce];
|
||||||
|
}
|
||||||
|
export function clearOpenPorts() {
|
||||||
|
openPorts = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getKernelIframe(): HTMLIFrameElement {
|
||||||
|
return kernelFrame;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setKernelIframe(iframe: HTMLIFrameElement) {
|
||||||
|
kernelFrame = iframe;
|
||||||
|
}
|
Reference in New Issue