forked from koajs/compress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.d.ts
More file actions
137 lines (129 loc) · 4.57 KB
/
index.d.ts
File metadata and controls
137 lines (129 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import type { BrotliOptions, ZlibOptions, ZstdOptions } from "node:zlib";
import type Koa = require("koa");
/**
* Function to calculate a threshold value dynamically from a MIME type,
* an existing size and the current context.
*/
export type ThresholdFunction = (
/** MIME type of the response */
type: string,
/** Size of the response in bytes */
size: number,
/** Context of the request */
ctx: Koa.Context,
) => number | string | ThresholdFunction;
/**
* Function to calculate compression parameters for `deflate` and `gzip` from a MIME type,
* an exisiting size and the current context.
*/
export type ZlibOptionsFunction = (
/** MIME type of the response */
type: string,
/** Size of the response in bytes */
size: number,
/** Context of the request */
ctx: Koa.Context,
) => boolean | null | ZlibOptions | ZlibOptionsFunction;
/**
* Function to calculate compression parameters for `brotli` from a MIME type,
* an exisiting size and the current context.
*/
export type BrotliOptionsFunction = (
/** MIME type of the response */
type: string,
/** Size of the response in bytes */
size: number,
/** Context of the request */
ctx: Koa.Context,
) => boolean | null | BrotliOptions | BrotliOptionsFunction;
/**
* Function to calculate compression parameters for `zstd` from a MIME type,
* an exisiting size and the current context.
*/
export type ZstdOptionsFunction = (
/** MIME type of the response */
type: string,
/** Size of the response in bytes */
size: number,
/** Context of the request */
ctx: Koa.Context,
) => boolean | null | ZstdOptions | ZstdOptionsFunction;
/**
* Compression options that govern how `koa/compress` handles responses.
*/
export type CompressOptions = {
/**
* Function to determine if compression should be applied.
* Default: `compressible()`.
* @param type MIME type of the response
* @returns `true` if compression should be applied, `false` otherwise
*/
filter?: (type: string) => boolean;
/**
* Lower limit to apply compression to content. If it is a number, it is size in bytes,
* if it is a string, it is a human-readable size accepted by `bytes()`, e.g., `"1mb"`,
* or a `ThresholdFunction` that can calculate that value. Default: `1024`.
*/
threshold?: number | string | ThresholdFunction;
/**
* Default value for `Accept-Encoding` header, if it is not supplied by the client.
* Default: `"identity"`.
*/
defaultEncoding?: string;
/**
* What `Accept-Encoding` value should be assumed if it is set to `"*"`. Default: `"gzip"`.
*/
wildcardAcceptEncoding?: string;
/**
* An array of compression types, which should be used when we have multiple choices
* with the same weight. An item with a lower index has higher priority.
* Default: `['zstd', 'br', 'gzip', 'deflate']`.
*/
encodingPreference?: string[];
/**
* Options to use when compressing with `deflate()`.
* If it is `false` or `null` this compression should be disabled.
* It can be a function used to calculate such values.
* Default: `{}`.
*/
deflate?: boolean | null | ZlibOptions | ZlibOptionsFunction;
/**
* Options to use when compressing with `gzip()`.
* If it is `false` or `null` this compression should be disabled.
* It can be a function used to calculate such values.
* Default: `{}`.
*/
gzip?: boolean | null | ZlibOptions | ZlibOptionsFunction;
/**
* Options to use when compressing with `br()`.
* If it is `false` or `null` this compression should be disabled.
* It can be a function used to calculate such values.
* Default: `{[zlib.constants.BROTLI_PARAM_QUALITY]: 4}`.
*/
br?: boolean | null | BrotliOptions | BrotliOptionsFunction;
/**
* Options to use when compressing with `zstd()`.
* If it is `false` or `null` this compression should be disabled.
* It can be a function used to calculate such values.
* Default: `{}`.
*/
zstd?: boolean | null | ZstdOptions | ZstdOptionsFunction;
};
/**
* The main function that constructs a middleware for compressing responses.
* @param options sets the default way to handle compression.
* @returns a middleware function that compresses responses.
*/
declare function compress(options?: CompressOptions): Koa.Middleware;
declare module "koa" {
interface DefaultContext {
/**
* Context property used to handle individual responses.
* If it is set to `false`, the compression is disabled.
* If it is an object, it is mixed with the default options overriding
* its properties for this request.
*/
compress?: boolean | CompressOptions;
}
}
export default compress;