diff --git a/server/src/app.ts b/server/src/app.ts index ba685ef..0a6fb96 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 { sourceDocument } from "./source.js"; @@ -1337,7 +1337,7 @@ export function createApp(basePath = config.basePath): Hono { if (accountId && pushAttach(session.username, accountId, pushCredential(session), 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); @@ -1663,6 +1663,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 6b79345..7ec0f84 100644 --- a/server/src/push.ts +++ b/server/src/push.ts @@ -62,6 +62,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 (ihasmail#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..025f0dd --- /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 (ihasmail#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..70b1d27 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 (ihasmail#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;