@@ -12,7 +12,7 @@ export const hasBody = (method: string) => ['POST', 'PUT', 'PATCH', 'DELETE'].in
12
12
13
13
const defaultPayloadLimit = 104857600 // 100KB
14
14
15
- export type LimitErrorFn = ( payloadLimit : number ) => Error
15
+ export type LimitErrorFn = ( limit : number ) => Error
16
16
17
17
export type ParserOptions = Partial < {
18
18
payloadLimit : number
@@ -91,7 +91,9 @@ const getBoundary = (contentType: string) => {
91
91
return match ? `--${ match [ 1 ] } ` : null
92
92
}
93
93
94
- const parseMultipart = ( body : string , boundary : string , { fileCountLimit } : MultipartOptions ) => {
94
+ const defaultFileSizeLimitErrorFn : LimitErrorFn = ( limit ) => new Error ( `File too large. Limit: ${ limit } bytes` )
95
+
96
+ const parseMultipart = ( body : string , boundary : string , { fileCountLimit, fileSizeLimit, fileSizeLimitErrorFn = defaultFileSizeLimitErrorFn } : MultipartOptions ) => {
95
97
// Split the body into an array of parts
96
98
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
99
const parsedBody : Record < string , ( File | string ) [ ] > = { }
@@ -104,6 +106,8 @@ const parseMultipart = (body: string, boundary: string, { fileCountLimit }: Mult
104
106
const [ headers , ...lines ] = part . split ( '\r\n' ) . filter ( ( part ) => ! ! part )
105
107
const data = lines . join ( '\r\n' ) . trim ( )
106
108
109
+ if ( fileSizeLimit && data . length > fileSizeLimit ) throw fileSizeLimitErrorFn ( fileSizeLimit )
110
+
107
111
// Extract the name and filename from the headers
108
112
const name = / n a m e = " ( .+ ?) " / . exec ( headers ) ! [ 1 ]
109
113
const filename = / f i l e n a m e = " ( .+ ?) " / . exec ( headers )
@@ -126,6 +130,7 @@ const parseMultipart = (body: string, boundary: string, { fileCountLimit }: Mult
126
130
type MultipartOptions = Partial < {
127
131
fileCountLimit : number
128
132
fileSizeLimit : number
133
+ fileSizeLimitErrorFn : LimitErrorFn
129
134
} >
130
135
131
136
const multipart =
0 commit comments