UNPKG

7.76 kBTypeScriptView Raw
1import express = require("express");
2
3// This module adds a res.flush() method to force the partially-compressed response to be flushed to the client.
4
5declare global {
6 namespace Express {
7 interface Response {
8 /**
9 * Forces the partially-compressed response to be flushed to the client.
10 */
11 flush(): void;
12 }
13 }
14}
15/**
16 * Returns the compression middleware using the given `options`. The middleware will attempt to compress response bodies
17 * for all request that traverse through the middleware, based on the given `options`.
18 *
19 * This middleware will never compress responses that include a `Cache-Control` header with the `no-transform` directive,
20 * as compressing will transform the body.
21 *
22 * @see {@link https://github.com/expressjs/compression#compressionoptions|`compression([options]) documentation`}
23 */
24declare function compression(options?: compression.CompressionOptions): express.RequestHandler;
25
26declare namespace compression {
27 /**
28 * The default `filter` function. This is used to construct a custom filter function that is an extension of the default function.
29 *
30 * ```js
31 * var compression = require('compression')
32 * var express = require('express')
33 *
34 * var app = express()
35 * app.use(compression({ filter: shouldCompress }))
36 *
37 * function shouldCompress (req, res) {
38 * if (req.headers['x-no-compression']) {
39 * // don't compress responses with this request header
40 * return false
41 * }
42 *
43 * // fallback to standard filter function
44 * return compression.filter(req, res)
45 * }
46 * ```
47 *
48 * @see {@link https://github.com/expressjs/compression#filter-1|`.filter` documentation}
49 */
50 function filter(req: express.Request, res: express.Response): boolean;
51
52 /**
53 * A function to decide if the response should be considered for compression.
54 */
55 interface CompressionFilter {
56 (req: express.Request, res: express.Response): boolean;
57 }
58
59 /**
60 * compression() accepts these properties in the options object.
61 * In addition to those listed below, `zlib` options may be passed in to the options object.
62 */
63 interface CompressionOptions {
64 /**
65 * @default zlib.constants.Z_DEFAULT_CHUNK or 16384
66 * @see {@link https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning| Node.js documentation}
67 * @see {@link https://github.com/expressjs/compression#chunksize|chunkSize documentation}
68 */
69 chunkSize?: number | undefined;
70
71 /**
72 * A function to decide if the response should be considered for compression. This function is called as
73 * `filter(req, res)` and is expected to return `true` to consider the response for compression, or `false` to
74 * not compress the response.
75 *
76 * The default filter function uses the `compressible` module to determine if `res.getHeader('Content-Type')`
77 * is compressible.
78 *
79 * @see {@link https://github.com/expressjs/compression#filter|`filter` documentation}
80 * @see {@link https://www.npmjs.com/package/compressible|compressible module}
81 */
82 filter?: CompressionFilter | undefined;
83
84 /**
85 * The level of zlib compression to apply to responses. A higher level will result in better compression, but
86 * will take longer to complete. A lower level will result in less compression, but will be much faster.
87 *
88 * This is an integer in the range of `0` (no compression) to `9` (maximum compression). The special value `-1`
89 * can be used to mean the "default compression level", which is a default compromise between speed and
90 * compression (currently equivalent to level 6).
91 *
92 * - `-1` Default compression level (also `zlib.constants.Z_DEFAULT_COMPRESSION`).
93 * - `0` No compression (also `zlib.constants.Z_NO_COMPRESSION`).
94 * - `1` Fastest compression (also `zlib.constants.Z_BEST_SPEED`).
95 * - `2`
96 * - `3`
97 * - `4`
98 * - `5`
99 * - `6` (currently what `zlib.constants.Z_DEFAULT_COMPRESSION` points to).
100 * - `7`
101 * - `8`
102 * - `9` Best compression (also `zlib.constants.Z_BEST_COMPRESSION`).
103 *
104 * **Note** in the list above, `zlib` is from `zlib = require('zlib')`.
105 *
106 * @default zlib.constants.DEFAULT_COMPRESSION or -1
107 * @see {@link https://github.com/expressjs/compression#level|`level` documentation}
108 */
109 level?: number | undefined;
110
111 /**
112 * This specifies how much memory should be allocated for the internal compression state and is an integer in
113 * the range of `1` (minimum level) and `9` (maximum level).
114 *
115 * @default zlib.constants.DEFAULT_MEMLEVEL or 8
116 * @see {@link https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning|Node.js documentation}
117 * @see {@link https://github.com/expressjs/compression#memlevel|`memLevel` documentation}
118 */
119 memLevel?: number | undefined;
120
121 /**
122 * This is used to tune the compression algorithm. This value only affects the compression ratio, not the
123 * correctness of the compressed output, even if it is not set appropriately.
124 *
125 * - `zlib.constants.Z_DEFAULT_STRATEGY` Use for normal data.
126 * - `zlib.constants.Z_FILTERED` Use for data produced by a filter (or predictor). Filtered data consists mostly of small
127 * values with a somewhat random distribution. In this case, the compression algorithm is tuned to compress
128 * them better. The effect is to force more Huffman coding and less string matching; it is somewhat intermediate
129 * between `zlib.constants.Z_DEFAULT_STRATEGY` and `zlib.constants.Z_HUFFMAN_ONLY`.
130 * - `zlib.constants.Z_FIXED` Use to prevent the use of dynamic Huffman codes, allowing for a simpler decoder for special applications.
131 * - `zlib.constants.Z_HUFFMAN_ONLY` Use to force Huffman encoding only (no string match).
132 * - `zlib.constants.Z_RLE` Use to limit match distances to one (run-length encoding). This is designed to be almost as
133 * fast as `zlib.constants.Z_HUFFMAN_ONLY`, but give better compression for PNG image data.
134 *
135 * **Note** in the list above, `zlib` is from `zlib = require('zlib')`.
136 */
137 strategy?: number | undefined;
138
139 /**
140 * The byte threshold for the response body size before compression is considered for the response, defaults to
141 * 1kb. This is a number of bytes or any string accepted by the bytes module.
142 *
143 * **Note** this is only an advisory setting; if the response size cannot be determined at the time the response
144 * headers are written, then it is assumed the response is *over* the threshold. To guarantee the response size
145 * can be determined, be sure set a `Content-Length` response header.
146 *
147 * @see {@link https://www.npmjs.com/package/bytes|`bytes` module}
148 * @see {@link https://github.com/expressjs/compression#threshold|`threshold` documentation}
149 */
150 threshold?: number | string | undefined;
151
152 /**
153 * @default zlib.constants.Z_DEFAULT_WINDOWBITS or 15.
154 * @see {@link https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning|Node.js documentation}
155 */
156 windowBits?: number | undefined;
157 /**
158 * In addition , `zlib` options may be passed in to the options object.
159 */
160 [property: string]: any;
161 }
162}
163
164export = compression;