From 4fbdace4465cfa851ea20d2a283c0b883bed0e8a Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 1 Mar 2022 22:41:44 +0200 Subject: [PATCH] add.. something..... --- out/_/jd3ymgfg3gk6vrlaxhbn/GatewayClient.js | 105 +++++++++++ out/_/jd3ymgfg3gk6vrlaxhbn/app.js | 21 +++ out/_/jd3ymgfg3gk6vrlaxhbn/components.js | 185 ++++++++++++++++++++ out/_/jd3ymgfg3gk6vrlaxhbn/index.html | 23 +++ out/_/jd3ymgfg3gk6vrlaxhbn/moduleruntime.js | 26 +++ 5 files changed, 360 insertions(+) create mode 100644 out/_/jd3ymgfg3gk6vrlaxhbn/GatewayClient.js create mode 100644 out/_/jd3ymgfg3gk6vrlaxhbn/app.js create mode 100644 out/_/jd3ymgfg3gk6vrlaxhbn/components.js create mode 100644 out/_/jd3ymgfg3gk6vrlaxhbn/index.html create mode 100644 out/_/jd3ymgfg3gk6vrlaxhbn/moduleruntime.js diff --git a/out/_/jd3ymgfg3gk6vrlaxhbn/GatewayClient.js b/out/_/jd3ymgfg3gk6vrlaxhbn/GatewayClient.js new file mode 100644 index 0000000..8c594a0 --- /dev/null +++ b/out/_/jd3ymgfg3gk6vrlaxhbn/GatewayClient.js @@ -0,0 +1,105 @@ +window.modules.register("GatewayClient", () => { + const messageSchema = { t: "number", d: "object" }; + const messageTypes = { + HELLO: 0, + YOO: 1, + READY: 2, + EVENT: 3 + }; + + class GatewayClient { + constructor(gatewayPath) { + this.gatewayPath = gatewayPath; + this.ws = null; + this.token = null; + this.user = null; + this.onEvent = (e) => {}; + this.onConnected = () => {}; + this.onDisconnected = () => {}; + } + + connect(token) { + if (!token) + token = this.token; + + console.log("gateway: connecting"); + + this.ws = new WebSocket(this.gatewayPath); + + this.ws.addEventListener("message", ({ data }) => { + if (typeof data !== "string") { + console.warn("gateway: got non-string data from server, ignoring..."); + return; + } + + let message; + try { + message = JSON.parse(data); + } catch(e) { + console.warn("gateway: got invalid JSON from server (failed to parse), ignoring..."); + return; + } + + if (!this._checkMessageSchema(message)) { + console.warn("gateway: got invalid JSON from server (does not match schema), ignoring..."); + return; + } + + switch (message.t) { + case messageTypes.HELLO: { + console.log("gateway: HELLO"); + this.ws.send(JSON.stringify({ + t: messageTypes.YOO, + d: { + token + } + })); + break; + } + case messageTypes.READY: { + console.log("gateway: READY"); + this.user = message.d.user; + this.onConnected(); + break; + } + case messageTypes.EVENT: { + this.onEvent(message.d); + break; + } + default: { + console.warn("gateway: got invalid JSON from server (invalid type), ignoring..."); + return; + } + } + }); + this.ws.addEventListener("open", () => { + console.log("gateway: open"); + }); + this.ws.addEventListener("close", ({ code }) => { + console.log("gateway: closed"); + this.onDisconnected(code); + if (code === 4001) { + console.log(`gateway: disconnect code is ${code} (bad auth), will not attempt reconnect`); + return; + } + setTimeout(() => { + console.log("gateway: reconnecting"); + this.connect(token); + }, 4000); + }); + } + + _checkMessageSchema(message) { + for (const [key, value] of Object.entries(message)) { + if (!messageSchema[key]) + return false; + + if (typeof value !== messageSchema[key]) + return false; + } + return true; + } + } + + return GatewayClient; +}); diff --git a/out/_/jd3ymgfg3gk6vrlaxhbn/app.js b/out/_/jd3ymgfg3gk6vrlaxhbn/app.js new file mode 100644 index 0000000..83d72df --- /dev/null +++ b/out/_/jd3ymgfg3gk6vrlaxhbn/app.js @@ -0,0 +1,21 @@ +window.modules.register("$app", () => { + const App = window.modules.require("App"); + + if (!window._APP_ENV) + throw new Error("$app: could not find window._APP_ENV"); + if (!App) + throw new Error("$app: require('App') returned undefined"); + + const initialLoading = document.getElementById("initial-loading"); + if (initialLoading) { + initialLoading.parentElement.removeChild(initialLoading); + } + + const appMountElement = document.createElement("div"); + document.body.appendChild(appMountElement); + + const app = new App(appMountElement); + app.mount(); + + return app; +}); diff --git a/out/_/jd3ymgfg3gk6vrlaxhbn/components.js b/out/_/jd3ymgfg3gk6vrlaxhbn/components.js new file mode 100644 index 0000000..db9f1bb --- /dev/null +++ b/out/_/jd3ymgfg3gk6vrlaxhbn/components.js @@ -0,0 +1,185 @@ +window.modules.register("AuthPromptRoute", () => { + class AuthPromptRoute { + constructor() { + this.element = null; + } + + mountOn(target) { + if (this.element) + return; // Already mounted + + this.element = document.createRange().createContextualFragment(` +
loading
+ + + + + + + \ No newline at end of file diff --git a/out/_/jd3ymgfg3gk6vrlaxhbn/moduleruntime.js b/out/_/jd3ymgfg3gk6vrlaxhbn/moduleruntime.js new file mode 100644 index 0000000..ebb5905 --- /dev/null +++ b/out/_/jd3ymgfg3gk6vrlaxhbn/moduleruntime.js @@ -0,0 +1,26 @@ +const modules = { + _cache: {}, + _registry: {}, + require(moduleName) { + if (this._cache[moduleName]) { + return this._cache[moduleName]; + } + + if (this._registry[moduleName]) { + const loaderFunction = this._registry[moduleName]; + this._cache[moduleName] = loaderFunction(1); + return this._cache[moduleName]; + } + + return null; + }, + register(moduleName, loaderFunction) { + this._registry[moduleName] = loaderFunction; + this._cache[moduleName] = loaderFunction(0); + }, + registerLazy(moduleName, loaderFunction) { + this._registry[moduleName] = loaderFunction; + } +}; + +window.modules = modules;