/** * @module SseFrameParser * Turns the raw text of a `text/event-stream` response into complete SSE frames. * * A streamed response arrives in arbitrary chunks. A single frame is regularly split across two * chunks, and two frames regularly arrive in one chunk. The parser buffers whatever is incomplete * so the caller only ever sees whole frames. * * Internal to the http module. Not part of the public API. * * @example * const parser = new SseFrameParser(); * parser.push('event: token\ndata: {"te'); // [] * parser.push('xt":"hi"}\n\n'); // [{ event: 'token', data: '{"text":"hi"}' }] */ /** * One complete event received from the server. */ export interface SseFrame { /** * Name from the `event:` field, or `message` when the server did not send one. */ event: string; /** * Payload from the `data:` field. Several `data:` lines are joined with a newline. */ data: string; /** * Value of the `id:` field, when the server sent one for this frame. */ id?: string; /** * Reconnection delay in milliseconds from the `retry:` field. */ retry?: number; } /** * Parses `text/event-stream` text into frames. * * A frame that is still incomplete when the stream ends is discarded, as the event stream * specification requires. Half a JSON payload is worse than no payload. */ export declare class SseFrameParser { private buffer; private eventName; private data; private id?; private retry?; /** * Feed the next piece of the response body in. * * @param chunk - Decoded text, of any length and split at any position. * @returns Every frame that became complete with this chunk, in arrival order. */ push(chunk: string): SseFrame[]; /** * Locates the next line terminator, which may be LF, CRLF or a lone CR. * * A CR at the very end of the buffer is left unresolved: the next chunk decides whether it was * a lone CR or the first half of a CRLF. */ private findLineBreak; private readField; private takeFrame; private reset; }