Push relay: answer the browser before Stalwart does #72

Merged
jcoffey-dev merged 1 commits from fix/relay-headers-now into main 2026-11-14 21:40:01 +00:00
2 changed files with 125 additions and 32 deletions

No files matched your search

+53 -32
View File
@@ -1133,18 +1133,32 @@ export function createApp(basePath = config.basePath): Hono<Env> {
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<Uint8Array> | null = null;
const body = new ReadableStream<Uint8Array>({
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);
}
@@ -1332,6 +1346,26 @@ async function readGated(stream: ReadableStream<Uint8Array>): Promise<string> {
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
* (issue #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.
*
@@ -1347,14 +1381,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<Env>, url: string, authorization: string, username?: string): Response {
export function relayPushRaw(c: Context<Env>, 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, {
@@ -1365,27 +1392,22 @@ function relayPushRaw(c: Context<Env>, 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();
@@ -1396,7 +1418,6 @@ function relayPushRaw(c: Context<Env>, 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(); });
+72
View File
@@ -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.STALWART_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 (issue
* #71). The fake upstream here does the same, after a delay, and the browser
* side must not wait for it.
*/
async function listen(server: http.Server): Promise<string> {
await new Promise<void>((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();
}
});