Files
xo-cash-utils/source/sse-session/constants.ts
T
2026-09-17 07:51:48 +00:00

58 lines
1.8 KiB
TypeScript

/**
* Regex that splits decoded SSE text into lines.
*
* The SSE wire format is line-oriented (`field: value` per line). Servers may
* send `\r\n` (HTTP default), `\n` (Unix), or `\r` (legacy Mac). Matching all
* three keeps parsing correct regardless of platform or server implementation.
*/
export const SSE_LINE_ENDINGS = /\r\n|\r|\n/;
/**
* Regex that matches the single optional leading space in an SSE field value.
*
* Per the SSE spec, `field: value` may include one space immediately after the
* colon; that space is not part of the value. Used with `.replace()` to strip
* it when parsing lines such as `data: hello` → `hello`.
*/
export const SSE_FIELD_VALUE_REGEX = /^ /;
/**
* Regex that matches a trailing newline at the end of a string.
*
* Multiple `data:` lines in one event are joined with `\n`. When the event is
* completed, this removes any stray trailing newline so callers receive the
* payload without an extra line break at the end.
*/
export const SSE_TRAILING_NEWLINE_REGEX = /\n$/;
/**
* The newline character used when normalizing SSE text internally.
*
* Used to join consecutive `data:` lines into one payload and to reassemble
* buffered partial lines between streamed chunks before the next parse call.
*/
export const NEW_LINE = '\n';
/**
* Default exponential backoff options for the SSE session.
*/
export const SSE_SESSION_EXPONENTIAL_BACKOFF_DEFAULTS = {
baseDelay: 250,
maxDelay: 10000,
maxAttempts: 0,
growthRate: 1.3,
jitter: 0.3,
} as const;
/**
* Default attempt reconnect flag for the SSE session.
* @default true
*/
export const SSE_SESSION_ATTEMPT_RECONNECT_DEFAULT = true;
/**
* Default persistent flag for the SSE session.
* @default false
*/
export const SSE_SESSION_PERSISTENT_DEFAULT = false;