import { ReadStream } from "node:fs"; import { Readable } from "node:stream"; import sharp from "sharp"; declare const FORMATS: readonly ["webp", "avif", "png", "jpeg", "jpg"]; export type Format = (typeof FORMATS)[number]; declare const FITS: readonly ["cover", "contain"]; export type Fit = (typeof FITS)[number]; export declare const DEFAULT_CACHE_FOLDER = "./data/images"; /** * ImgParams are the image optimization parameters from the incoming request * - width: The target width of the image. If not set, the original image's width will be used. * - height: The target height of the image. If not set, the original image's height will be used. * - fit: The fit mode for resizing the image: "cover" or "contain". Defaults to sharp's default "cover". * - format: The target format of the image: "webp", "avif", "png", "jpeg", or "jpg". If not specified, the format will be inferred from the source. */ export type ImgParams = { width?: number | undefined; height?: number | undefined; fit?: Fit | undefined; format?: Format | undefined; }; export type GetImgParamsArgs = { request: Request; }; /** * Called to get the image parameters for an incoming HTTP request. * The default implementation reads the parameters src, w (width), h (height), fit, and format from the search parameters. */ export type GetImgParams = (args: GetImgParamsArgs) => Promise | ImgParams | Response; /** * ImgData is the response body (ReadableStream), buffer, or other readable representation of an image. */ export type ImgData = ReadableStream> | Readable | Buffer | Uint8Array; /** * ImgSource describes where and how to retrieve the original image. * - type: The type of the source, either "fs" for local file system, "fetch" for remote URL, or "data" for supplying the image data directly. * - path: The path to the image if type is "fs". * - url: The URL to fetch the image from if type is "fetch", can be a relative path or an absolute URL. * - headers: Optional headers to be sent with the fetch request if type is "fetch". * - data: The image data if type is "data". * - cacheKey: If you provide custom image data and want the image to be cached, you need to provide your own cache key because there is no path or url to generate a cache key from. */ export type ImgSource = { type: "fs"; path: string; } | { type: "fetch"; url: string; headers?: HeadersInit | undefined; } | { type: "data"; data: ImgData; cacheKey: string | null; }; export type GetImgSourceArgs = { request: Request; params: ImgParams; }; /** * Called to get the source of the original image for a given request. * The default implementation uses the src ImgParams value to determine the source: * - If the src is a relative path, it is assumed to be a local file path and concatenated with './public'. * - If the src is an absolute URL, it is assumed to be a remote image. * Implement this function to customize the source retrieval logic. */ export type GetImgSource = (args: GetImgSourceArgs) => Promise | ImgSource | Response; export type GetSharpPipelineArgs = { params: ImgParams; source: ImgSource; }; export type SharpConfig = { pipeline: sharp.Sharp; cacheKey?: string; }; /** * Called to use a custom Sharp pipeline for a given request. * The default implementation uses the params and source to determine the pipeline: autoOrient, resize, format, etc. * Implement this function to return a custom Sharp pipeline for different image sources or parameters, * this is useful if you want to support custom image processing logic, e.g., support for black and white images, etc. * * Return undefined to use the default pipeline. * Return a custom SharpConfig for customizing the pipeline. * * Make sure to enforce custom URLs (e.g., /cat.png?no-colors=true) when returning a custom SharpConfig. * Otherwise, caching won't be able to differentiate between different pipelines and their output images. * * You must also return a custom cacheKey when file caching is enabled. * Otherwise, openimg's image cache won't be able to differentiate between different pipelines * and their output images. */ export type GetSharpPipeline = (args: GetSharpPipelineArgs) => Promise | SharpConfig | undefined; /** * Configuration values for the getImgResponse function. * - headers: Headers to be added to the response. Note that no caching headers will be added automatically. * - cacheFolder: Default: ".data/images". Set to "no_cache" for no caching. * - allowlistedOrigins: Default: []. List of allowed origins. If empty, no remote origins will be allowed and only relative pathnames are permitted (e.g., /cat.png). * Example allowlist: ['https://example.com', 'http://localhost:3000'] * Adding an '*' entry, ['*'], allows all remote origins. * - getImgSource: Provide a custom getImgSource function to map the request to a source path or url to the retrieve the original image. * - getImgParams: Provide a custom getImgParams function for more control over where to retrieve the image parameters from the request. */ export type Config = { headers?: HeadersInit; allowlistedOrigins?: string[]; getImgParams?: GetImgParams; getImgSource?: GetImgSource; getSharpPipeline?: GetSharpPipeline; cacheFolder?: string | "no_cache"; }; export declare function fromWebStream(stream: ReadableStream): Readable; export declare function toWebStream(readable: Readable | ReadStream): ReadableStream; export declare function getImgParams({ request, }: GetImgParamsArgs): ImgParams | Response; type GetCachePathArgs = { params: ImgParams; source: ImgSource; cacheFolder?: string | undefined | null; sharpConfig?: SharpConfig | undefined; }; export declare function getCachePath({ params, source, cacheFolder, sharpConfig, }: GetCachePathArgs): string; export declare function getImgSource({ request, }: GetImgSourceArgs): ImgSource | Response; export declare function validateImgSource(source: ImgSource, config: Config): Response | null; export declare function parseUrl(src: string): false | URL; export declare function getDefaultSharpPipeline(params: ImgParams): sharp.Sharp; export declare class PipelineLock { pipelines: Map; resolve: () => void; }>; get(cacheSrc: string): Promise | null; add(cacheSrc: string): void; resolve(cacheSrc: string | null): void; } /** * `invariant` is used to [assert](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that the `condition` is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy). * * 💥 `invariant` will `throw` an `Error` if the `condition` is [falsey](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) * * 🤏 `message`s are not displayed in production environments to help keep bundles small * * @example * * ```ts * const value: Person | null = { name: 'Alex' }; * invariant(value, 'Expected value to be a person'); * // type of `value`` has been narrowed to `Person` * ``` */ export default function invariant(condition: any, /** * Can provide a string, or a function that returns a string for cases where * the message takes a fair amount of effort to compute */ message?: string | (() => string)): asserts condition; export declare function getContentType(format: Format | undefined | string): string; export {};