#!/usr/bin/env python3 """Patches packages/core/src/config.ts so it can be reconfigured public/private at container-start time instead of only at build time. Vite bakes VITE_* env vars into the bundle as literal strings when it compiles (import.meta.env.VITE_FOO is a static replacement, not a runtime lookup) -- confirmed directly in config.ts's own comment on getViteEnv(). That means a container image built once can never be flipped between an isolated private relay and the public Conduit network via ordinary env vars after the fact; the values are frozen into the compiled JS. This packaging needs exactly that live toggle, so docker-entrypoint.sh writes a small /runtime-env.js at *container start* (from real env vars) setting window.__CONDUIT_RUNTIME_ENV__ before the app bundle runs. This script inserts one small override function right before the single call site that consumes getViteEnv()'s result (`const env = getViteEnv()`, confirmed to be the only call site in the file and to run once at module load), so only that one merge point needs to change. Only non-empty string fields override the Vite-baked default; a build with no runtime override behaves identically to stock upstream Conduit. Uses a literal string anchor rather than a line-numbered diff/patch -- more robust against upstream reformatting a comment or reflowing unrelated lines than a traditional unified diff would be. """ import sys CONFIG_PATH = "packages/core/src/config.ts" ANCHOR = "const env = getViteEnv()" OVERRIDE_BLOCK = '''// --- Archipelago runtime relay override ------------------------------------- // See docker/conduit-market/apply-runtime-override.py in the archy repo for // why this exists: Vite bakes VITE_* env vars into the bundle at build // time, so a container image built once can never be reconfigured // public/private afterward via ordinary env vars. docker-entrypoint.sh // writes window.__CONDUIT_RUNTIME_ENV__ from real environment variables at // container start, before this bundle runs. function getRuntimeOverrides(): Partial> { if (typeof window === "undefined") return {} const raw = (window as unknown as Record) .__CONDUIT_RUNTIME_ENV__ if (!raw || typeof raw !== "object") return {} const out: Record = {} for (const [key, value] of Object.entries(raw as Record)) { if (typeof value === "string" && value.length > 0) out[key] = value } return out as Partial> } const env = { ...getViteEnv(), ...getRuntimeOverrides() }''' with open(CONFIG_PATH) as f: content = f.read() count = content.count(ANCHOR) if count != 1: print( f"FATAL: expected exactly 1 occurrence of anchor line in {CONFIG_PATH}, " f"found {count}. Upstream config.ts has likely changed in a way that " "needs this script re-checked by hand before it can safely patch it.", file=sys.stderr, ) sys.exit(1) content = content.replace(ANCHOR, OVERRIDE_BLOCK, 1) with open(CONFIG_PATH, "w") as f: f.write(content) print(f"Patched {CONFIG_PATH}: runtime relay override installed.")