()
| 35 | let imageCreatorModule: { default: SharpCreator } | null = null |
| 36 | |
| 37 | export async function getImageProcessor(): Promise<SharpFunction> { |
| 38 | if (imageProcessorModule) { |
| 39 | return imageProcessorModule.default |
| 40 | } |
| 41 | |
| 42 | if (isInBundledMode()) { |
| 43 | // Try to load the native image processor first |
| 44 | try { |
| 45 | // Use the native image processor module |
| 46 | const imageProcessor = await import('image-processor-napi') |
| 47 | const sharp = imageProcessor.sharp || imageProcessor.default |
| 48 | imageProcessorModule = { default: sharp } |
| 49 | return sharp |
| 50 | } catch { |
| 51 | // Fall back to sharp if native module is not available |
| 52 | // biome-ignore lint/suspicious/noConsole: intentional warning |
| 53 | console.warn( |
| 54 | 'Native image processor not available, falling back to sharp', |
| 55 | ) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Use sharp for non-bundled builds or as fallback. |
| 60 | // Single structural cast: our SharpFunction is a subset of sharp's actual type surface. |
| 61 | const imported = (await import( |
| 62 | 'sharp' |
| 63 | )) as unknown as MaybeDefault<SharpFunction> |
| 64 | const sharp = unwrapDefault(imported) |
| 65 | imageProcessorModule = { default: sharp } |
| 66 | return sharp |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get image creator for generating new images from scratch. |
no test coverage detected