Defjs
Core

ClientSSEOptions

Optional SSE settings applied when creating a client: fetch handle, invalid-event hook, and reconnect policy.

@defjs/core · Type

Signature

interface ClientSSEOptions {
  handle?: FetchHandle;
  onInvalidEvent?: (context: {
    reason: "missing-struct" | "validation-failed";
    message: { id: string; event: string; data: string };
    cause?: unknown;
    signal: AbortSignal;
  }) => unknown;
  reconnect?: {
    attempts?: number;
    delayMs?: number;
    factor?: number;
    jitter?: number;
    maxDelayMs?: number;
    shouldReconnect?: (context: {
      attempt: number;
      cause?: unknown;
      lastEventId: string;
      method: string;
      open?: {
        response: { status: number; statusText: string; url: string };
        url: string;
      };
    }) => boolean | Promise<boolean>;
  };
}
  • handle — Fetch implementation that opens the stream. The default resolves globalThis.fetch on each attempt, including replacements installed after import. Every SSE handle receives a Request. Missing global Fetch fails default execution with ENV_UNSUPPORTED; a custom handle remains usable.
  • onInvalidEvent — Called when an event has no matching struct and no default struct ('missing-struct'), or its data fails validation ('validation-failed'). The event is skipped. An error thrown by the hook does not close the stream.
  • reconnect — Opt-in. Without it the stream never reconnects. attempts defaults to 3 per outage; attempts: 0 disables retry. A successful handshake resets the budget, callback attempt, and backoff exponent. delayMs is the first base delay, 1000 by default, and a server-sent retry: value replaces it. factor defaults to 1. jitter is a 0–1 multiplicative factor and defaults to 0. maxDelayMs caps the final delay after jitter and rounding; without a configured cap, the platform limit of 2_147_483_647 ms still applies. A zero base stays zero; valid-number overflow saturates at the caps. Without shouldReconnect only GET, HEAD, OPTIONS, PUT, and DELETE reconnect; with it, its result decides for every method and its context carries method. A ReadableStream request body never reconnects. Reconnect sends the latest ID as UTF-8 bytes in Last-Event-ID; an empty ID clears it and an ID containing NUL is ignored.

Reconnect options are validated before interceptors or Fetch: attempts is a non-negative safe integer; delayMs must be finite and non-negative; maxDelayMs must be a non-negative finite number or positive Infinity, which removes the configured cap but not the platform limit of 2_147_483_647 ms; factor is finite and positive; jitter is finite and within 0–1. Invalid values return REQ_OPTIONS_INVALID when the operation runs.

Set these with withSSEHandle, withSSEOnInvalidEvent, and withSSEReconnect. Omitting withSSEReconnect disables retry.

Members

Prop

Type

handle? FetchHandle | undefined

Fetch implementation used to open the event stream; defaults to resolving global fetch on each call.

onInvalidEvent? ((context: { reason: "missing-struct" | "validation-failed"; message: { id: string; event: string; data: string; }; cause?: unknown; signal: AbortSignal; }) => unknown) | undefined

Called when an event has no matching struct or fails validation.

reconnect? { attempts?: number; delayMs?: number; factor?: number; jitter?: number; maxDelayMs?: number; shouldReconnect?: (context: { attempt: number; cause?: unknown; lastEventId: string; method: string; open?: { response: { status: number; statusText: string; url: string; }; url: string; }; }) => boolean | Promise<boolean>; } | undefined

Automatic reconnect policy after stream failures.

Edit this page on GitHub
Released under the MIT License. · Copyright © 2026 MunMunMiao