refactor(web): drop same-origin sentinel from runtime config
This commit is contained in:
@@ -1,24 +1,17 @@
|
|||||||
const GLOBAL_KEY = "__OPENCONCHO_DEFAULT_HONCHO_URL__";
|
const GLOBAL_KEY = "__OPENCONCHO_DEFAULT_HONCHO_URL__";
|
||||||
const SAME_ORIGIN = "same-origin";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runtime-injected default Honcho base URL for container deployments.
|
* Runtime-injected default Honcho base URL for container deployments.
|
||||||
*
|
*
|
||||||
* The Docker image writes `/config.js` from the `OPENCONCHO_DEFAULT_HONCHO_URL`
|
* The Docker image writes `/config.js` from `OPENCONCHO_DEFAULT_HONCHO_URL` at
|
||||||
* env at container start, so one prebuilt image can target any backend without
|
* container start, so one prebuilt image can target any backend without a rebuild.
|
||||||
* a rebuild (Vite envs are baked at build time and can't do this).
|
* The web build proxies this URL via the same-origin `/api` reverse proxy (no CORS).
|
||||||
*
|
*
|
||||||
* - `"same-origin"` → the page's own origin (pairs with the nginx `/v3` reverse
|
* - an absolute URL → that URL (seeds the first instance)
|
||||||
* proxy, so the browser makes same-origin requests and CORS never applies)
|
* - empty / unset → null (no default; the user configures in Settings)
|
||||||
* - an absolute URL → that URL
|
|
||||||
* - empty / unset → `null` (no default; the user configures in Settings)
|
|
||||||
*/
|
*/
|
||||||
export function runtimeDefaultBaseUrl(): string | null {
|
export function runtimeDefaultBaseUrl(): string | null {
|
||||||
const raw = (globalThis as Record<string, unknown>)[GLOBAL_KEY];
|
const raw = (globalThis as Record<string, unknown>)[GLOBAL_KEY];
|
||||||
if (typeof raw !== "string" || raw.trim() === "") return null;
|
if (typeof raw !== "string" || raw.trim() === "") return null;
|
||||||
const value = raw.trim();
|
return raw.trim();
|
||||||
if (value === SAME_ORIGIN) {
|
|
||||||
return typeof location !== "undefined" ? location.origin : null;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,22 +8,14 @@ afterEach(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("runtimeDefaultBaseUrl", () => {
|
describe("runtimeDefaultBaseUrl", () => {
|
||||||
it("returns null when the global is unset", () => {
|
it("returns an injected absolute URL verbatim", () => {
|
||||||
expect(runtimeDefaultBaseUrl()).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns null when the global is blank", () => {
|
|
||||||
(globalThis as Record<string, unknown>)[KEY] = " ";
|
|
||||||
expect(runtimeDefaultBaseUrl()).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns an absolute URL verbatim", () => {
|
|
||||||
(globalThis as Record<string, unknown>)[KEY] = "https://honcho.example.net";
|
(globalThis as Record<string, unknown>)[KEY] = "https://honcho.example.net";
|
||||||
expect(runtimeDefaultBaseUrl()).toBe("https://honcho.example.net");
|
expect(runtimeDefaultBaseUrl()).toBe("https://honcho.example.net");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("resolves 'same-origin' to the page origin", () => {
|
it("returns null when unset or empty", () => {
|
||||||
(globalThis as Record<string, unknown>)[KEY] = "same-origin";
|
expect(runtimeDefaultBaseUrl()).toBeNull();
|
||||||
expect(runtimeDefaultBaseUrl()).toBe(location.origin);
|
(globalThis as Record<string, unknown>)[KEY] = " ";
|
||||||
|
expect(runtimeDefaultBaseUrl()).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user