2023-07-19 09:18:16 +00:00
|
|
|
import type { KernelAuthStatus } from "@lumeweb/libweb";
|
2023-07-25 15:46:40 +00:00
|
|
|
import defer from "p-defer";
|
|
|
|
import type { DeferredPromise } from "p-defer";
|
2023-07-20 16:41:46 +00:00
|
|
|
import Emittery from "emittery";
|
2023-07-20 16:42:38 +00:00
|
|
|
import objectDiff from "object-diff";
|
|
|
|
|
2023-07-19 09:18:32 +00:00
|
|
|
let queriesNonce = 1;
|
|
|
|
let queries: any = {};
|
|
|
|
let portsNonce = 0;
|
|
|
|
let openPorts = {} as any;
|
|
|
|
let timer = 20000;
|
2022-08-14 18:36:26 +00:00
|
|
|
|
|
|
|
let authStatus: KernelAuthStatus;
|
|
|
|
let authStatusKnown = false;
|
2023-07-19 09:17:42 +00:00
|
|
|
let authStatusDefer = defer();
|
2022-08-22 05:49:58 +00:00
|
|
|
let kernelFrame: HTMLIFrameElement;
|
2023-04-09 23:43:10 +00:00
|
|
|
let blockForDnsSetup = defer();
|
2023-07-19 09:06:08 +00:00
|
|
|
let loggedInDefer = defer();
|
2023-07-25 15:47:34 +00:00
|
|
|
let booted = defer();
|
2023-07-20 16:41:46 +00:00
|
|
|
export const events = new Emittery();
|
2022-08-14 18:36:26 +00:00
|
|
|
|
|
|
|
export function getAuthStatusKnown() {
|
|
|
|
return authStatusKnown;
|
|
|
|
}
|
|
|
|
export function setAuthStatusKnown(status: boolean) {
|
|
|
|
authStatusKnown = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAuthStatus(): KernelAuthStatus {
|
|
|
|
return authStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setAuthStatus(status: KernelAuthStatus) {
|
2023-07-20 16:42:38 +00:00
|
|
|
let diff;
|
|
|
|
if (!authStatus) {
|
|
|
|
diff = status;
|
|
|
|
} else {
|
|
|
|
diff = objectDiff(authStatus, status);
|
|
|
|
}
|
|
|
|
|
2022-08-14 18:36:26 +00:00
|
|
|
authStatus = status;
|
2023-07-20 16:42:38 +00:00
|
|
|
events.emit("authStatus", diff);
|
2022-08-14 18:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-08-22 05:49:58 +00:00
|
|
|
|
2023-04-09 23:43:10 +00:00
|
|
|
export function getDnsSetupDefer(): DeferredPromise<any> {
|
2022-08-22 05:52:10 +00:00
|
|
|
return blockForDnsSetup;
|
|
|
|
}
|
2022-08-22 05:49:58 +00:00
|
|
|
|
2023-07-19 09:17:42 +00:00
|
|
|
export function getAuthStatusDefer() {
|
|
|
|
return authStatusDefer;
|
2022-08-22 05:49:58 +00:00
|
|
|
}
|
2023-07-17 14:44:21 +00:00
|
|
|
|
2023-07-19 09:06:08 +00:00
|
|
|
export function getLoggedInDefer() {
|
|
|
|
return loggedInDefer;
|
|
|
|
}
|
|
|
|
export function resetLoggedInDefer() {
|
|
|
|
loggedInDefer = defer();
|
|
|
|
}
|
2023-07-25 15:47:34 +00:00
|
|
|
export function getBooted(): Promise<unknown> {
|
|
|
|
return booted.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resetBooted(): void {
|
|
|
|
booted = defer();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function weAreBooted(): void {
|
|
|
|
booted.resolve();
|
|
|
|
}
|