@codefast/image-loader
Version:
Simple, functional image loader for Next.js supporting multiple CDN providers
72 lines (71 loc) • 2.17 kB
TypeScript
import type { ImageLoaderProps } from "next/image";
import type { LoaderConfig, LoaderFunction } from "./types";
/**
* Optimized image loader with domain-based registry and caching
*
* Performance optimizations:
* - O(1) domain-based lookup instead of O(n) linear search
* - URL parsing cache to avoid repeated parsing
* - Result caching for frequently accessed URLs
* - Smart domain extraction for faster matching
*/
export declare class ImageLoader {
private readonly domainRegistry;
private readonly fallbackLoaders;
private readonly fallbackLoader?;
private readonly urlCache;
private readonly resultCache;
private readonly maxCacheSize;
constructor(config?: LoaderConfig[], fallbackLoader?: LoaderFunction, maxCacheSize?: number);
/**
* Register a new loader
*/
registerLoader(name: string, matcher: (src: string) => boolean, loader: LoaderFunction): void;
/**
* Transform image URL using optimized registry lookup
*/
transform(params: ImageLoaderProps): string;
/**
* Get all registered loader names
*/
getRegisteredLoaders(): string[];
/**
* Check if a loader is registered
*/
hasLoader(name: string): boolean;
/**
* Get cache statistics for debugging
*/
getCacheStats(): {
urlCacheSize: number;
resultCacheSize: number;
};
/**
* Clear caches (useful for testing or memory management)
*/
clearCaches(): void;
/**
* Initialize the domain-based registry for O(1) lookup
*/
private initializeRegistry;
/**
* Extract domain from matcher function for optimization
*/
private extractDomainFromMatcher;
/**
* Extract domain from URL for fast lookup
*/
private extractDomainFromUrl;
/**
* Parse URL with caching to avoid repeated parsing
*/
private parseURL;
/**
* Get cached result or compute and cache
*/
private getCachedResult;
}
/**
* Create a new image loader instance
*/
export declare function createImageLoader(config?: LoaderConfig[], fallbackLoader?: LoaderFunction, maxCacheSize?: number): ImageLoader;