/** * EventBatcher - Batched event transport for Node.js environments. * * Features: * - Batches events for efficient network usage * - Flushes on batch size or interval (whichever comes first) * - Graceful shutdown with final flush * - Error handling with configurable callback * * Unlike the browser EventLogger, this implementation: * - Does not use sendBeacon (Node.js doesn't have it) * - Does not persist failed events to storage * - Uses standard fetch for HTTP requests */ import type { TrackableEvent, OnSchemaWarnings } from "@traffical/core"; /** * Options for EventBatcher. */ export interface EventBatcherOptions { /** API endpoint for events */ endpoint: string; /** API key for authentication */ apiKey: string; /** Max events before auto-flush (default: 10) */ batchSize?: number; /** Auto-flush interval in ms (default: 30000) */ flushIntervalMs?: number; /** * Timeout in ms for the event batch POST (default: 10000). * On timeout the request is aborted and treated like a failed send: * events are re-queued for retry. */ requestTimeoutMs?: number; /** * Maximum number of events buffered in memory (default: 1000). When the * queue is full, the OLDEST event is dropped (a counter is bumped) — the * queue never grows without bound. Aligns with the Python SDK's model. */ maxQueueSize?: number; /** * Max retry attempts (after the first) for a transient delivery failure * (network error, timeout, 429, 5xx) before the batch is re-queued for a * later flush (default: 3). */ maxRetries?: number; /** Base for exponential retry backoff in ms: base * 2^(attempt-1) (default: 250). */ retryBackoffMs?: number; /** Callback on flush error */ onError?: (error: Error) => void; /** Enable debug logging */ debug?: boolean; /** Callback when schema validation warnings are received from the edge (dev-mode) */ onSchemaWarnings?: OnSchemaWarnings; } export declare class EventBatcher { private readonly _endpoint; private readonly _apiKey; private readonly _batchSize; private readonly _flushIntervalMs; private readonly _requestTimeoutMs; private readonly _maxQueueSize; private readonly _maxRetries; private readonly _retryBackoffMs; private readonly _onError?; private readonly _onSchemaWarnings?; private readonly _debug; private _queue; private _flushTimer; private _isFlushing; private _isDestroyed; /** Permanently true after an HTTP 401 kill-switch fires. */ private _isDisabled; /** Count of events dropped because the bounded queue overflowed. */ private _droppedCount; constructor(options: EventBatcherOptions); /** * Log an event (added to the bounded batch queue). When the queue is full * the OLDEST event is dropped (drop-oldest) so memory can't grow without * bound. After a 401 kill-switch, events are silently discarded. */ log(event: TrackableEvent): void; /** * Flush queued events immediately. * * Drains the queue in batch-sized chunks. Each batch is delivered with * exponential-backoff retry on transient failures (network/timeout/429/5xx). * A batch that still fails after `maxRetries` is re-queued at the FRONT * (bounded) and draining stops until the next flush. A non-retryable 4xx * drops the batch. An HTTP 401 permanently disables delivery and clears the * queue (auth kill-switch). */ flush(): Promise; /** * Delivers one batch with bounded exponential-backoff retry. * onError fires exactly once per batch that ends in a retry-later. */ private _deliverWithRetry; /** Re-queues a failed batch at the front, dropping oldest on overflow. */ private _requeueFront; private _disable; private _sleep; /** * Get the number of events in the queue. */ get queueSize(): number; /** Number of events dropped because the bounded queue overflowed. */ get droppedCount(): number; /** True once an HTTP 401 permanently disabled delivery. */ get isDisabled(): boolean; /** * Check if the batcher is destroyed. */ get isDestroyed(): boolean; /** * Destroy the batcher (cleanup timers and flush remaining events). */ destroy(): Promise; /** * Synchronous destroy (for process exit handlers). * Does not wait for flush to complete. */ destroySync(): void; /** * Sends one batch and returns the HTTP status code. Throws only on a * transport-level failure (network error / abort), which the caller treats * as a transient, retryable error. HTTP status classification (2xx / 401 / * 4xx / 5xx) is done by the caller. */ private _sendEvents; private _startFlushTimer; private _log; } //# sourceMappingURL=event-batcher.d.ts.map