Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(relay-server): send absolute URLs on this page's own origin and /api through the relay
Bugfixer 2840dd5e24fe3
diff --git a/src/lib/relay-server.test.ts b/src/lib/relay-server.test.tsindex e5b73d20a6..4dd4bd32c4 100644--- a/src/lib/relay-server.test.ts+++ b/src/lib/relay-server.test.ts@@ -105,6 +105,19 @@ describe("serverFetch", () => {expect(sent.map((one) => one.path)).toEqual(["/api/bots?name=A%20B", "/api/files/a%20b.txt"]);});+ it("sends an absolute URL on this page's own origin and /api through the link; other origins stay plain", async () => {+ const plain = vi.fn(async (_path: string) => new Response("plain"));+ vi.stubGlobal("fetch", plain);+ vi.stubGlobal("location", { origin: "https://app.example" });+ const { link, sent } = fakeLink(() => new Response(null, { status: 204 }));+ setRelayLink(link);+ await serverFetch("https://app.example/api/bots/b1/browser/upload?chooser=c1&token=t", { method: "POST", body: new FormData() });+ await serverFetch("https://other.example/api/bots");+ await serverFetch("https://app.example/apiary");+ expect(sent.map((one) => one.path)).toEqual(["/api/bots/b1/browser/upload?chooser=c1&token=t"]);+ expect(plain.mock.calls.map(([path]) => path)).toEqual(["https://other.example/api/bots", "https://app.example/apiary"]);+ });+it("passes the caller's signal to the link", async () => {const { link, sent } = fakeLink(() => new Response("{}"));setRelayLink(link);diff --git a/src/lib/relay-server.ts b/src/lib/relay-server.tsindex 6720a79e49..b923a3c61c 100644--- a/src/lib/relay-server.ts+++ b/src/lib/relay-server.ts@@ -34,13 +34,25 @@ export function relayLink(): ServerLink | null {const API = /^\/api(?:[/?#]|$)/;-/** `fetch` for a path on this page's own server. */+/** `path` as a request to this page's own `/api` routes: a root path, or an absolute URL on this page's origin (some+ * links are built from `location`); null for anything else. */+function ownApiPath(path: string): string | null {+ if (API.test(path)) return path;+ const origin = (globalThis as { location?: { origin?: string } }).location?.origin;+ if (!origin || !path.startsWith(`${origin}/`)) return null;+ const url = new URL(path);+ return url.origin === origin && API.test(url.pathname) ? url.pathname + url.search : null;+}++/** `fetch` for a path on this page's own server. Over the relay, `keepalive` does not apply: the link lives in this page+ * and ends with it. */export async function serverFetch(path: string, init?: RequestInit): Promise<Response> {const current = link;+ const own = current ? ownApiPath(path) : null;// Called as the caller called it: with no options when it gave none.- if (!current || !API.test(path)) return init ? fetch(path, init) : fetch(path);+ if (!current || own === null) return init ? fetch(path, init) : fetch(path);// A Request turns any body fetch takes (FormData, Blob, URLSearchParams…) into bytes and gives it its content type.- const request = new Request(new URL(path, "http://relay.invalid"), { ...init, signal: undefined });+ const request = new Request(new URL(own, "http://relay.invalid"), { ...init, signal: undefined });const body = request.method === "GET" || request.method === "HEAD" ? null : new Uint8Array(await request.arrayBuffer());// The normalized target: spaces and other unsafe characters encoded, any fragment left out.const { pathname, search } = new URL(request.url);