refactor a lot

This commit is contained in:
Seth 2025-05-04 20:47:19 -04:00
parent cf21a56d7a
commit deb223c642
27 changed files with 401 additions and 381 deletions

View file

@ -0,0 +1,88 @@
import ReconnectingWebSocket from 'reconnecting-websocket';
export default class {
private _socket: ReconnectingWebSocket;
private _keepAlive: NodeJS.Timeout | null;
private _interval: NodeJS.Timeout | null;
private _callback: (data: number) => void;
constructor(callback: (data: number) => void) {
this._socket = new ReconnectingWebSocket("wss://app.hyperate.io/socket/websocket?token=wv39nM6iyrNJulvpmMQrimYPIXy2dVrYRjkuHpbRapKT2VSh65ngDGHdCdCtmEN9")
this._keepAlive = null;
this._interval = null;
this._callback = callback;
this._socket.binaryType = "arraybuffer";
this._socket.onopen = () => {
console.log("Hyperate socket opened")
this._socket.send(
JSON.stringify({
topic: "hr:84aa0f",
event: "phx_join",
payload: {},
ref: 0,
}),
);
this._keepAlive = setInterval(() => {
this._socket.send(
JSON.stringify({
topic: "phoenix",
event: "heartbeat",
payload: {},
ref: 0,
}),
);
}, 10000);
}
this._socket.onmessage = ({ data }: MessageEvent) => {
data = JSON.parse(data);
switch (data.event) {
case "hr_update": {
this._callback(data.payload.hr);
this.heartbeat();
break;
}
}
}
this._socket.onerror = () => {
console.error("Hyperate socket error");
if (this._keepAlive) {
clearInterval(this._keepAlive);
this._keepAlive = null;
}
if (this._interval) {
clearInterval(this._interval);
this._interval = null;
}
}
this._socket.onclose = () => {
console.log("Hyperate socket closed");
if (this._keepAlive) {
clearInterval(this._keepAlive);
this._keepAlive = null;
}
if (this._interval) {
clearInterval(this._interval);
this._interval = null;
}
}
}
private heartbeat() {
if (this._interval) {
clearTimeout(this._interval);
this._interval = null;
}
this._interval = setTimeout(() => {
this._callback(0);
}, 6000);
}
}

View file

@ -0,0 +1,61 @@
import ReconnectingWebSocket from 'reconnecting-websocket';
export default class {
private _socket: ReconnectingWebSocket;
private _keepAlive: NodeJS.Timeout | null;
private _callback: (data: { [key: string]: string }) => void;
constructor(callback: (data: { [key: string]: string }) => void) {
this._socket = new ReconnectingWebSocket("wss://lanyard.creations.works/socket")
this._keepAlive = null;
this._callback = callback;
this._socket.binaryType = "arraybuffer";
this._socket.onopen = () => {
console.log("Lanyard socket opened")
}
this._socket.onmessage = ({ data }: MessageEvent) => {
data = JSON.parse(data);
switch (data.op) {
case 0: {
this._callback(data.d)
break;
}
case 1: {
this._socket.send(JSON.stringify({
op: 2,
d: {
subscribe_to_id: "1273447359417942128"
}
}))
this._keepAlive = setInterval(() => {
this._socket.send(JSON.stringify({
op: 3
}))
}, data.d.heartbeat_interval)
break;
}
}
}
this._socket.onerror = () => {
console.error("Lanyard socket error");
if (this._keepAlive) {
clearInterval(this._keepAlive);
this._keepAlive = null;
}
}
this._socket.onclose = () => {
console.log("Lanyard socket closed");
if (this._keepAlive) {
clearInterval(this._keepAlive);
this._keepAlive = null;
}
}
}
}

74
src/back/index.ts Normal file
View file

@ -0,0 +1,74 @@
import Hyperate from "./Sockets/Hyperate";
import Lanyard from "./Sockets/Lanyard";
const development = process.env.NODE_ENV === "development";
const build = async () => {
return await Bun.build({
entrypoints: ['./src/front/index.html'],
outdir: './dist',
minify: !development,
sourcemap: (development ? "inline" : "none"),
splitting: true,
publicPath: "/assets/",
})
}
const respOptions = {
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache",
"Content-Encoding": "gzip",
}
}
const okResp = new Response(Bun.gzipSync(JSON.stringify({ data: "ok" })), respOptions)
const Responses = {
ok: () => {
return okResp.clone();
},
json: (data: { [key: string]: string }) => {
return new Response(Bun.gzipSync(JSON.stringify(data)), respOptions);
},
file: async (file: Bun.BunFile) => {
return new Response(Bun.gzipSync(await file.arrayBuffer()), {
headers: {
"Content-Type": file.type,
"Cache-Control": "public, max-age=31536000",
"Content-Encoding": "gzip",
}
});
}
}
const postAnalytics = async (req: Request | Bun.BunRequest, server: Bun.Server) => {
return await fetch("https://plausible.creations.works/api/event", {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": req.headers.get("user-agent") || "",
"X-Forwarded-For": String(
req.headers.get("CF-Connecting-IP") ||
req.headers.get("X-Real-IP") ||
req.headers.get("X-Forwarded-For")?.split(",")[0] ||
(typeof server.requestIP(req) === "string" ? server.requestIP(req) : (server.requestIP(req)?.address || ""))
),
},
body: JSON.stringify({
domain: "ipv4.army",
name: "pageview",
url: req.url,
referrer: req.headers.get("referer") || "",
})
})
}
export default {
Sockets: {
Hyperate,
Lanyard
},
Responses,
build,
development,
postAnalytics,
};