UNPKG

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