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