we (web engine): Experimental web browser project to understand the limits of Claude
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement EventSource (Server-Sent Events) #188

open opened by pierrelf.com

Goal#

Implement the EventSource interface for consuming text/event-stream server pushes, including automatic reconnection and event ID tracking.

Depends on: "Integrate Streams API with fetch: Response.body and streaming Request bodies" (uses streaming HTTP responses).

Scope#

  • text/event-stream parser (WHATWG HTML §9.2.6)
    • Fields: data:, event:, id:, retry:, comments (: prefix)
    • Multi-line data: accumulates with a newline separator
    • Event dispatch on blank line; reset buffers between events
    • id: field updates the connection's lastEventId; an explicit id: with no value clears it; an id: field containing a NUL byte is ignored
    • retry: field updates the reconnection time (must be a non-negative integer of milliseconds)
    • Decoder: UTF-8 with replacement, tolerant of CR/LF/CRLF line endings
  • EventSource interface
    • new EventSource(url, { withCredentials = false }?)
    • Properties: url, readyState (CONNECTING = 0, OPEN = 1, CLOSED = 2), withCredentials
    • close() permanently closes the connection
    • onopen, onmessage, onerror plus addEventListener equivalents
    • Custom event types via the event: field are dispatched as their own type
  • HTTP behaviour
    • Request: Accept: text/event-stream, Cache-Control: no-store, send Last-Event-ID header on reconnect when one is known
    • Reject non-text/event-stream responses with a CLOSED failure (no reconnect)
    • On network error or successful close, schedule a reconnect with the current retry timeout (default 3 s, capped to a reasonable maximum)
    • 204 / 401 / 403 / 404 / 410 / 5xx and many other non-2xx statuses follow the spec's reconnect-or-fail rules — implement the failure case (no reconnect) per spec for the obvious terminal codes
    • CORS: respect withCredentials, send Origin, accept Access-Control-Allow-Origin / Access-Control-Allow-Credentials
  • Lifecycle
    • GC root active EventSource instances similar to WebSocket

Acceptance Criteria#

  • Connects to a text/event-stream endpoint and dispatches message events as the server pushes data
  • Multi-line data: blocks are joined with \\n
  • event: foo\\ndata: x\\n\\n dispatches a custom foo event with data === 'x'
  • After a server disconnect, the client reconnects automatically using the configured retry timeout and sends Last-Event-ID
  • close() ends the connection and prevents further reconnects
  • Cross-origin requests with withCredentials: false succeed only when the server returns the required CORS headers
  • Unit tests for parser edge cases (CR/LF/CRLF mixing, multi-line data, retry parsing, NUL in id, comments) + an integration test against an in-process SSE server
  • Conventions
    • cargo fmt --all --check
    • cargo clippy --workspace -- -D warnings
    • cargo test --workspace
sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mly7sdc2n72v