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