From 6def126ea9d490fd92ea36f07753503debdeb2ad Mon Sep 17 00:00:00 2001 From: John Coffey Date: Sat, 14 Nov 2026 14:36:53 -0700 Subject: [PATCH] Push relay: answer the browser before the server does (ihasmail #72) The upstream sends nothing on its event stream, not even response headers, until its first ping, 30 seconds in. The relay waited for that before answering the browser, so the live-updates dot showed "reconnecting" for half a minute on every connect, and a proxy that gives up on a response that slow kept it there for good. Both relay paths now send their headers and a comment line at once, and end the stream if the upstream fails. Reported in coffey-labs/ihasmail#71. (cherry picked from commit 3ad9e2e96725d2e518663e90513d586a1f41a3d1) --- server/src/app.ts | 85 +++++++++++++++++++++++++--------------- server/src/relay.test.ts | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 32 deletions(-) create mode 100644 server/src/relay.test.ts diff --git a/server/src/app.ts b/server/src/app.ts index b88fbb0..ba685ef 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -1343,18 +1343,32 @@ export function createApp(basePath = config.basePath): Hono { if (config.rawPushRelay) return relayPushRaw(c, url, session.authorization, session.username); const controller = new AbortController(); c.req.raw.signal.addEventListener("abort", () => controller.abort()); - const res = await fetch(url, { + const upstreamRes = fetch(url, { headers: { authorization: session.authorization, accept: "text/event-stream" }, signal: controller.signal, }); - if (!res.ok || !res.body) return c.json({ error: "upstream_error" }, 502); - const headers = new Headers({ - "Content-Type": "text/event-stream", - "Cache-Control": "no-cache, no-transform", - Connection: "keep-alive", - "X-Accel-Buffering": "no", + upstreamRes.catch(() => {}); // read in pull(); a tab gone first must not leave it unhandled + // Answer at once, as relayPushRaw() does (see RELAY_OPENING), and end + // the stream if the upstream fails. + let reader: ReadableStreamDefaultReader | null = null; + const body = new ReadableStream({ + start(ctrl) { ctrl.enqueue(new TextEncoder().encode(RELAY_OPENING)); }, + async pull(ctrl) { + try { + if (!reader) { + const res = await upstreamRes; + if (!res.ok || !res.body) { await res.body?.cancel(); ctrl.close(); return; } + reader = res.body.getReader(); + } + const { done, value } = await reader.read(); + if (done) ctrl.close(); else ctrl.enqueue(value); + } catch { + ctrl.close(); + } + }, + cancel() { controller.abort(); }, }); - return new Response(res.body, { status: 200, headers }); + return new Response(body, { status: 200, headers: SSE_HEADERS }); } catch (err) { return upstreamFailure(c, err); } @@ -1582,6 +1596,26 @@ async function readGated(stream: ReadableStream): Promise { const PASSTHROUGH_HEADERS = new Set(["content-type", "content-disposition", "content-language", "etag", "last-modified", "retry-after"]); +const SSE_HEADERS = { + "content-type": "text/event-stream", + "cache-control": "no-cache, no-transform", + connection: "keep-alive", + "x-accel-buffering": "no", +} as const; + +/* + * Written to the browser the moment a relayed stream starts, before Stalwart + * has answered. Stalwart sends nothing on its event stream, not even response + * headers, until its first ping -- 30 seconds in with the ping=30 the client + * asks for (checked against 0.16.27 and 1.0.3). Waiting for that kept the + * live-updates dot on "reconnecting" for half a minute on every connect, and + * a proxy that gives up on a response that slow kept it there for good + * (ihasmail#71). Headers and a comment line now go out at once; a comment is + * ignored by EventSource, but it is a body byte, so no hop buffers the + * response waiting for one. + */ +const RELAY_OPENING = ": relay\n\n"; + /** * Hold a push stream open with the least machinery that will do it. * @@ -1597,14 +1631,7 @@ const PASSTHROUGH_HEADERS = new Set(["content-type", "content-disposition", "con * Returns a Response Hono treats as already sent: the raw bindings are * written to directly, and the returned value is never serialized. */ -const SSE_HEADERS = { - "content-type": "text/event-stream", - "cache-control": "no-cache, no-transform", - connection: "keep-alive", - "x-accel-buffering": "no", -} as const; - -function relayPushRaw(c: Context, url: string, authorization: string, username?: string): Response { +export function relayPushRaw(c: Context, url: string, authorization: string, username?: string): Response { const out = (c.env as { outgoing: import("node:http").ServerResponse }).outgoing; const target = new URL(url); const req = (target.protocol === "https:" ? httpsRequest : httpRequest)(target, { @@ -1615,27 +1642,22 @@ function relayPushRaw(c: Context, url: string, authorization: string, usern const abort = () => req.destroy(); signal.addEventListener("abort", abort); out.on("close", abort); - const fail = () => { - if (!out.headersSent) { - out.writeHead(502, { "content-type": "application/json", "cache-control": "no-store" }); - out.end(JSON.stringify({ error: "upstream_error" })); - } else { - out.end(); - } - }; + out.writeHead(200, SSE_HEADERS); + out.write(RELAY_OPENING); + // The browser already has a 200, so an upstream failure ends the stream; + // EventSource reports that as an error and the client retries with backoff. + const fail = () => { out.end(); }; /* * Once this account's subscription verifies, the upstream request goes and - * the browser stream below is served by fan-out instead. Three things have - * to be true for that to be seamless: the browser must already have its - * headers (verification can beat the upstream response); nothing may treat - * the torn-down upstream as an error; and nothing may keep a reference to - * it -- the request, its response and this handler's context are exactly - * the per-tab weight the subscription exists to shed. + * the browser stream below is served by fan-out instead. Two things have + * to be true for that to be seamless: nothing may treat the torn-down + * upstream as an error; and nothing may keep a reference to it -- the + * request, its response and this handler's context are exactly the per-tab + * weight the subscription exists to shed. */ let migrated = false; const migrate = () => { migrated = true; - if (!out.headersSent) { out.writeHead(200, SSE_HEADERS); out.flushHeaders(); } signal.removeEventListener("abort", abort); out.removeListener("close", abort); req.removeAllListeners(); @@ -1646,7 +1668,6 @@ function relayPushRaw(c: Context, url: string, authorization: string, usern req.on("response", (res) => { if (migrated) { res.destroy(); return; } if (res.statusCode !== 200) { res.resume(); fail(); return; } - if (!out.headersSent) { out.writeHead(200, SSE_HEADERS); out.flushHeaders(); } // end: false -- the browser stream outlives the upstream if we migrate. res.pipe(out, { end: false }); res.on("end", () => { if (!migrated) out.end(); }); diff --git a/server/src/relay.test.ts b/server/src/relay.test.ts new file mode 100644 index 0000000..ec0c163 --- /dev/null +++ b/server/src/relay.test.ts @@ -0,0 +1,72 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import type { AddressInfo } from "node:net"; +process.env.MAIL_SERVER_URL = "http://127.0.0.1:1"; +const { relayPushRaw } = await import("./app.js"); + +/* + * Stalwart sends no headers on its event stream until its first ping + * (ihasmail#71). The fake upstream does the same, after a delay, and the browser + * side must not wait for it. + */ +async function listen(server: http.Server): Promise { + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + return `http://127.0.0.1:${(server.address() as AddressInfo).port}`; +} + +function relayServer(upstreamUrl: string) { + return http.createServer((req, res) => { + const ac = new AbortController(); + res.on("close", () => ac.abort()); + const c = { env: { outgoing: res }, req: { raw: { signal: ac.signal } } }; + relayPushRaw(c as never, upstreamUrl, "Basic x"); + void req; + }); +} + +test("a relayed push stream answers before the upstream sends anything", async () => { + const upstream = http.createServer((_req, res) => { + setTimeout(() => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.write('event: ping\ndata: {"interval": 30}\n\n'); + }, 1500); + }); + const relay = relayServer(`${await listen(upstream)}/jmap/eventsource/`); + const base = await listen(relay); + try { + const started = Date.now(); + const res = await fetch(`${base}/api/events`); + assert.ok(Date.now() - started < 1000, "headers must not wait for the upstream"); + assert.equal(res.status, 200); + assert.equal(res.headers.get("content-type"), "text/event-stream"); + const reader = res.body!.getReader(); + const dec = new TextDecoder(); + let text = ""; + while (!text.includes("event: ping")) { + const { done, value } = await reader.read(); + if (done) break; + text += dec.decode(value, { stream: true }); + } + assert.ok(text.startsWith(": relay\n\n"), "a comment line goes out first"); + assert.ok(text.includes("event: ping"), "the upstream's events still come through"); + await reader.cancel(); + } finally { + relay.closeAllConnections(); upstream.closeAllConnections(); + relay.close(); upstream.close(); + } +}); + +test("an upstream refusal ends the relayed stream", async () => { + const upstream = http.createServer((_req, res) => { res.writeHead(401); res.end(); }); + const relay = relayServer(`${await listen(upstream)}/jmap/eventsource/`); + const base = await listen(relay); + try { + const res = await fetch(`${base}/api/events`); + assert.equal(res.status, 200); + assert.equal(await res.text(), ": relay\n\n"); + } finally { + relay.closeAllConnections(); upstream.closeAllConnections(); + relay.close(); upstream.close(); + } +}); -- 2.54.0