UNPKG

6.95 kBTypeScriptView Raw
1// Type definitions for busboy 1.5
2// Project: https://github.com/mscdex/busboy
3// Definitions by: Jacob Baskin <https://github.com/jacobbaskin>
4// BendingBender <https://github.com/BendingBender>
5// Martin Badin <https://github.com/martin-badin>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8/// <reference types="node" />
9
10import { IncomingHttpHeaders } from 'http';
11import { Readable, Writable } from 'stream';
12
13declare namespace busboy {
14 interface Limits {
15 /**
16 * Max field name size (in bytes).
17 *
18 * @default 100
19 */
20 fieldNameSize?: number | undefined;
21
22 /**
23 * Max field value size (in bytes).
24 *
25 * @default 1048576 (1MB)
26 */
27 fieldSize?: number | undefined;
28
29 /**
30 * Max number of non-file fields.
31 *
32 * @default Infinity
33 */
34 fields?: number | undefined;
35
36 /**
37 * For multipart forms, the max file size (in bytes).
38 *
39 * @default Infinity
40 */
41 fileSize?: number | undefined;
42
43 /**
44 * For multipart forms, the max number of file fields.
45 *
46 * @default Infinity
47 */
48 files?: number | undefined;
49
50 /**
51 * For multipart forms, the max number of parts (fields + files).
52 *
53 * @default Infinity
54 */
55 parts?: number | undefined;
56
57 /**
58 * For multipart forms, the max number of header key-value pairs to parse.
59 *
60 * @default 2000 (same as node's http module)
61 */
62 headerPairs?: number | undefined;
63 }
64
65 interface BusboyConfig {
66 /**
67 * These are the HTTP headers of the incoming request, which are used by individual parsers.
68 */
69 headers?: IncomingHttpHeaders | undefined;
70
71 /**
72 * 'highWaterMark' to use for the parser stream
73 *
74 * @default stream.Writable
75 */
76 highWaterMark?: number | undefined;
77
78 /**
79 * 'highWaterMark' to use for individual file streams
80 *
81 * @default stream.Readable
82 */
83 fileHwm?: number | undefined;
84
85 /**
86 * Default character set to use when one isn't defined.
87 *
88 * @default 'utf8'
89 */
90 defCharset?: string | undefined;
91
92 /**
93 * For multipart forms, the default character set to use for values of part header parameters (e.g. filename)
94 * that are not extended parameters (that contain an explicit charset
95 *
96 * @default 'latin1'
97 */
98 defParamCharset?: string | undefined;
99
100 /**
101 * If paths in filenames from file parts in a 'multipart/form-data' request shall be preserved.
102 *
103 * @default false
104 */
105 preservePath?: boolean | undefined;
106
107 /**
108 * Various limits on incoming data.
109 */
110 limits?: Limits | undefined;
111 }
112
113 interface Busboy extends Writable {
114 addListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
115 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
116
117 on<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
118 on(event: string | symbol, listener: (...args: any[]) => void): this;
119
120 once<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
121 once(event: string | symbol, listener: (...args: any[]) => void): this;
122
123 removeListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
124 removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
125
126 off<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
127 off(event: string | symbol, listener: (...args: any[]) => void): this;
128
129 prependListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
130 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
131
132 prependOnceListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
133 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
134 }
135
136 interface Info {
137 encoding: string;
138 mimeType: string;
139 }
140
141 interface FileInfo extends Info {
142 filename: string;
143 }
144
145 interface FieldInfo extends Info {
146 nameTruncated: boolean;
147 valueTruncated: boolean;
148 }
149
150 interface BusboyEvents {
151 /**
152 * Emitted for each new file form field found.
153 *
154 * * Note: if you listen for this event, you should always handle the `stream` no matter if you care about the
155 * file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents),
156 * otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any**
157 * incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically
158 * and safely discarded (these discarded files do still count towards `files` and `parts` limits).
159 * * If a configured file size limit was reached, `stream` will both have a boolean property `truncated`
160 * (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
161 *
162 * @param listener.transferEncoding Contains the 'Content-Transfer-Encoding' value for the file stream.
163 * @param listener.mimeType Contains the 'Content-Type' value for the file stream.
164 */
165 file: (name: string, stream: Readable, info: FileInfo) => void;
166
167 /**
168 * Emitted for each new non-file field found.
169 */
170 field: (name: string, value: string, info: FieldInfo) => void;
171
172 /**
173 * Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted.
174 */
175 partsLimit: () => void;
176
177 /**
178 * Emitted when specified `files` limit has been reached. No more 'file' events will be emitted.
179 */
180 filesLimit: () => void;
181
182 /**
183 * Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted.
184 */
185 fieldsLimit: () => void;
186
187 error: (error: unknown) => void;
188
189 /**
190 * @deprecated
191 * @since 1.0
192 */
193 finish: () => void;
194
195 /**
196 * Use 'close' event instead of 'finish' event when you need to execute
197 *
198 * @since 1.0
199 */
200 close: () => void;
201 }
202}
203
204declare function busboy(config: busboy.BusboyConfig): busboy.Busboy;
205
206export = busboy;