@@ -91,10 +91,13 @@ const getBoundary = (contentType: string) => {
91
91
return match ? `--${ match [ 1 ] } ` : null
92
92
}
93
93
94
- const parseMultipart = ( body : string , boundary : string ) => {
94
+ const parseMultipart = ( body : string , boundary : string , { fileCountLimit } : MultipartOptions ) => {
95
95
// Split the body into an array of parts
96
96
const parts = body . split ( new RegExp ( `${ boundary } (--)?` ) ) . filter ( ( part ) => ! ! part && / c o n t e n t - d i s p o s i t i o n / i. test ( part ) )
97
97
const parsedBody : Record < string , ( File | string ) [ ] > = { }
98
+
99
+ if ( fileCountLimit && parts . length > fileCountLimit ) throw new Error ( `Too many files. Limit: ${ fileCountLimit } ` )
100
+
98
101
// Parse each part into a form data object
99
102
// biome-ignore lint/complexity/noForEach: <explanation>
100
103
parts . forEach ( ( part ) => {
@@ -126,13 +129,13 @@ type MultipartOptions = Partial<{
126
129
} >
127
130
128
131
const multipart =
129
- ( opts : MultipartOptions = { } ) =>
132
+ ( { limit , errorFn , ... opts } : MultipartOptions & ParserOptions = { } ) =>
130
133
async ( req : ReqWithBody , res : Response , next : NextFunction ) => {
131
134
if ( hasBody ( req . method ! ) ) {
132
135
req . body = await p ( ( x ) => {
133
136
const boundary = getBoundary ( req . headers [ 'content-type' ] ! )
134
- if ( boundary ) return parseMultipart ( x , boundary )
135
- } ) ( req , res , next )
137
+ if ( boundary ) return parseMultipart ( x , boundary , opts )
138
+ } , limit , errorFn ) ( req , res , next )
136
139
next ( )
137
140
} else next ( )
138
141
}
0 commit comments