From 20d49560e91fa2f12930cdd0e4dbbea628aa0b81 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=93=8D=BC?= <143974574+xojw@users.noreply.github.com>
Date: Sun, 8 Jun 2025 00:43:31 -0500
Subject: [PATCH] stuff
---
index.mjs | 127 ++++++++++++----------------------
public/$.html | 4 +-
public/assets/css/$.css | 16 +++--
public/assets/js/$.js | 27 +++-----
public/assets/js/greetings.js | 52 +++++++++-----
public/assets/js/ping.js | 8 +--
6 files changed, 103 insertions(+), 131 deletions(-)
diff --git a/index.mjs b/index.mjs
index 0349c739..a2a77ba5 100644
--- a/index.mjs
+++ b/index.mjs
@@ -17,7 +17,6 @@ import wisp from "wisp-server-node";
const surgeConfigPath = path.resolve("surge.config.json");
const isSurgedRun = process.argv.includes("--surged");
-let startTime = Date.now();
function applySurgeAndRestartIfNeeded() {
if (isSurgedRun) {
@@ -48,20 +47,13 @@ applySurgeAndRestartIfNeeded();
if (global.gc) {
setInterval(() => {
const { heapUsed, heapTotal } = process.memoryUsage();
- if (heapTotal > 0 && heapUsed / heapTotal > 0.7) global.gc();
- }, 60000);
+ if (heapTotal > 0 && heapUsed / heapTotal > 0.8) global.gc();
+ }, 120000);
}
import "./others/scaler.mjs";
import "./others/warmup.mjs";
-const cache = new LRUCache({
- maxSize: 1000,
- ttl: 60_000,
- allowStale: false,
- sizeCalculation: (value, key) => Buffer.byteLength(value) + Buffer.byteLength(key)
-});
-
const port = parseInt(process.env.PORT || "3000", 10);
function logInfo(msg) {
@@ -80,9 +72,7 @@ process.on("uncaughtException", err => logError(`Unhandled Exception: ${err}`));
process.on("unhandledRejection", reason => logError(`Unhandled Rejection: ${reason}`));
if (cluster.isPrimary) {
- const cpus = os.cpus().length;
- const workers = Math.max(1, cpus - 1);
-
+ const workers = Math.max(1, os.cpus().length - 1);
logInfo(`Master: forking ${workers} workers`);
for (let i = 0; i < workers; i++) {
@@ -108,18 +98,26 @@ if (cluster.isPrimary) {
const __dirname = process.cwd();
const publicPath = path.join(__dirname, "public");
const app = express();
- let latencySamples = [];
+
+ const cache = new LRUCache({
+ max: 500,
+ ttl: 60_000,
+ allowStale: false
+ });
+
+ const latencySamples = new Array(200);
app.use(compression({ level: 4, memLevel: 4, threshold: 1024 }));
app.use((req, res, next) => {
+ if (req.path.startsWith("/api/")) return next();
const key = req.originalUrl;
const val = cache.get(key);
if (val) {
res.setHeader("X-Cache", "HIT");
return res.send(val);
}
- res.sendResponse = res.send.bind(res);
+ res.sendResponse = res.send;
res.send = body => {
cache.set(key, body);
res.setHeader("X-Cache", "MISS");
@@ -128,13 +126,12 @@ if (cluster.isPrimary) {
next();
});
- const staticOpts = { maxAge: "7d", immutable: true };
+ const staticOpts = { maxAge: "7d", immutable: true, etag: false };
app.use("/baremux/", express.static(baremuxPath, staticOpts));
app.use("/epoxy/", express.static(epoxyPath, staticOpts));
app.use("/libcurl/", express.static(libcurlPath, staticOpts));
app.use(express.static(publicPath, staticOpts));
app.use("/wah/", express.static(uvPath, staticOpts));
- app.use(express.json());
const sendHtml = file => (_req, res) => res.sendFile(path.join(publicPath, file));
@@ -144,75 +141,40 @@ if (cluster.isPrimary) {
app.get("/resent", (_req, res) => res.sendFile(path.join(publicPath, "resent", "index.html")));
app.get("/api/info", (_req, res) => {
- try {
- const average = latencySamples.length
- ? latencySamples.reduce((a, b) => a + b, 0) / latencySamples.length
- : 0;
- let speed = "Medium";
- if (average < 200) speed = "Fast";
- else if (average > 500) speed = "Slow";
- const cpus = os.cpus();
- const totalMem = os.totalmem() / 1024 / 1024 / 1024;
- res.json({
- speed,
- averageLatency: average.toFixed(2),
- specs: `${cpus[0].model} + ${cpus.length} CPU Cores + ${totalMem.toFixed(1)}GB of RAM`,
- startTime,
- samples: latencySamples.length,
- timestamp: Date.now()
- });
- } catch {
- res.status(500).json({ error: "Internal error" });
- }
- });
-
- app.get("/api/latest-commit", async (_req, res) => {
- try {
- const ghRes = await fetch(
- "https://api.github.com/repos/xojw/waves/commits?per_page=1",
- {
- headers: {
- "User-Agent": "waves-app",
- Accept: "application/vnd.github.v3+json"
- }
- }
- );
- if (!ghRes.ok) return res.status(ghRes.status).json({ error: "GitHub API error" });
-
- const commits = await ghRes.json();
- const updates = commits.map(c => ({
- sha: c.sha.slice(0, 7),
- message: c.commit.message.split("\n")[0],
- author: c.commit.author.name,
- date: c.commit.author.date
- }));
-
- res.json({ repo: "xojw/waves", updates });
- } catch {
- res.status(500).json({ error: "Internal server error" });
- }
+ const validSamples = latencySamples.filter(s => s !== undefined);
+ const average = validSamples.length ? validSamples.reduce((a, b) => a + b, 0) / validSamples.length : 0;
+ res.json({
+ speed: average < 200 ? "Fast" : average > 500 ? "Slow" : "Medium",
+ averageLatency: average.toFixed(2),
+ timestamp: Date.now()
+ });
});
app.use((_req, res) => res.status(404).sendFile(path.join(publicPath, "404.html")));
const server = createServer(app);
- server.keepAliveTimeout = 0;
- server.headersTimeout = 0;
+ server.keepAliveTimeout = 5000;
+ server.headersTimeout = 10000;
const pingWSS = new WebSocket.Server({
noServer: true,
- maxPayload: 4 * 1024 * 1024,
+ maxPayload: 16384,
perMessageDeflate: false
});
pingWSS.on("connection", (ws, req) => {
const remote = req.socket.remoteAddress || "unknown";
- let lat = [];
- const interval = setInterval(() => {
+ const lat = [];
+ let sampleIndex = 0;
+
+ const sendPing = () => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: "ping", timestamp: Date.now() }));
}
- }, 1000);
+ };
+
+ const pingInterval = setInterval(sendPing, 500);
+ sendPing();
ws.on("message", msg => {
try {
@@ -220,39 +182,36 @@ if (cluster.isPrimary) {
if (data.type === "pong" && data.timestamp) {
const d = Date.now() - data.timestamp;
lat.push(d);
- if (lat.length > 5) lat.shift();
- latencySamples.push(d);
- if (latencySamples.length > 100) latencySamples.shift();
- ws.send(JSON.stringify({ type: "latency", latency: d }));
+ if (lat.length > 10) lat.shift();
+
+ latencySamples[sampleIndex % latencySamples.length] = d;
+ sampleIndex = (sampleIndex + 1) % latencySamples.length;
+
+ ws.send(JSON.stringify({ type: "latency", latency: d }), { compress: false });
}
- } catch (e) {
- logError(`Ping error: ${e}`);
- }
+ } catch {}
});
ws.on("close", () => {
- clearInterval(interval);
- const avg = lat.length
- ? (lat.reduce((a, b) => a + b) / lat.length).toFixed(2)
- : 0;
+ clearInterval(pingInterval);
+ const avg = lat.length ? (lat.reduce((a, b) => a + b) / lat.length).toFixed(2) : 0;
logInfo(`WS ${remote} closed. Avg: ${avg}ms`);
});
});
server.on("upgrade", (req, sock, head) => {
if (req.url === "/w/ping") {
- pingWSS.handleUpgrade(req, sock, head, ws =>
+ pingWSS.handleUpgrade(req, sock, head, ws =>
pingWSS.emit("connection", ws, req)
);
} else if (req.url.startsWith("/w/")) {
wisp.routeRequest(req, sock, head);
} else {
- sock.end();
+ sock.destroy();
}
});
server.on("error", err => logError(`Worker error: ${err}`));
-
server.listen(0, () => logSuccess(`Worker ${process.pid} ready`));
process.on("message", (msg, conn) => {
diff --git a/public/$.html b/public/$.html
index 0883685c..adb25a62 100644
--- a/public/$.html
+++ b/public/$.html
@@ -114,10 +114,10 @@
-
Ping: Connecting...
+ Ping: Connecting...
- Loading latest commit
+ Hii,
2025
diff --git a/public/assets/css/$.css b/public/assets/css/$.css
index e0d6fb3c..b13cac19 100644
--- a/public/assets/css/$.css
+++ b/public/assets/css/$.css
@@ -55,7 +55,7 @@ body {
color: #ffffff;
display: inline-block;
text-align: left;
- margin: 1px 80px 0 10px;
+ margin: 1px 75px 0 10px;
white-space: nowrap;
cursor: default;
transition: all 0.3s ease;
@@ -88,7 +88,7 @@ body {
.home-navbar .favicon {
width: 28px;
height: 24px;
- margin-right: -10px;
+ margin-right: -9px;
vertical-align: middle;
}
@@ -114,8 +114,8 @@ body {
.home-navbar i {
color: #ffffff;
- margin-right: -15px;
- margin-left: 115px;
+ margin-right: -19px;
+ margin-left: 119px;
}
.navbar {
@@ -590,14 +590,14 @@ body {
animation: fadeOut 0.3s ease-in-out forwards;
}
-#lastest-commit {
+#hi {
position: fixed;
bottom: 10px;
left: 10px;
z-index: 9999;
background-color: #08080894;
border: 1px solid #ffffff21;
- color: #cfcfcf;
+ color: #aaaaaa;
font-weight: 540;
transition: all 0.3s ease;
cursor: default;
@@ -606,6 +606,10 @@ body {
font-size: 14px;
}
+#hi:hover {
+ color: #ffffff;
+}
+
#copyright {
position: fixed;
bottom: 10px;
diff --git a/public/assets/js/$.js b/public/assets/js/$.js
index cd4db44b..6e9659b2 100644
--- a/public/assets/js/$.js
+++ b/public/assets/js/$.js
@@ -141,6 +141,14 @@ document.addEventListener('DOMContentLoaded', () => {
updateDecodedSearchInput();
}
+ function generateSubject() {
+ const subjects = ['math', 'science', 'history', 'art', 'programming', 'philosophy'];
+ const randomSubject = subjects[Math.floor(Math.random() * subjects.length)];
+ try {
+ history.replaceState({}, '', `/learning?subject=${randomSubject}`);
+ } catch (e) {}
+ }
+
function setupIframeNavigationListeners() {
try {
const iframeWindow = iframe.contentWindow;
@@ -209,6 +217,7 @@ document.addEventListener('DOMContentLoaded', () => {
}
}
setupIframeNavigationListeners();
+ generateSubject();
if (navbarToggle && navbarToggle.checked && navBar) {
navBar.style.display = 'block';
}
@@ -466,20 +475,4 @@ document.addEventListener('DOMContentLoaded', () => {
window.APP.decodeUrl = decodeUrl;
window.APP.normalizeUrl = normalizeUrl;
-});
-
-(async () => {
- try {
- const res = await fetch('/api/latest-commit');
- const {
- updates
- } = await res.json();
- const u = updates[0];
- const commitUrl = `https://github.com/xojw/waves/commit/${u.sha}`;
- document.getElementById('lastest-commit').innerHTML =
- `
${u.sha}`;
- } catch {
- document.getElementById('lastest-commit').textContent =
- 'Failed to load lastest commit';
- }
-})();
\ No newline at end of file
+});
\ No newline at end of file
diff --git a/public/assets/js/greetings.js b/public/assets/js/greetings.js
index 14b344e1..a697d495 100644
--- a/public/assets/js/greetings.js
+++ b/public/assets/js/greetings.js
@@ -1,6 +1,7 @@
window.onload = function() {
const storedName = localStorage.getItem('userName');
const path = window.location.pathname;
+
if (!storedName) {
document.getElementById('overlay').style.display = 'block';
document.getElementById('namePrompt').style.display = 'block';
@@ -12,20 +13,27 @@ window.onload = function() {
});
return;
}
+
const welcomeMsg = getWelcomeMessage(storedName);
- const iconType = getIconType(path);
+ const iconType = getIconType(path);
showToast(welcomeMsg, 'success', iconType);
+
const greetingElement = document.getElementById('greeting');
if (greetingElement) {
updateGreeting(storedName);
}
+
+ updateHi(storedName);
};
function submitName() {
const name = document.getElementById('userName').value.trim();
if (!name) return;
+
localStorage.setItem('userName', name);
updateGreeting(name);
+ updateHi(name);
+
document.getElementById('namePrompt').classList.add('fade-out');
showToast(`Hey, ${name}! Welcome to Waves!`, 'success', 'wave');
setTimeout(() => {
@@ -52,8 +60,7 @@ function getIconType(path) {
}
const generalGreetings = [
- { text: 'Welcome aboard', icon: '
', suffix: '!' },
- { text: 'Hii', icon: '
', suffix: '!!' },
+ { text: 'Have fun', icon: '
', suffix: '!' },
{ text: 'Hope you enjoy Waves', icon: '
', suffix: ' <3' },
{ text: 'Consider joining our Discord (discord.gg/ire)', icon: '
', suffix: '!' },
{ text: 'How you doing today', icon: '
', suffix: '?' }
@@ -67,21 +74,18 @@ timeGreetings.push(
{ text: 'Enjoy your morning', icon: '
', suffix: '!' },
{ text: 'Your day starts here', icon: '
', suffix: '!' }
);
-
timeGreetings.push(
{ text: 'Good afternoon', icon: '
', suffix: '!' },
{ text: 'Hope your day is going well', icon: '
', suffix: '.' },
{ text: 'Keep up the pace', icon: '
', suffix: '!' },
{ text: 'Stay on track today', icon: '
', suffix: '.' }
);
-
timeGreetings.push(
{ text: 'Good evening', icon: '
', suffix: '!' },
{ text: 'Time to unwind', icon: '
', suffix: '.' },
{ text: 'Evening’s here—relax', icon: '
', suffix: '.' },
{ text: 'Breathe and recharge', icon: '
', suffix: '…' }
);
-
timeGreetings.push(
{ text: 'Good night', icon: '
', suffix: '!' },
{ text: 'Rest well', icon: '
', suffix: '.' },
@@ -93,6 +97,7 @@ function getGreeting() {
const now = new Date();
const hour = now.getHours();
let pool = [];
+
if (hour >= 5 && hour < 12) {
pool = timeGreetings.slice(0, 4);
} else if (hour < 17) {
@@ -102,41 +107,50 @@ function getGreeting() {
} else {
pool = timeGreetings.slice(12, 16);
}
+
if (Math.random() < 0.5) {
pool = generalGreetings;
}
- const choice = pool[Math.floor(Math.random() * pool.length)];
- return { text: choice.text, icon: choice.icon, suffix: choice.suffix };
+
+ return pool[Math.floor(Math.random() * pool.length)];
}
function updateGreeting(name) {
const { text, icon, suffix } = getGreeting();
const el = document.getElementById('greeting');
if (!el) return;
- if (text === 'Hope you enjoy Waves') {
- el.innerHTML = `${icon} ${text}, ${name}${suffix}`;
- } else {
- el.innerHTML = `${icon} ${text}, ${name}${suffix}`;
- }
+
+ el.innerHTML = `${icon} ${text}, ${name}${suffix}`;
el.style.opacity = 1;
}
+function updateHi(name) {
+ const hiEl = document.getElementById('hi');
+ if (hiEl) {
+ hiEl.textContent = `Hii, ${name}!!`;
+ }
+}
+
function showToast(message, type = 'success', iconType = 'wave') {
const toast = document.createElement('div');
toast.className = `toast show ${type}`;
+
const icons = {
success: '
',
- error: '
',
- info: '
',
+ error: '
',
+ info: '
',
warning: '
',
- wave: '
',
- game: '
',
- apps: '
'
+ wave: '
',
+ game: '
',
+ apps: '
'
};
+
toast.innerHTML = `${icons[iconType] || icons.wave}${message}`;
+
const progressBar = document.createElement('div');
progressBar.className = 'progress-bar';
toast.appendChild(progressBar);
+
const closeBtn = document.createElement('button');
closeBtn.className = 'toast-close';
closeBtn.innerHTML = '
';
@@ -145,7 +159,9 @@ function showToast(message, type = 'success', iconType = 'wave') {
setTimeout(() => toast.remove(), 500);
});
toast.appendChild(closeBtn);
+
document.body.appendChild(toast);
+
setTimeout(() => {
toast.classList.add('hide');
setTimeout(() => toast.remove(), 500);
diff --git a/public/assets/js/ping.js b/public/assets/js/ping.js
index 1f80ca0d..9e1c9aae 100644
--- a/public/assets/js/ping.js
+++ b/public/assets/js/ping.js
@@ -10,7 +10,7 @@
ws = new WebSocket(wsUrl);
ws.onopen = function() {
- pingDisplay.innerHTML = '
Ping: Waiting...';
+ pingDisplay.innerHTML = '
Ping: Waiting...';
};
ws.onmessage = function(event) {
@@ -23,7 +23,7 @@
}));
}
if (data.type === "latency" && typeof data.latency === "number") {
- pingDisplay.innerHTML = '
Ping: ' + '~' + data.latency + 'ms';
+ pingDisplay.innerHTML = '
' + 'Ping: ' + '~' + data.latency + 'ms';
}
} catch (err) {
console.error("Error parsing message:", err);
@@ -31,11 +31,11 @@
};
ws.onerror = function() {
- pingDisplay.innerHTML = '
Ping: Error';
+ pingDisplay.innerHTML = '
Ping: Error';
};
ws.onclose = function() {
- pingDisplay.innerHTML = '
Ping: Disconnected';
+ pingDisplay.innerHTML = '
Ping: Disconnected';
setTimeout(createWebSocket, 1000);
};
}