UNPKG

@nanggo/social-preview

Version:

Generate beautiful social media preview images from any URL

130 lines (129 loc) 3.9 kB
/** * Sharp Operation Caching System * Optimizes performance by caching frequently used Sharp operations * * Benefits: * - SVG parsing cache reduces overhead for text overlays (80-90% reduction) * - Metadata cache prevents duplicate image analysis (60-80% reduction) * - Canvas cache reuses common background patterns (50-70% reduction) * * Performance: * - O(1) LRU operations using Map's insertion order property * - Efficient cache eviction without O(N) scans * - TTL-based cleanup to prevent memory leaks */ import sharp, { Sharp } from 'sharp'; interface CacheOptions { maxSize?: number; maxAge?: number; idleTimeout?: number; cleanupInterval?: number; } export interface CanvasOptions { colors?: { background?: string; accent?: string; text?: string; }; background?: string; } /** * High-performance LRU cache with TTL support * Uses Map's insertion order property for O(1) LRU operations */ declare class LRUCache<T> { private cache; private readonly maxSize; private readonly maxAge; private readonly idleTimeout; private cleanupInterval; constructor(options?: CacheOptions); get(key: string): T | undefined; set(key: string, value: T): void; private cleanup; getStats(): { size: number; maxSize: number; totalHits: number; averageAge: number; }; clear(): void; destroy(): void; } /** * SVG Cache for text overlays and graphics */ declare class SVGCache extends LRUCache<Buffer> { constructor(); getCachedSVG(svgContent: string): Buffer | undefined; cacheSVG(svgContent: string): Buffer; private generateSVGKey; } /** * Metadata Cache for image analysis */ declare class MetadataCache extends LRUCache<sharp.Metadata> { constructor(); getCachedMetadata(imageBuffer: Buffer): sharp.Metadata | undefined; cacheMetadata(imageBuffer: Buffer, metadata: sharp.Metadata): void; private generateBufferKey; } /** * Canvas Cache for common backgrounds and patterns * Caches SVG content instead of Sharp instances for better compatibility */ declare class CanvasCache extends LRUCache<string> { constructor(); getCachedCanvas(width: number, height: number, options: CanvasOptions): Sharp | undefined; cacheCanvas(width: number, height: number, options: CanvasOptions, svgContent: string): void; private generateCanvasKey; } export declare const svgCache: SVGCache; export declare const metadataCache: MetadataCache; export declare const canvasCache: CanvasCache; /** * Cached SVG processing with automatic cache management */ export declare function createCachedSVG(svgContent: string): Promise<Sharp>; /** * Cached metadata extraction */ export declare function getCachedMetadata(imageBuffer: Buffer): Promise<sharp.Metadata>; /** * Cached canvas creation for common backgrounds */ export declare function createCachedCanvas(width: number, height: number, options: CanvasOptions): Promise<Sharp>; /** * Get comprehensive cache statistics */ export declare function getCacheStats(): { svg: { size: number; maxSize: number; totalHits: number; averageAge: number; }; metadata: { size: number; maxSize: number; totalHits: number; averageAge: number; }; canvas: { size: number; maxSize: number; totalHits: number; averageAge: number; }; }; /** * Clear all caches (for testing or memory management) */ export declare function clearAllCaches(): void; /** * Graceful shutdown - cleanup intervals and clear caches */ export declare function shutdownSharpCaches(): void; export declare function registerCacheShutdownHandlers(): void; export declare function unregisterCacheShutdownHandlers(): void; export { registerCacheShutdownHandlers as enableCacheShutdownHandlers };