/** * @file Network Status Utilities * @description Online/offline detection, connection quality monitoring, * and network-aware request handling */ /** * Network connection type */ export type ConnectionType = 'wifi' | '4g' | '3g' | '2g' | 'slow-2g' | 'ethernet' | 'bluetooth' | 'cellular' | 'none' | 'unknown'; /** * Network effective type (speed-based) */ export type EffectiveType = 'slow-2g' | '2g' | '3g' | '4g'; /** * Network status information */ export interface NetworkStatus { /** Whether the browser is online */ online: boolean; /** Connection type */ type: ConnectionType; /** Effective connection type (based on measured speed) */ effectiveType: EffectiveType; /** Downlink speed in Mbps */ downlink: number; /** Round-trip time in ms */ rtt: number; /** Whether data saver mode is enabled */ saveData: boolean; /** Timestamp of last status update */ timestamp: number; } /** * Network quality tier */ export type NetworkQuality = 'excellent' | 'good' | 'fair' | 'poor' | 'offline'; /** * Network status change callback */ export type NetworkStatusCallback = (status: NetworkStatus) => void; /** * Network quality change callback */ export type NetworkQualityCallback = (quality: NetworkQuality) => void; /** * Network monitor configuration */ export interface NetworkMonitorConfig { /** Ping endpoint for connectivity check */ pingEndpoint?: string; /** Ping interval in ms */ pingInterval?: number; /** Ping timeout in ms */ pingTimeout?: number; /** Enable periodic pinging */ enablePing?: boolean; /** Downlink threshold for "good" quality (Mbps) */ goodDownlinkThreshold?: number; /** RTT threshold for "good" quality (ms) */ goodRttThreshold?: number; } /** * Network status monitor with quality detection and event handling */ export declare class NetworkMonitor { private config; private statusCallbacks; private qualityCallbacks; private currentStatus; private currentQuality; private pingIntervalId; private disposed; constructor(config?: NetworkMonitorConfig); /** * Start monitoring */ start(): void; /** * Stop monitoring */ stop(): void; /** * Perform connectivity ping */ ping(): Promise; /** * Get current status */ getStatus(): NetworkStatus; /** * Get current quality */ getQuality(): NetworkQuality; /** * Check if online */ isOnline(): boolean; /** * Check if connection is metered/slow */ isSlowConnection(): boolean; /** * Subscribe to status changes */ onStatusChange(callback: NetworkStatusCallback): () => void; /** * Subscribe to quality changes */ onQualityChange(callback: NetworkQualityCallback): () => void; /** * Wait for online status */ waitForOnline(timeout?: number): Promise; /** * Dispose resources */ dispose(): void; /** * Get the Network Information API connection object */ private getConnection; /** * Get current network status */ private getNetworkStatus; /** * Calculate network quality tier */ private calculateQuality; /** * Setup event listeners */ private setupListeners; /** * Handle online event */ private handleOnline; /** * Handle offline event */ private handleOffline; /** * Handle connection change event */ private handleConnectionChange; /** * Update status and notify callbacks */ private updateStatus; } /** * Network-aware fetch options */ export interface NetworkAwareFetchOptions extends RequestInit { /** Whether to queue request when offline */ queueOffline?: boolean; /** Minimum quality required to send request */ minQuality?: NetworkQuality; /** Timeout for waiting online */ onlineTimeout?: number; /** Enable automatic retry on network recovery */ retryOnRecovery?: boolean; } /** * Network-aware fetch wrapper */ export declare class NetworkAwareFetch { private monitor; private offlineQueue; private unsubscribe; constructor(monitor: NetworkMonitor); /** * Make network-aware fetch request */ fetch(url: string, options?: NetworkAwareFetchOptions): Promise; /** * Get queue size */ getQueueSize(): number; /** * Clear offline queue */ clearQueue(): void; /** * Dispose resources */ dispose(): void; /** * Setup handler for network recovery */ private setupRecoveryHandler; /** * Process queued requests */ private processQueue; /** * Queue request for later execution */ private queueRequest; } /** * Network-related error */ export declare class NetworkError extends Error { readonly reason: 'offline' | 'timeout' | 'quality' | 'unknown'; constructor(message: string, reason: 'offline' | 'timeout' | 'quality' | 'unknown'); } /** * Check if error is a network error */ export declare function isNetworkError(error: unknown): error is NetworkError; /** * Check if error is due to offline status */ export declare function isOfflineError(error: unknown): boolean; /** * Get suggested action based on network quality */ export declare function getSuggestedAction(quality: NetworkQuality): { loadImages: boolean; prefetch: boolean; useCache: boolean; reducedMotion: boolean; }; /** * Global network monitor instance */ export declare const networkMonitor: NetworkMonitor; /** * Global network-aware fetch instance */ export declare const networkAwareFetch: NetworkAwareFetch; /** * Check if browser is online */ export declare function isOnline(): boolean; /** * Check if connection is slow */ export declare function isSlowConnection(): boolean; /** * Get current network quality */ export declare function getNetworkQuality(): NetworkQuality; /** * Wait for online status */ export declare function waitForOnline(timeout?: number): Promise;