import { HttpResponse } from "../http/index.js"; import type { HttpLayer } from "../layer/index.js"; import type { HttpService } from "../service/index.js"; /** * Level of compression data should be compressed with. * * Supports the following pre-defined values: * * - `"fastest"`: fastest quality of compression, usually produces bigger size. * - `"best"`: best quality of compression, usually produces the smallest size. * - `"default"`: default quality of compression defined by the selected * compression algorithm. * * You can also set this value to a `number` to define a precise quality based * on the underlying compression algorithms' qualities. * * The interpretation of this depends on the algorithm chosen and the specific * implementation backing it. * * Qualities are implicitly clamped to the algorithm's maximum. */ export type CompressionLevel = "fastest" | "best" | "default" | number; export type Predicate = (response: HttpResponse) => boolean; /** * A layer that compresses response bodies. * * This uses the `Accept-Encoding` header to pick an appropriate encoding and * adds the `Content-Encoding` header to responses. * * @example * ```ts * import { ResponseCompressionLayer } from "@taxum/core/middleware/compression"; * import { m, Router } from "@taxum/core/routing"; * * const router = new Router() * .route("/", m.get(() => "Hello World)) * .layer(new ResponseCompressionLayer()); * ``` */ export declare class ResponseCompressionLayer implements HttpLayer { private readonly accept; private predicate; private compressionLevel; /** * Creates a new {@link ResponseCompressionLayer}. * * By default, all encodings are accepted and the * {@link CompressionLevel | default compression level} is used. * * @see {@link DEFAULT_PREDICATE} */ constructor(); /** * Sets whether to support gzip encoding. */ gzip(enable: boolean): this; /** * Sets whether to support Deflate encoding. */ deflate(enable: boolean): this; /** * Sets whether to support Brotli encoding. */ br(enable: boolean): this; /** * Sets whether to support Zstd encoding. */ zstd(enable: boolean): this; /** * Sets the compression quality. */ quality(level: CompressionLevel): this; /** * Disabled support for gzip encoding. */ noGzip(): this; /** * Disables support for Deflate encoding. */ noDeflate(): this; /** * Disables support for Brotli encoding. */ noBr(): this; /** * Disables support for Zstd encoding. */ noZstd(): this; /** * Replace the current compression predicate. * * Predicates are used to determine whether a response should be compressed * or not. The default predicate is {@link DEFAULT_PREDICATE}. See its * documentation for more details on which responses it won't compress. * * Responses that are already compressed (i.e., have a `content-encoding` * header) will _never_ be recompressed, regardless of what the predicate * says. */ compressWhen(predicate: Predicate): this; layer(inner: HttpService): HttpService; } /** * Combination of multiple predicates. */ export declare const andPredicate: (predicates: Predicate[]) => Predicate; /** * Predicate that will only allow compression of responses above a certain size. * * The content size is determined through either the body size or the content-length header. * If neither can be determined (e.g., the body is a `Readable`), the response will always be compressed. */ export declare const sizeAbovePredicate: (minSize: number) => Predicate; /** * Predicate that won't allow responses with a specific `content-type` to be compressed. */ export declare const notForContentTypePredicate: (contentType: string, exception?: string) => Predicate; /** * Default predicate for response compression. * * All responses will be compressed unless: * * - They're gRPC, which has its own protocol-specific compression scheme. * - It's an image as determined by the `content-type` starting with * `image/`. * - They're Server-Sent Events (SSE) as determined by the `content-type` * being `text/event-stream`. * - The response is less than 32 */ export declare const DEFAULT_PREDICATE: Predicate;