import { env } from "cloudflare:workers";
const DEV_FORGE_HOST = "localhost";
const DEV_EDITOR_HOST = "editor.localhost";
function configured(): URL | null {
if (!env.EDITOR_ORIGIN) return null;
try {
return new URL(env.EDITOR_ORIGIN);
} catch {
return null;
}
}
/**
* The editor runs on its own origin so its scripts (VS Code, Open VSX extensions) can never reach forge
* cookies or pages. Production sets EDITOR_ORIGIN, ideally on a separate registrable domain; local dev maps
* `localhost:<port>` to `editor.localhost:<port>` (browsers resolve `*.localhost` to loopback, and the
* forge's host-only cookies are never sent to another host).
*/
export function editorOriginFor(forgeUrl: URL): string | null {
const explicit = configured();
if (explicit) return explicit.origin;
if (forgeUrl.hostname !== DEV_FORGE_HOST) return null;
const url = new URL(forgeUrl.origin);
url.hostname = DEV_EDITOR_HOST;
return url.origin;
}
export function isEditorOrigin(url: URL): boolean {
const explicit = configured();
if (explicit) return url.origin === explicit.origin;
return url.hostname === DEV_EDITOR_HOST;
}
/** The forge origin an editor-origin page talks to and accepts messages from. */
export function forgeOriginFor(editorUrl: URL): string {
if (configured()) return new URL(env.PUBLIC_URL).origin;
const url = new URL(editorUrl.origin);
url.hostname = DEV_FORGE_HOST;