mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 23:41:59 +00:00
275fd0dccc
## Description: This is a refactor to simplify config handling. Replaces the per-environment DevConfig/PreprodConfig/ProdConfig class hierarchy with two static classes: ClientEnv (browser main thread, reads from window.BOOTSTRAP_CONFIG) and ServerEnv (Node server, reads from process.env). The four config classes are deleted, the abstract DefaultServerConfig is gone, and DefaultConfig is renamed to Config. The values that flow server → client (gameEnv, numWorkers, turnstileSiteKey, jwtAudience, instanceId) used to be baked into the hardcoded per-env classes. They're now real env vars on the server, embedded into a single window.BOOTSTRAP_CONFIG object in index.html at request time (alongside the existing gitCommit/assetManifest/cdnBase globals, which moved into the same object), and read back by ClientEnv on the client. The dev defaults previously hidden inside DevServerConfig are now explicit in start:server-dev (NUM_WORKERS=2, TURNSTILE_SITE_KEY=1x..., JWT_AUDIENCE=localhost, etc.) and in vite.config.ts's html plugin inject.data. Production deploys plumb NUM_WORKERS and TURNSTILE_SITE_KEY through deploy.yml (GitHub vars) into the remote env file; JWT_AUDIENCE is derived from DOMAIN in deploy.sh. The dynamic /api/instance endpoint is gone — INSTANCE_ID rides along in BOOTSTRAP_CONFIG now. ServerEnv is the only thing server code touches; ClientEnv is browser-only. The two classes have intentional overlap (env, numWorkers, jwtIssuer, gameCreationRate, workerIndex, etc.) since they derive identical logic from different sources — there's a TODO in each to consolidate via a shared helper later. The game-logic Config no longer stores a ServerConfig/ClientEnv reference and its serverConfig() getter is gone; the one caller (MultiTabModal) now reads ClientEnv.env() directly. Worker init no longer carries server-config values since nothing in the worker actually reads them. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
121 lines
3.6 KiB
TypeScript
121 lines
3.6 KiB
TypeScript
import { JWK } from "jose";
|
|
import { z } from "zod";
|
|
import { GameID } from "../core/Schemas";
|
|
import { simpleHash } from "../core/Util";
|
|
import {
|
|
GameEnv,
|
|
JwksSchema,
|
|
parseGameEnv,
|
|
} from "../core/configuration/Config";
|
|
|
|
export class ClientEnv {
|
|
private static values: ClientEnvValues | null = null;
|
|
private static publicKey: JWK | null = null;
|
|
|
|
/** Test-only. */
|
|
static reset(): void {
|
|
ClientEnv.values = null;
|
|
ClientEnv.publicKey = null;
|
|
}
|
|
|
|
private static get(): ClientEnvValues {
|
|
if (ClientEnv.values) return ClientEnv.values;
|
|
if (typeof window === "undefined") {
|
|
throw new Error("ClientEnv is only available on the browser main thread");
|
|
}
|
|
const bc = window.BOOTSTRAP_CONFIG;
|
|
if (
|
|
!bc ||
|
|
bc.gameEnv === undefined ||
|
|
bc.numWorkers === undefined ||
|
|
bc.turnstileSiteKey === undefined ||
|
|
bc.jwtAudience === undefined ||
|
|
bc.instanceId === undefined ||
|
|
bc.gitCommit === undefined
|
|
) {
|
|
throw new Error("Missing BOOTSTRAP_CONFIG");
|
|
}
|
|
ClientEnv.values = {
|
|
gameEnv: parseGameEnv(bc.gameEnv),
|
|
numWorkers: bc.numWorkers,
|
|
turnstileSiteKey: bc.turnstileSiteKey,
|
|
jwtAudience: bc.jwtAudience,
|
|
instanceId: bc.instanceId,
|
|
gitCommit: bc.gitCommit,
|
|
};
|
|
return ClientEnv.values;
|
|
}
|
|
|
|
// TODO: the following methods are duplicated on ServerEnv. The two classes
|
|
// read from different sources (window.BOOTSTRAP_CONFIG vs process.env) but
|
|
// the derived logic is identical. Consolidate into a shared helper that
|
|
// takes a source so we don't have to keep them in sync by hand.
|
|
static env(): GameEnv {
|
|
return ClientEnv.get().gameEnv;
|
|
}
|
|
static numWorkers(): number {
|
|
return ClientEnv.get().numWorkers;
|
|
}
|
|
static turnstileSiteKey(): string {
|
|
return ClientEnv.get().turnstileSiteKey;
|
|
}
|
|
static jwtAudience(): string {
|
|
return ClientEnv.get().jwtAudience;
|
|
}
|
|
static instanceId(): string {
|
|
return ClientEnv.get().instanceId;
|
|
}
|
|
static gitCommit(): string {
|
|
return ClientEnv.get().gitCommit;
|
|
}
|
|
static jwtIssuer(): string {
|
|
const audience = ClientEnv.jwtAudience();
|
|
return audience === "localhost"
|
|
? "http://localhost:8787"
|
|
: `https://api.${audience}`;
|
|
}
|
|
static async jwkPublicKey(): Promise<JWK> {
|
|
if (ClientEnv.publicKey) return ClientEnv.publicKey;
|
|
const jwksUrl = ClientEnv.jwtIssuer() + "/.well-known/jwks.json";
|
|
console.log(`Fetching JWKS from ${jwksUrl}`);
|
|
const response = await fetch(jwksUrl);
|
|
if (!response.ok) {
|
|
const body = await response.text();
|
|
throw new Error(`JWKS fetch failed: ${response.status} ${body}`);
|
|
}
|
|
const result = JwksSchema.safeParse(await response.json());
|
|
if (!result.success) {
|
|
const error = z.prettifyError(result.error);
|
|
console.error("Error parsing JWKS", error);
|
|
throw new Error("Invalid JWKS");
|
|
}
|
|
ClientEnv.publicKey = result.data.keys[0];
|
|
return ClientEnv.publicKey;
|
|
}
|
|
static turnIntervalMs(): number {
|
|
return 100;
|
|
}
|
|
static gameCreationRate(): number {
|
|
return ClientEnv.env() === GameEnv.Dev ? 5 * 1000 : 2 * 60 * 1000;
|
|
}
|
|
static workerIndex(gameID: GameID): number {
|
|
return simpleHash(gameID) % ClientEnv.numWorkers();
|
|
}
|
|
static workerPath(gameID: GameID): string {
|
|
return `w${ClientEnv.workerIndex(gameID)}`;
|
|
}
|
|
}
|
|
/**
|
|
* Values that flow from server → client via index.html. Set on the server from
|
|
* process.env, then re-hydrated on the client from window.BOOTSTRAP_CONFIG.
|
|
*/
|
|
|
|
export interface ClientEnvValues {
|
|
gameEnv: GameEnv;
|
|
numWorkers: number;
|
|
turnstileSiteKey: string;
|
|
jwtAudience: string;
|
|
instanceId: string;
|
|
gitCommit: string;
|
|
}
|