import type { Buffer } from 'buffer'import { isInBundledMode } from '../../utils/bundledMode.js'export type SharpInstance = {metadata(): Promise<{ width: number; height: number; format: string }>resize(width: number,height: number,options?: { fit?: string; withoutEnlargement?: boolean },): SharpInstancejpeg(options?: { quality?: number }): SharpInstancepng(options?: {compressionLevel?: numberpalette?: booleancolors?: number}): SharpInstancewebp(options?: { quality?: number }): SharpInstancetoBuffer(): Promise<Buffer>}export type SharpFunction = (input: Buffer) => SharpInstancetype SharpCreatorOptions = {create: {width: numberheight: numberchannels: 3 | 4background: { r: number; g: number; b: number }}}type SharpCreator = (options: SharpCreatorOptions) => SharpInstancelet imageProcessorModule: { default: SharpFunction } | null = nulllet imageCreatorModule: { default: SharpCreator } | null = nullexport async function getImageProcessor(): Promise<SharpFunction> {if (imageProcessorModule) {return imageProcessorModule.default}if (isInBundledMode()) {// Try to load the native image processor firsttry {// Use the native image processor moduleconst imageProcessor = await import('image-processor-napi')const sharp = imageProcessor.sharp || imageProcessor.defaultimageProcessorModule = { default: sharp }return sharp} catch {// Fall back to sharp if native module is not available// biome-ignore lint/suspicious/noConsole: intentional warningconsole.warn('Native image processor not available, falling back to sharp',)}}// Use sharp for non-bundled builds or as fallback.// Single structural cast: our SharpFunction is a subset of sharp's actual type surface.const imported = (await import('sharp')) as unknown as MaybeDefault<SharpFunction>const sharp = unwrapDefault(imported)imageProcessorModule = { default: sharp }return sharp}/*** Get image creator for generating new images from scratch.* Note: image-processor-napi doesn't support image creation,* so this always uses sharp directly.*/export async function getImageCreator(): Promise<SharpCreator> {if (imageCreatorModule) {return imageCreatorModule.default}const imported = (await import('sharp')) as unknown as MaybeDefault<SharpCreator>const sharp = unwrapDefault(imported)imageCreatorModule = { default: sharp }return sharp}// Dynamic import shape varies by module interop mode — ESM yields { default: fn }, CJS yields fn directly.type MaybeDefault<T> = T | { default: T }function unwrapDefault<T extends (...args: never[]) => unknown>(mod: MaybeDefault<T>,): T {return typeof mod === 'function' ? mod : mod.default}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。