/** * @youranalytics/web-sdk Network Adapter * Adaptive behavior for different network conditions * Optimized for 2G/3G networks with smart batching and compression */ import type { ConnectionType, RawEvent, EventBatch } from './types'; /** * Batching strategy based on network conditions. * - aggressive: Large batches, less frequent sends (slow networks) * - balanced: Medium batches, moderate frequency (3G) * - conservative: Small batches, frequent sends (fast networks) */ export type BatchingStrategy = 'aggressive' | 'balanced' | 'conservative'; /** * Adaptive settings computed from network conditions. */ export interface AdaptiveSettings { /** Maximum events to queue before forcing a flush */ maxQueueSize: number; /** Interval between automatic flushes in milliseconds */ flushInterval: number; /** Whether to compress event payloads */ compressionEnabled: boolean; /** Batching strategy name */ batchingStrategy: BatchingStrategy; } /** Event priority levels for request prioritization */ export declare const EventPriority: { readonly LOW: 0; readonly NORMAL: 1; readonly HIGH: 2; }; export type EventPriorityValue = (typeof EventPriority)[keyof typeof EventPriority]; /** * Event with priority information. */ export interface PrioritizedEvent extends RawEvent { priority: EventPriorityValue; } /** * Check if user has enabled data saver mode. * Data saver mode indicates the user wants to minimize data usage. * * @returns true if data saver is enabled */ export declare function isDataSaverEnabled(): boolean; /** * Get the round-trip time (RTT) in milliseconds. * Returns a default value if not available. * * @returns RTT in milliseconds */ export declare function getRtt(): number; /** * Get the downlink speed in Mbps. * Returns a default value if not available. * * @returns Downlink speed in Mbps */ export declare function getDownlink(): number; /** * Sort events by priority (high priority first). * Useful for slow connections where high-priority events should be sent first. * * @param events - Array of prioritized events * @returns Sorted array with high priority events first */ export declare function sortByPriority(events: PrioritizedEvent[]): PrioritizedEvent[]; /** * Assign priority to an event based on its type. * * @param event - Raw event to prioritize * @returns Event with priority assigned */ export declare function assignPriority(event: RawEvent): PrioritizedEvent; /** Smart retry strategy with exponential backoff. */ export declare class RetryStrategy { private attempts; private readonly maxRetries; constructor(connectionType?: ConnectionType); getBackoffDelay(): number; shouldRetry(error: Error): boolean; recordAttempt(): void; reset(): void; getAttempts(): number; getMaxRetries(): number; } /** * Network adapter that provides adaptive settings based on connection quality. * Listens for connection changes and notifies subscribers. */ export declare class NetworkAdapter { private currentSettings; private connection; private changeCallbacks; private boundHandleChange; private cachedConnectionType; private lastUpdateTime; private readonly cacheTimeout; /** * Create a new network adapter. * Automatically detects connection and sets up listeners. */ constructor(); /** * Compute settings based on current connection. */ private computeSettings; /** * Handle connection change event. */ private handleConnectionChange; /** * Get current connection type with caching. */ private getConnectionType; /** * Get current adaptive settings. * * @returns Current settings based on connection quality */ getSettings(): AdaptiveSettings; /** * Register a callback for connection changes. * Callback is invoked when connection quality changes. * * @param callback - Function to call with new settings * @returns Cleanup function to remove the callback */ onConnectionChange(callback: (settings: AdaptiveSettings) => void): () => void; /** * Check if compression should be used. * Based on connection type, CompressionStream availability, and data saver mode. * * @returns true if compression should be enabled */ shouldCompress(): boolean; /** * Get the optimal batch size for current connection. * Considers connection speed, RTT, and data saver mode. * * @returns Optimal batch size */ getOptimalBatchSize(): number; /** * Get a retry strategy appropriate for current connection. * * @returns RetryStrategy configured for connection */ getRetryStrategy(): RetryStrategy; /** * Check if network conditions are poor. * * @returns true if on slow connection */ isSlowConnection(): boolean; /** * Get recommended timeout for requests. * Based on RTT and connection type. * * @returns Timeout in milliseconds */ getRecommendedTimeout(): number; /** * Clean up the adapter. * Removes event listeners. */ destroy(): void; } /** * Send data with adaptive behavior based on network conditions. * Chooses the best transport method and applies compression as needed. * * @param url - Endpoint URL * @param data - Event batch to send * @param adapter - Network adapter for connection info */ export declare function sendAdaptive(url: string, data: EventBatch, adapter: NetworkAdapter): Promise; /** * Get or create the global network adapter instance. * * @returns Global NetworkAdapter instance */ export declare function getNetworkAdapter(): NetworkAdapter; /** * Reset the global adapter (for testing). */ export declare function resetNetworkAdapter(): void; //# sourceMappingURL=network-adapter.d.ts.map