/** * Parser for Server-Sent Events (SSE) streams according to the HTML specification. * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation */ /** * Represents an event dispatched from an SSE stream. */ export interface ISSEEvent { /** * The event type. If not specified, the type is "message". */ type: string; /** * The event data. */ data: string; /** * The last event ID, used for reconnection. */ id?: string; /** * Reconnection time in milliseconds. */ retry?: number; } /** * Callback function type for event dispatch. */ export type SSEEventHandler = (event: ISSEEvent) => void; /** * Parser for Server-Sent Events (SSE) streams. */ export declare class SSEParser { private dataBuffer; private eventTypeBuffer; private currentEventId?; private lastEventIdBuffer?; private reconnectionTime?; private buffer; private endedOnCR; private readonly onEventHandler; private readonly decoder; /** * Creates a new SSE parser. * @param onEvent The callback to invoke when an event is dispatched. */ constructor(onEvent: SSEEventHandler); /** * Gets the last event ID received by this parser. */ getLastEventId(): string | undefined; /** * Gets the reconnection time in milliseconds, if one was specified by the server. */ getReconnectionTime(): number | undefined; /** * Feeds a chunk of the SSE stream to the parser. * @param chunk The chunk to parse as a Uint8Array of UTF-8 encoded data. */ feed(chunk: Uint8Array): void; /** * Processes a single line from the SSE stream. */ private processLine; /** * Processes a field with the given name and value. */ private processField; /** * Dispatches the event based on the current buffer states. */ private dispatchEvent; /** * Resets the parser state. */ reset(): void; }