1 | // Type definitions for formidable 2.0
|
2 | // Project: https://github.com/node-formidable/formidable
|
3 | // Definitions by: Wim Looman <https://github.com/Nemo157>
|
4 | // Martin Badin <https://github.com/martin-badin>
|
5 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
6 |
|
7 | /// <reference types="node" />
|
8 |
|
9 | import { Stream, Writable } from "stream";
|
10 | import Formidable = require("./Formidable");
|
11 | import parsers = require("./parsers/index");
|
12 | import PersistentFile = require("./PersistentFile");
|
13 | import VolatileFile = require("./VolatileFile");
|
14 | import errors = require("./FormidableError");
|
15 |
|
16 | declare namespace formidable {
|
17 | type BufferEncoding =
|
18 | | "ascii"
|
19 | | "base64"
|
20 | | "binary"
|
21 | | "hex"
|
22 | | "latin1"
|
23 | | "ucs-2"
|
24 | | "ucs2"
|
25 | | "utf-8"
|
26 | | "utf16le"
|
27 | | "utf8";
|
28 |
|
29 | type EventNames =
|
30 | /**
|
31 | * Emitted when the request was aborted by the user. Right now this can be due to a 'timeout' or
|
32 | * 'close' event on the socket. After this event is emitted, an error event will follow. In the
|
33 | * future there will be a separate 'timeout' event (needs a change in the node core).
|
34 | *
|
35 | * @link https://github.com/node-formidable/formidable#aborted
|
36 | */
|
37 | | "aborted"
|
38 | /**
|
39 | * Emitted when the entire request has been received, and all contained files have finished
|
40 | * flushing to disk. This is a great place for you to send your response.
|
41 | *
|
42 | * @link https://github.com/node-formidable/formidable#end
|
43 | */
|
44 | | "end"
|
45 | /**
|
46 | * Emitted when there is an error processing the incoming form. A request that experiences an
|
47 | * error is automatically paused, you will have to manually call request.resume() if you want the
|
48 | * request to continue firing 'data' events.
|
49 | *
|
50 | * @link https://github.com/node-formidable/formidable#error
|
51 | */
|
52 | | "error"
|
53 | /**
|
54 | * Emitted whenever a field / value pair has been received.
|
55 | *
|
56 | * @link https://github.com/node-formidable/formidable#field
|
57 | */
|
58 | | "field"
|
59 | /**
|
60 | * Emitted whenever a field / file pair has been received. file is an instance of File.
|
61 | *
|
62 | * @link https://github.com/node-formidable/formidable#file-1
|
63 | */
|
64 | | "file"
|
65 | /**
|
66 | * Emitted whenever a new file is detected in the upload stream. Use this event if you want to
|
67 | * stream the file to somewhere else while buffering the upload on the file system.
|
68 | *
|
69 | * @link https://github.com/node-formidable/formidable#filebegin
|
70 | */
|
71 | | "fileBegin"
|
72 | | "headerEnd"
|
73 | | "headerField"
|
74 | | "headersEnd"
|
75 | | "headerValue"
|
76 | | "partBegin"
|
77 | | "partData"
|
78 | /**
|
79 | * Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own
|
80 | * progress bar.
|
81 | *
|
82 | * @link https://github.com/node-formidable/formidable#progress
|
83 | */
|
84 | | "progress";
|
85 |
|
86 | interface Options {
|
87 | /**
|
88 | * sets encoding for incoming form fields
|
89 | *
|
90 | * @default 'utf-8'
|
91 | */
|
92 | encoding?: BufferEncoding | undefined;
|
93 |
|
94 | /**
|
95 | * the directory for placing file uploads in. You can move them later by using fs.rename()
|
96 | *
|
97 | * @default os.tmpdir()
|
98 | */
|
99 | uploadDir?: string | undefined;
|
100 |
|
101 | /**
|
102 | * to include the extensions of the original files or not
|
103 | *
|
104 | * @default false
|
105 | */
|
106 | keepExtensions?: boolean | undefined;
|
107 |
|
108 | /**
|
109 | * allow upload empty files
|
110 | *
|
111 | * @default true
|
112 | */
|
113 | allowEmptyFiles?: boolean | undefined;
|
114 |
|
115 | /**
|
116 | * the minium size of uploaded file
|
117 | *
|
118 | * @default 1
|
119 | */
|
120 | minFileSize?: number | undefined;
|
121 |
|
122 | /**
|
123 | * limit the amount of uploaded files, set Infinity for unlimited
|
124 | *
|
125 | * @default Infinity
|
126 | */
|
127 | maxFiles?: number | undefined;
|
128 |
|
129 | /**
|
130 | * limit the size of uploaded file
|
131 | *
|
132 | * @default 200 * 1024 * 1024
|
133 | */
|
134 | maxFileSize?: number | undefined;
|
135 |
|
136 | /**
|
137 | * limit the size of the batch of uploaded files
|
138 | *
|
139 | * @default options.maxFileSize
|
140 | */
|
141 | maxTotalFileSize?: number | undefined;
|
142 |
|
143 | /**
|
144 | * limit the number of fields, set 0 for unlimited
|
145 | *
|
146 | * @default 1000
|
147 | */
|
148 | maxFields?: number | undefined;
|
149 |
|
150 | /**
|
151 | * limit the amount of memory all fields together (except files) can allocate in bytes
|
152 | *
|
153 | * @default 20 * 1024 * 1024
|
154 | */
|
155 | maxFieldsSize?: number | undefined;
|
156 |
|
157 | /**
|
158 | * include checksums calculated for incoming files, set this to some hash algorithm, see
|
159 | * crypto.createHash for available algorithms
|
160 | *
|
161 | * @default false
|
162 | */
|
163 | hashAlgorithm?: string | false | undefined;
|
164 |
|
165 | /**
|
166 | * which by default writes to host machine file system every file parsed; The function should
|
167 | * return an instance of a Writable stream that will receive the uploaded file data. With this
|
168 | * option, you can have any custom behavior regarding where the uploaded file data will be
|
169 | * streamed for. If you are looking to write the file uploaded in other types of cloud storages
|
170 | * (AWS S3, Azure blob storage, Google cloud storage) or private file storage, this is the option
|
171 | * you're looking for. When this option is defined the default behavior of writing the file in the
|
172 | * host machine file system is lost.
|
173 | *
|
174 | * @default null
|
175 | */
|
176 | fileWriteStreamHandler?: (() => Writable) | undefined;
|
177 |
|
178 | /**
|
179 | * when you call the .parse method, the files argument (of the callback) will contain arrays of
|
180 | * files for inputs which submit multiple files using the HTML5 multiple attribute. Also, the
|
181 | * fields argument will contain arrays of values for fields that have names ending with '[]'
|
182 | *
|
183 | * @default false
|
184 | */
|
185 | multiples?: boolean | undefined;
|
186 |
|
187 | /**
|
188 | * Use it to control newFilename. Must return a string. Will be joined with
|
189 | * options.uploadDir.
|
190 | *
|
191 | * @default undefined
|
192 | */
|
193 | filename?: (name: string, ext: string, part: Part, form: Formidable) => string;
|
194 |
|
195 | enabledPlugins?: string[] | undefined;
|
196 |
|
197 | filter?: (part: Part) => boolean;
|
198 | }
|
199 |
|
200 | interface Fields {
|
201 | [field: string]: string | string[];
|
202 | }
|
203 | interface Files {
|
204 | [file: string]: File | File[];
|
205 | }
|
206 |
|
207 | interface Part extends Stream {
|
208 | name: string | null;
|
209 | originalFilename: string | null;
|
210 | mimetype: string | null;
|
211 | }
|
212 |
|
213 | /**
|
214 | * @link https://github.com/node-formidable/formidable#file
|
215 | */
|
216 | interface FileJSON extends Pick<File, "size" | "filepath" | "originalFilename" | "mimetype" | "hash"> {
|
217 | length: number;
|
218 | mimetype: string | null;
|
219 | mtime: Date | null;
|
220 | }
|
221 |
|
222 | interface File {
|
223 | /**
|
224 | * The size of the uploaded file in bytes. If the file is still being uploaded (see `'fileBegin'`
|
225 | * event), this property says how many bytes of the file have been written to disk yet.
|
226 | */
|
227 | size: number;
|
228 |
|
229 | /**
|
230 | * The path this file is being written to. You can modify this in the `'fileBegin'` event in case
|
231 | * you are unhappy with the way formidable generates a temporary path for your files.
|
232 | */
|
233 | filepath: string;
|
234 |
|
235 | /**
|
236 | * The name this file had according to the uploading client.
|
237 | */
|
238 | originalFilename: string | null;
|
239 |
|
240 | /**
|
241 | * Calculated based on options provided
|
242 | */
|
243 | newFilename: string;
|
244 |
|
245 | /**
|
246 | * The mime type of this file, according to the uploading client.
|
247 | */
|
248 | mimetype: string | null;
|
249 |
|
250 | /**
|
251 | * A Date object (or `null`) containing the time this file was last written to. Mostly here for
|
252 | * compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).
|
253 | */
|
254 | mtime?: Date | null | undefined;
|
255 |
|
256 | hashAlgorithm: false | "sha1" | "md5" | "sha256";
|
257 |
|
258 | /**
|
259 | * If `options.hashAlgorithm` calculation was set, you can read the hex digest out of this var
|
260 | * (at the end it will be a string).
|
261 | */
|
262 | hash?: string | null;
|
263 |
|
264 | /**
|
265 | * This method returns a JSON-representation of the file, allowing you to JSON.stringify() the
|
266 | * file which is useful for logging and responding to requests.
|
267 | *
|
268 | * @link https://github.com/node-formidable/formidable#filetojson
|
269 | */
|
270 | toJSON(): FileJSON;
|
271 |
|
272 | toString(): string;
|
273 | }
|
274 |
|
275 | interface EmitData {
|
276 | formname: any;
|
277 | key?: string | number | undefined;
|
278 | name: "fileBegin" | "file";
|
279 | value: File | string;
|
280 | }
|
281 |
|
282 | interface EventData {
|
283 | name: EventNames;
|
284 | buffer: string;
|
285 | end: string;
|
286 | formname: string;
|
287 | key: string;
|
288 | start: string;
|
289 | value: string;
|
290 | }
|
291 |
|
292 | type PluginFunction = (formidable: Formidable, options: Partial<Options>) => void;
|
293 |
|
294 | type MappedParsers = {
|
295 | [P in keyof typeof parsers]: typeof parsers[P];
|
296 | };
|
297 |
|
298 | type Plugins = ["octetstream", "querystring", "multipart", "json"];
|
299 |
|
300 | type Plugin = keyof { [K in Plugins[number]]: K };
|
301 |
|
302 | type DefaultOptions = Required<Omit<Options, "enabledPlugins">> & {
|
303 | enabledPlugins: EnabledPlugins;
|
304 | };
|
305 |
|
306 | type EnabledPlugins = {
|
307 | [P in Plugin]: PluginFunction;
|
308 | };
|
309 | }
|
310 |
|
311 | declare const formidable: {
|
312 | (options?: formidable.Options): Formidable;
|
313 | defaultOptions: formidable.DefaultOptions;
|
314 | enabledPlugins: formidable.EnabledPlugins;
|
315 | plugins: formidable.EnabledPlugins;
|
316 | errors: typeof errors;
|
317 | File: typeof PersistentFile;
|
318 | PersistentFile: typeof PersistentFile;
|
319 | VolatileFile: typeof VolatileFile;
|
320 | Formidable: typeof Formidable;
|
321 | formidable: (options?: formidable.Options) => Formidable;
|
322 | // alias
|
323 | IncomingForm: typeof Formidable;
|
324 | // parsers and mapped parsers
|
325 | parsers: typeof parsers;
|
326 | } & formidable.MappedParsers;
|
327 |
|
328 | export = formidable;
|