Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(relay-server): forward the normalized request target and end SSE lines at a CR at once

Bugfixer 2757b3c452b25
diff --git a/src/lib/relay-server.test.ts b/src/lib/relay-server.test.ts
index b8d60f089c..e5b73d20a6 100644
--- a/src/lib/relay-server.test.ts
+++ b/src/lib/relay-server.test.ts
@@ -97,6 +97,14 @@ describe("serverFetch", () => {
expect(text).toContain("hello");
});
+ it("forwards the normalized target: unsafe characters encoded, no fragment", async () => {
+ const { link, sent } = fakeLink(() => new Response("{}"));
+ setRelayLink(link);
+ await serverFetch("/api/bots?name=A B#section");
+ await serverFetch("/api/files/a b.txt");
+ expect(sent.map((one) => one.path)).toEqual(["/api/bots?name=A%20B", "/api/files/a%20b.txt"]);
+ });
+
it("passes the caller's signal to the link", async () => {
const { link, sent } = fakeLink(() => new Response("{}"));
setRelayLink(link);
@@ -158,6 +166,27 @@ describe("relayEventSource", () => {
expect(source.onerror).not.toHaveBeenCalled();
});
+ it("delivers an event whose last line ends in a CR at once, and counts a CRLF split across chunks once", async () => {
+ const stream = eventStream();
+ const { link } = fakeLink(() => stream.response);
+ const source = relayEventSource(link, "/api/events");
+ const messages: string[] = [];
+ source.onmessage = (event) => messages.push(event.data);
+ source.onerror = vi.fn();
+ await tick();
+
+ stream.push("data: x\r\r");
+ await tick();
+ expect(messages).toEqual(["x"]);
+
+ stream.push("data: y\r");
+ stream.push("\ndata: z\r");
+ stream.push("\n\r\n");
+ await tick();
+ expect(messages).toEqual(["x", "y\nz"]);
+ expect(source.onerror).not.toHaveBeenCalled();
+ });
+
it("reports onerror once when the stream ends, so live events reconnect", async () => {
const stream = eventStream();
const { link } = fakeLink(() => stream.response);
diff --git a/src/lib/relay-server.ts b/src/lib/relay-server.ts
index dec1a5a0cb..4e2d33f8b8 100644
--- a/src/lib/relay-server.ts
+++ b/src/lib/relay-server.ts
@@ -26,7 +26,9 @@ export async function serverFetch(path: string, init: RequestInit = {}): Promise
// 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 body = request.method === "GET" || request.method === "HEAD" ? null : new Uint8Array(await request.arrayBuffer());
- return current.fetch(path, { method: request.method, headers: request.headers, body, signal: init.signal ?? undefined });
+ // The normalized target: spaces and other unsafe characters encoded, any fragment left out.
+ const { pathname, search } = new URL(request.url);
+ return current.fetch(pathname + search, { method: request.method, headers: request.headers, body, signal: init.signal ?? undefined });
}
/** An EventSource on `url` through `current`, as the live events transport uses it (src/lib/live-events.ts): `onerror`
@@ -58,6 +60,8 @@ async function readEvents(body: ReadableStream<Uint8Array>, signal: AbortSignal,
const reader = body.getReader();
const decoder = new TextDecoder();
let pending = "";
+ // A chunk that ended in CR has ended that line already; an LF that starts the next chunk belongs to the same CRLF.
+ let skipLF = false;
let data: string[] = [];
let type = "";
let lastEventId = "";
@@ -80,9 +84,14 @@ async function readEvents(body: ReadableStream<Uint8Array>, signal: AbortSignal,
for (;;) {
const { done, value } = await reader.read();
if (done || signal.aborted) return;
- pending += decoder.decode(value, { stream: true });
- // A lone CR at the end may be the first half of a CRLF: it waits for the next chunk.
- const parts = pending.split(/\r\n|\r(?!$)|\n/);
+ let text = decoder.decode(value, { stream: true });
+ if (skipLF && text.startsWith("\n")) {
+ text = text.slice(1);
+ skipLF = false;
+ }
+ if (text) skipLF = text.endsWith("\r");
+ pending += text;
+ const parts = pending.split(/\r\n|\r|\n/);
pending = parts.pop()!;
for (const part of parts) {
line(part);