From 3bb92600cd492b84935bf1ba74f858871f53e391 Mon Sep 17 00:00:00 2001 From: scamiv <6170744+scamiv@users.noreply.github.com> Date: Mon, 23 Feb 2026 13:50:33 +0100 Subject: [PATCH] Refactor environment variable defaults and improve error handling in various scripts. --- scripts/probe-production-api.mjs | 29 ++++++++++++++++++----------- src/ingest/ingestService.ts | 4 ---- src/ingest/server.ts | 4 ---- src/ingest/store.ts | 1 - src/web/main.ts | 1 - 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/scripts/probe-production-api.mjs b/scripts/probe-production-api.mjs index 5455db253..b21a93cff 100644 --- a/scripts/probe-production-api.mjs +++ b/scripts/probe-production-api.mjs @@ -1,10 +1,11 @@ import WebSocket from "ws"; -const BASE_URL = process.env.TARGET_BASE_URL || "https://openfront.io"; -const ARCHIVE_API_BASE = process.env.ARCHIVE_API_BASE || "https://api.openfront.io"; -const NUM_WORKERS = Number(process.env.NUM_WORKERS || "20"); -const WS_WAIT_MS = Number(process.env.WS_WAIT_MS || "6000"); -const CONNECT_TIMEOUT_MS = Number(process.env.CONNECT_TIMEOUT_MS || "5000"); +const BASE_URL = process.env.TARGET_BASE_URL ?? "https://openfront.io"; +const ARCHIVE_API_BASE = + process.env.ARCHIVE_API_BASE ?? "https://api.openfront.io"; +const NUM_WORKERS = Number(process.env.NUM_WORKERS ?? "20"); +const WS_WAIT_MS = Number(process.env.WS_WAIT_MS ?? "6000"); +const CONNECT_TIMEOUT_MS = Number(process.env.CONNECT_TIMEOUT_MS ?? "5000"); const trim = (v) => v.replace(/\/+$/, ""); const base = trim(BASE_URL); @@ -20,8 +21,6 @@ const workerIndexForGame = (gameID, workers) => { return Math.abs(hash) % Math.max(1, workers); }; -const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); - const fetchJson = async (url, timeoutMs = 6000) => { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), timeoutMs); @@ -30,7 +29,7 @@ const fetchJson = async (url, timeoutMs = 6000) => { signal: controller.signal, headers: { Accept: "application/json" }, }); - const contentType = response.headers.get("content-type") || ""; + const contentType = response.headers.get("content-type") ?? ""; const text = await response.text(); let json = null; if (contentType.includes("application/json")) { @@ -93,7 +92,9 @@ const wsProbe = async (url, waitMs = WS_WAIT_MS) => { result.error = "connect-timeout"; try { ws?.terminate(); - } catch {} + } catch (error) { + result.error = result.error ?? String(error); + } finish(); }, CONNECT_TIMEOUT_MS); @@ -113,7 +114,9 @@ const wsProbe = async (url, waitMs = WS_WAIT_MS) => { setTimeout(() => { try { ws.close(1000, "probe-done"); - } catch {} + } catch (error) { + result.error = result.error ?? String(error); + } }, waitMs); }); @@ -207,7 +210,11 @@ async function main() { } printHeader("Sample game follow-up"); - const parsed = JSON.parse(withLobbies.firstMessageRaw || withLobbies.firstMessageSample); + const firstPayload = + withLobbies.firstMessageRaw === "" + ? withLobbies.firstMessageSample + : withLobbies.firstMessageRaw; + const parsed = JSON.parse(firstPayload); const game = parsed.games?.[0] ?? parsed.data?.lobbies?.[0]; if (!game?.gameID) { console.log("No sample game in first games payload."); diff --git a/src/ingest/ingestService.ts b/src/ingest/ingestService.ts index ce550a135..cb3f1a979 100644 --- a/src/ingest/ingestService.ts +++ b/src/ingest/ingestService.ts @@ -172,7 +172,6 @@ export class LobbyIngestService { this.store.systemNote( `Invalid /lobbies JSON parse error: ${String(error)} | payload=${payload}`, ); - // eslint-disable-next-line no-console console.error("[lobbystatistics] invalid websocket payload", { error, payload, @@ -183,7 +182,6 @@ export class LobbyIngestService { if (json && typeof json === "object" && (json as { type?: string }).type === "error") { const payload = compactPayload(text); this.store.systemNote(`WebSocket error reply received: payload=${payload}`); - // eslint-disable-next-line no-console console.error("[lobbystatistics] websocket error reply", payload); return; } @@ -195,7 +193,6 @@ export class LobbyIngestService { `Invalid /lobbies schema: ${normalized.error .slice(0, 240)} | payload=${payload}`, ); - // eslint-disable-next-line no-console console.error("[lobbystatistics] websocket schema mismatch", { error: normalized.error, payload, @@ -525,7 +522,6 @@ export class LobbyIngestService { await fn(); } catch (error) { this.store.systemNote(`Task ${label} failed: ${String(error)}`); - // eslint-disable-next-line no-console console.error(`[lobbystatistics] task ${label} failed`, error); } } diff --git a/src/ingest/server.ts b/src/ingest/server.ts index dd9a5c987..6ef05a08a 100644 --- a/src/ingest/server.ts +++ b/src/ingest/server.ts @@ -95,17 +95,14 @@ async function main() { }); const server = app.listen(config.port, () => { - // eslint-disable-next-line no-console console.log(`[lobbystatistics] ingest server listening on :${config.port}`); }); process.on("unhandledRejection", (reason) => { - // eslint-disable-next-line no-console console.error("[lobbystatistics] unhandledRejection", reason); }); process.on("uncaughtException", (error) => { - // eslint-disable-next-line no-console console.error("[lobbystatistics] uncaughtException", error); }); @@ -122,7 +119,6 @@ async function main() { } void main().catch((error) => { - // eslint-disable-next-line no-console console.error("[lobbystatistics] fatal startup error", error); process.exit(1); }); diff --git a/src/ingest/store.ts b/src/ingest/store.ts index fdbcf396e..30758acc5 100644 --- a/src/ingest/store.ts +++ b/src/ingest/store.ts @@ -375,7 +375,6 @@ export class JsonStore { this.flushTimer = null; void this.flush().catch((error) => { this.dirty = true; - // eslint-disable-next-line no-console console.error("[lobbystatistics] db flush failed", error); }); }, 500); diff --git a/src/web/main.ts b/src/web/main.ts index c341bf18a..5fccbaaca 100644 --- a/src/web/main.ts +++ b/src/web/main.ts @@ -8,7 +8,6 @@ import { import * as d3 from "d3"; import "./styles.css"; -const DEFAULT_BUCKET_MODE: BucketMode = "game_mode_team"; const DEFAULT_LOOKBACK_HOURS = 24; const DEFAULT_ORDER_COUNT = 40;