From 3661e0fd2365bd616ff5f47bd729c826948cae29 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Sat, 14 Nov 2026 17:36:19 -0700 Subject: [PATCH] Live-updates dot: connected only once an event arrives (#71) Since #72 the relay answers the browser before Stalwart has, so an open stream no longer proves anything: when Stalwart never answers, the dot would turn green while no updates arrive. The client now shows "connected" on the first ping or state event instead of on open, and resets its backoff there too, so a stream that opens and ends at once keeps backing off instead of retrying every second. Relayed tabs get Stalwart's own first ping. Fan-out has no upstream ping, so the server sends one when a tab joins fan-out, directly or by moving off the relay. --- server/src/app.ts | 5 ++- server/src/push.ts | 9 ++++ web/src/jmap/__tests__/push.test.ts | 67 +++++++++++++++++++++++++++++ web/src/jmap/push.ts | 15 +++++-- 4 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 web/src/jmap/__tests__/push.test.ts diff --git a/server/src/app.ts b/server/src/app.ts index 40871f4..1203efc 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -6,7 +6,7 @@ import { compress } from "hono/compress"; import { request as httpRequest } from "node:http"; import { request as httpsRequest } from "node:https"; import { RESPONSE_ALREADY_SENT } from "@hono/node-server/utils/response"; -import { attach as pushAttach, attachRelay as pushAttachRelay, prepare as pushPrepare, receive as pushReceive, pushStatus } from "./push.js"; +import { ALIVE_FRAME, attach as pushAttach, attachRelay as pushAttachRelay, prepare as pushPrepare, receive as pushReceive, pushStatus } from "./push.js"; import { getConnInfo } from "@hono/node-server/conninfo"; import { config } from "./config.js"; import { fetchPermissions } from "./permissionSchema.js"; @@ -1127,7 +1127,7 @@ export function createApp(basePath = config.basePath): Hono { if (accountId && pushAttach(session.username, accountId, session.authorization, out)) { out.writeHead(200, SSE_HEADERS); out.flushHeaders(); - out.write(": subscribed\n\n"); + out.write(": subscribed\n\n" + ALIVE_FRAME); return RESPONSE_ALREADY_SENT; } if (config.rawPushRelay) return relayPushRaw(c, url, session.authorization, session.username); @@ -1413,6 +1413,7 @@ export function relayPushRaw(c: Context, url: string, authorization: string req.removeAllListeners(); req.on("error", () => {}); req.destroy(); + out.write(ALIVE_FRAME); }; if (username) pushAttachRelay(username, out, migrate); req.on("response", (res) => { diff --git a/server/src/push.ts b/server/src/push.ts index a6caeca..4c9d9e2 100644 --- a/server/src/push.ts +++ b/server/src/push.ts @@ -53,6 +53,15 @@ const byKey = new Map(); const byToken = new Map(); let sweeper: NodeJS.Timeout | null = null; +/** + * An event the browser can see, sent when a stream is known to be live: at + * once on fan-out, and when a relayed tab moves to fan-out. The client shows + * "connected" only after an event arrives, not when the response opens -- + * the relay answers before the upstream has (issue #71), so an open response + * alone proves nothing. Relayed tabs get the upstream's own first ping. + */ +export const ALIVE_FRAME = "event: ping\ndata: {}\n\n"; + export function pushEnabled(): boolean { return config.pushMode === "subscribe" && !!config.pushUrl; } diff --git a/web/src/jmap/__tests__/push.test.ts b/web/src/jmap/__tests__/push.test.ts new file mode 100644 index 0000000..734dafc --- /dev/null +++ b/web/src/jmap/__tests__/push.test.ts @@ -0,0 +1,67 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { push, type PushState } from "../push"; + +/** Just enough of EventSource to drive PushManager by hand. */ +class FakeEventSource { + static last: FakeEventSource | null = null; + onopen: (() => void) | null = null; + onerror: (() => void) | null = null; + private handlers = new Map void)[]>(); + closed = false; + constructor(public url: string) { FakeEventSource.last = this; } + addEventListener(type: string, fn: (ev: MessageEvent) => void) { + this.handlers.set(type, [...(this.handlers.get(type) ?? []), fn]); + } + close() { this.closed = true; } + emit(type: string, data: string) { + for (const fn of this.handlers.get(type) ?? []) fn(new MessageEvent(type, { data })); + } +} + +describe("push connection state (issue #71)", () => { + let states: PushState[]; + let off: () => void; + + beforeEach(() => { + vi.useFakeTimers(); + vi.stubGlobal("EventSource", FakeEventSource); + states = []; + off = push.onConnection((s) => states.push(s)); + push.start(); + }); + + afterEach(() => { + off(); + push.stop(); + vi.unstubAllGlobals(); + vi.useRealTimers(); + }); + + it("an opened stream is not connected until an event arrives", () => { + const es = FakeEventSource.last!; + es.onopen?.(); + expect(push.state).toBe("connecting"); + es.emit("ping", '{"interval": 30}'); + expect(push.state).toBe("connected"); + }); + + it("a state change counts as proof of life too", () => { + const es = FakeEventSource.last!; + es.emit("state", JSON.stringify({ "@type": "StateChange", changed: {} })); + expect(push.state).toBe("connected"); + }); + + it("a stream that opens and ends at once keeps backing off", () => { + const delays: number[] = []; + for (let i = 0; i < 4; i++) { + const es = FakeEventSource.last!; + es.onopen?.(); + es.onerror?.(); + const before = Date.now(); + vi.advanceTimersToNextTimer(); + delays.push(Date.now() - before); + } + expect(delays).toEqual([1000, 2000, 4000, 8000]); + expect(states).not.toContain("connected"); + }); +}); diff --git a/web/src/jmap/push.ts b/web/src/jmap/push.ts index e603ada..3804842 100644 --- a/web/src/jmap/push.ts +++ b/web/src/jmap/push.ts @@ -74,11 +74,20 @@ class PushManager { const url = withBase(`/api/events?types=*&closeafter=no&ping=30`); const es = new EventSource(url, { withCredentials: true }); this.es = es; - es.onopen = () => { + /* + * Connected means an event has arrived, not that the response opened. + * The server answers before the mail server has (issue #71), so an + * open stream can still be one that never delivers anything. Every live + * stream sends a ping soon after it starts -- the mail server's own, or + * ours on fan-out -- so the first event is the proof. The backoff resets + * here too, so a stream that opens and ends at once keeps backing off. + */ + const alive = () => { this.backoff = 1000; this.setState("connected"); }; es.addEventListener("state", (ev) => { + alive(); try { const data = JSON.parse((ev as MessageEvent).data as string) as StateChange; if (data["@type"] !== "StateChange") return; @@ -94,9 +103,7 @@ class PushManager { /* ignore malformed */ } }); - es.addEventListener("ping", () => { - /* keepalive */ - }); + es.addEventListener("ping", alive); es.onerror = () => { es.close(); this.es = null; -- 2.54.0