import { AssetMetadata, AssetType, AssetLoaderConfig, CacheOptions, CacheResult } from './types.js'; /** * Asset Cache Error Types * * Provides typed error handling for all cache operations. * Each error type corresponds to a specific failure mode in the caching system. */ /** * Error codes for cache operations */ declare enum CacheErrorCode { CACHE_API_NOT_SUPPORTED = "CACHE_API_NOT_SUPPORTED", CACHE_NOT_INITIALIZED = "CACHE_NOT_INITIALIZED", CACHE_OPEN_FAILED = "CACHE_OPEN_FAILED", MANIFEST_NOT_FOUND = "MANIFEST_NOT_FOUND", MANIFEST_PARSE_ERROR = "MANIFEST_PARSE_ERROR", MANIFEST_SAVE_FAILED = "MANIFEST_SAVE_FAILED", ASSET_NOT_CACHED = "ASSET_NOT_CACHED", ASSET_EXPIRED = "ASSET_EXPIRED", ASSET_FETCH_FAILED = "ASSET_FETCH_FAILED", ASSET_TOO_LARGE = "ASSET_TOO_LARGE", ASSET_INVALID_RESPONSE = "ASSET_INVALID_RESPONSE", SERVICE_WORKER_NOT_SUPPORTED = "SERVICE_WORKER_NOT_SUPPORTED", SERVICE_WORKER_REGISTRATION_FAILED = "SERVICE_WORKER_REGISTRATION_FAILED", SERVICE_WORKER_COMMUNICATION_FAILED = "SERVICE_WORKER_COMMUNICATION_FAILED", STORAGE_QUOTA_EXCEEDED = "STORAGE_QUOTA_EXCEEDED", STORAGE_ACCESS_DENIED = "STORAGE_ACCESS_DENIED", UNKNOWN_ERROR = "UNKNOWN_ERROR" } /** * Base class for all cache-related errors */ declare class CacheError extends Error { readonly code: CacheErrorCode; readonly recoverable: boolean; readonly details?: Record; readonly originalCause?: Error; constructor(code: CacheErrorCode, message: string, options?: { recoverable?: boolean; details?: Record; cause?: Error; }); /** * Convert error to JSON for logging/serialization */ toJSON(): Record; } /** * Error thrown when Cache API is not supported in the browser */ declare class CacheAPINotSupportedError extends CacheError { constructor(); } /** * Error thrown when cache operations are attempted before initialization */ declare class CacheNotInitializedError extends CacheError { constructor(operation?: string); } /** * Error thrown when opening the cache fails */ declare class CacheOpenFailedError extends CacheError { constructor(cacheName: string, cause?: Error); } /** * Error thrown when manifest is not found in cache */ declare class ManifestNotFoundError extends CacheError { constructor(); } /** * Error thrown when manifest JSON parsing fails */ declare class ManifestParseError extends CacheError { constructor(cause?: Error); } /** * Error thrown when saving manifest fails */ declare class ManifestSaveFailedError extends CacheError { constructor(cause?: Error); } /** * Error thrown when requested asset is not in cache */ declare class AssetNotCachedError extends CacheError { constructor(url: string); } /** * Error thrown when cached asset has expired */ declare class AssetExpiredError extends CacheError { constructor(url: string, expiredAt: Date); } /** * Error thrown when fetching asset from network fails */ declare class AssetFetchFailedError extends CacheError { constructor(url: string, status?: number, statusText?: string, cause?: Error); } /** * Error thrown when asset exceeds maximum cacheable size */ declare class AssetTooLargeError extends CacheError { constructor(url: string, size: number, maxSize: number); } /** * Error thrown when response is invalid (status >= 400) */ declare class AssetInvalidResponseError extends CacheError { constructor(url: string, status: number, statusText: string); } /** * Error thrown when Service Worker API is not supported */ declare class ServiceWorkerNotSupportedError extends CacheError { constructor(); } /** * Error thrown when Service Worker registration fails */ declare class ServiceWorkerRegistrationFailedError extends CacheError { constructor(path: string, cause?: Error); } /** * Error thrown when communication with Service Worker fails */ declare class ServiceWorkerCommunicationFailedError extends CacheError { constructor(messageType: string, cause?: Error); } /** * Error thrown when storage quota is exceeded */ declare class StorageQuotaExceededError extends CacheError { constructor(cause?: Error); } /** * Error thrown when storage access is denied */ declare class StorageAccessDeniedError extends CacheError { constructor(cause?: Error); } /** * Check if an error is a storage quota exceeded error */ declare function isQuotaExceededError(error: unknown): boolean; /** * Check if an error is a storage access denied error */ declare function isStorageAccessDeniedError(error: unknown): boolean; /** * Wrap an unknown error in a CacheError */ declare function wrapError(error: unknown, context?: string): CacheError; /** * Cache manager module * * This module: * - Manages cache operations * - Handles cache invalidation * - Provides utilities for working with the cache * - Handles all error states with typed errors */ /** * Initialization state of the cache manager */ declare enum CacheManagerState { UNINITIALIZED = "UNINITIALIZED", INITIALIZING = "INITIALIZING", READY = "READY", ERROR = "ERROR", UNSUPPORTED = "UNSUPPORTED" } /** * Result of initialization attempt */ interface InitResult { success: boolean; state: CacheManagerState; error?: CacheError; } /** * Cache manager class with proper error handling */ declare class CacheManager { private static cache; private static manifest; private static state; private static lastError; private static initPromise; /** * Get the current state of the cache manager */ static getState(): CacheManagerState; /** * Get the last error that occurred */ static getLastError(): CacheError | null; /** * Check if the cache manager is ready for operations */ static isReady(): boolean; /** * Check if caching is supported in the current environment */ static isSupported(): boolean; /** * Initialize the cache manager * @param cacheName Name of the cache to use * @returns Promise resolving to initialization result */ static init(cacheName?: string): Promise; /** * Perform the actual initialization */ private static performInit; /** * Handle initialization errors and convert to typed errors */ private static handleInitError; /** * Reset the cache manager (useful for recovery or testing) */ static reset(): void; /** * Attempt to recover from an error state * @param cacheName Name of the cache to use */ static recover(cacheName?: string): Promise; /** * Load the cache manifest */ static loadManifest(): Promise; /** * Create a new empty manifest */ private static createNewManifest; /** * Ensure the cache manager is initialized */ private static ensureInitialized; /** * Save the cache manifest */ static saveManifest(): Promise; /** * Add an asset to the manifest * @param url URL of the asset * @param metadata Asset metadata */ static addToManifest(url: string, metadata: Partial): Promise; /** * Remove an asset from the manifest * @param url URL of the asset */ static removeFromManifest(url: string): Promise; /** * Get asset metadata from the manifest * @param url URL of the asset * @returns Asset metadata or null if not found */ static getAssetMetadata(url: string): Promise; /** * Get all assets of a specific type * @param type Asset type * @returns Array of asset metadata */ static getAssetsByType(type: AssetType): Promise; /** * Get all assets with a specific tag * @param tag Tag to filter by * @returns Array of asset metadata */ static getAssetsByTag(tag: string): Promise; /** * Get cache statistics * @returns Cache statistics */ static getStats(): Promise<{ totalAssets: number; totalSize: number; assetsByType: Record; formattedSize: string; state: CacheManagerState; lastError: CacheError | null; }>; /** * Get empty stats object */ private static getEmptyStats; /** * Clear the cache and manifest */ static clear(): Promise; /** * Purge expired assets from the cache * @returns Number of assets purged */ static purgeExpired(): Promise; } /** * Asset loader module * * This module: * - Provides methods for loading assets with caching * - Handles preloading of assets * - Manages cache invalidation */ /** * Asset loader state */ declare enum AssetLoaderState { UNINITIALIZED = "UNINITIALIZED", INITIALIZING = "INITIALIZING", READY = "READY", ERROR = "ERROR", CACHE_UNAVAILABLE = "CACHE_UNAVAILABLE" } /** * Asset loader initialization result */ interface AssetLoaderInitResult { success: boolean; state: AssetLoaderState; cacheManagerState: CacheManagerState; error?: CacheError; } /** * Asset loader class */ declare class AssetLoader { private static state; private static lastError; private static initPromise; private static config; /** * Get the current state of the asset loader */ static getState(): AssetLoaderState; /** * Get the last error that occurred */ static getLastError(): CacheError | null; /** * Check if the asset loader is ready */ static isReady(): boolean; /** * Check if caching is available (even if degraded) */ static isCacheAvailable(): boolean; /** * Initialize the asset loader * @param config Configuration options * @returns Initialization result with state information */ static init(config?: Partial): Promise; /** * Perform the actual initialization */ private static performInit; /** * Reset the asset loader state (primarily for testing) */ static reset(): void; /** * Attempt to recover from an error state * @param config Configuration options * @returns Recovery result */ static recover(config?: Partial): Promise; /** * Ensure the asset loader is initialized * @throws {CacheNotInitializedError} If initialization fails */ private static ensureInitialized; /** * Preload an asset * @param url URL of the asset to preload * @param options Caching options * @throws {CacheError} If preloading fails */ static preload(url: string, options?: CacheOptions): Promise; /** * Load an asset with caching * @param url URL of the asset to load * @param options Caching options * @returns Promise resolving to the asset data with typed error information */ static load(url: string, options?: CacheOptions): Promise>; /** * Check if an asset is cached * @param url URL of the asset * @returns Whether the asset is cached */ static isCached(url: string): Promise; /** * Invalidate a cached asset * @param url URL of the asset to invalidate * @throws {CacheError} If invalidation fails */ static invalidate(url: string): Promise; /** * Invalidate cached assets by tag * @param tag Tag to invalidate * @throws {CacheError} If invalidation fails */ static invalidateByTag(tag: string): Promise; /** * Store an asset in the cache * @param url URL of the asset * @param entry Cache entry * @throws {CacheError} If storage fails (e.g., quota exceeded) */ private static storeInCache; /** * Get an asset from the cache * @param url URL of the asset * @returns Cache entry or null if not found */ private static getFromCache; /** * Remove an asset from the cache * @param url URL of the asset * @throws {CacheError} If removal fails */ private static removeFromCache; /** * Revalidate an asset in the background * Background revalidation errors are logged but not thrown * @param url URL of the asset * @param options Caching options */ private static revalidateInBackground; } export { AssetLoader as A, CacheManager as C, type InitResult as I, ManifestNotFoundError as M, ServiceWorkerNotSupportedError as S, AssetLoaderState as a, type AssetLoaderInitResult as b, CacheManagerState as c, CacheErrorCode as d, CacheError as e, CacheAPINotSupportedError as f, CacheNotInitializedError as g, CacheOpenFailedError as h, ManifestParseError as i, ManifestSaveFailedError as j, AssetNotCachedError as k, AssetExpiredError as l, AssetFetchFailedError as m, AssetTooLargeError as n, AssetInvalidResponseError as o, ServiceWorkerRegistrationFailedError as p, ServiceWorkerCommunicationFailedError as q, StorageQuotaExceededError as r, StorageAccessDeniedError as s, isQuotaExceededError as t, isStorageAccessDeniedError as u, wrapError as w };