@@ -24,66 +24,66 @@ const defaultErrorFn: LimitErrorFn = (limit) => `Payload too large. Limit: ${lim
24
24
// Main function
25
25
export const p =
26
26
< T = any > ( fn : ( body : any ) => any , limit = defaultPayloadLimit , errorFn : LimitErrorFn = defaultErrorFn ) =>
27
- async ( req : ReqWithBody < T > , _res : Response , next : ( err ?: any ) => void ) => {
28
- try {
29
- let body = ''
30
-
31
- for await ( const chunk of req ) {
32
- if ( body . length > limit ) throw new Error ( errorFn ( limit ) )
33
- body += chunk
27
+ async ( req : ReqWithBody < T > , _res : Response , next : ( err ?: any ) => void ) => {
28
+ try {
29
+ let body = ''
30
+
31
+ for await ( const chunk of req ) {
32
+ if ( body . length > limit ) throw new Error ( errorFn ( limit ) )
33
+ body += chunk
34
+ }
35
+
36
+ return fn ( body )
37
+ } catch ( e ) {
38
+ next ( e )
34
39
}
35
-
36
- return fn ( body )
37
- } catch ( e ) {
38
- next ( e )
39
40
}
40
- }
41
41
42
42
const custom =
43
43
< T = any > ( fn : ( body : any ) => any ) =>
44
- async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
45
- if ( hasBody ( req . method ! ) ) req . body = await p < T > ( fn ) ( req , _res , next )
46
- next ( )
47
- }
44
+ async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
45
+ if ( hasBody ( req . method ! ) ) req . body = await p < T > ( fn ) ( req , _res , next )
46
+ next ( )
47
+ }
48
48
49
49
const json =
50
50
( { limit, errorFn } : ParserOptions = { } ) =>
51
- async ( req : ReqWithBody , res : Response , next : NextFunction ) => {
52
- if ( hasBody ( req . method ! ) ) {
53
- req . body = await p ( ( x ) => ( x ? JSON . parse ( x . toString ( ) ) : { } ) , limit , errorFn ) ( req , res , next )
54
- } else next ( )
55
- }
51
+ async ( req : ReqWithBody , res : Response , next : NextFunction ) => {
52
+ if ( hasBody ( req . method ! ) ) {
53
+ req . body = await p ( ( x ) => ( x ? JSON . parse ( x . toString ( ) ) : { } ) , limit , errorFn ) ( req , res , next )
54
+ } else next ( )
55
+ }
56
56
57
57
const raw =
58
58
( { limit, errorFn } : ParserOptions = { } ) =>
59
- async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
60
- if ( hasBody ( req . method ! ) ) {
61
- req . body = await p ( ( x ) => x , limit , errorFn ) ( req , _res , next )
62
- } else next ( )
63
- }
59
+ async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
60
+ if ( hasBody ( req . method ! ) ) {
61
+ req . body = await p ( ( x ) => x , limit , errorFn ) ( req , _res , next )
62
+ } else next ( )
63
+ }
64
64
65
65
const text =
66
66
( { limit, errorFn } : ParserOptions = { } ) =>
67
- async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
68
- if ( hasBody ( req . method ! ) ) {
69
- req . body = await p ( ( x ) => x . toString ( ) , limit , errorFn ) ( req , _res , next )
70
- } else next ( )
71
- }
67
+ async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
68
+ if ( hasBody ( req . method ! ) ) {
69
+ req . body = await p ( ( x ) => x . toString ( ) , limit , errorFn ) ( req , _res , next )
70
+ } else next ( )
71
+ }
72
72
73
73
const urlencoded =
74
74
( { limit, errorFn } : ParserOptions = { } ) =>
75
- async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
76
- if ( hasBody ( req . method ! ) ) {
77
- req . body = await p (
78
- ( x ) => {
79
- const urlSearchParam = new URLSearchParams ( x . toString ( ) )
80
- return Object . fromEntries ( urlSearchParam . entries ( ) )
81
- } ,
82
- limit ,
83
- errorFn
84
- ) ( req , _res , next )
85
- } else next ( )
86
- }
75
+ async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
76
+ if ( hasBody ( req . method ! ) ) {
77
+ req . body = await p (
78
+ ( x ) => {
79
+ const urlSearchParam = new URLSearchParams ( x . toString ( ) )
80
+ return Object . fromEntries ( urlSearchParam . entries ( ) )
81
+ } ,
82
+ limit ,
83
+ errorFn
84
+ ) ( req , _res , next )
85
+ } else next ( )
86
+ }
87
87
88
88
const getBoundary = ( contentType : string ) => {
89
89
// Extract the boundary from the Content-Type header
@@ -94,9 +94,10 @@ const getBoundary = (contentType: string) => {
94
94
const parseMultipart = ( body : string , boundary : string ) => {
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
- const parsedBody = { }
97
+ const parsedBody : Record < string , ( File | string ) [ ] > = { }
98
98
// Parse each part into a form data object
99
- parts . map ( ( part ) => {
99
+ // biome-ignore lint/complexity/noForEach: <explanation>
100
+ parts . forEach ( ( part ) => {
100
101
const [ headers , ...lines ] = part . split ( '\r\n' ) . filter ( ( part ) => ! ! part )
101
102
const data = lines . join ( '\r\n' ) . trim ( )
102
103
@@ -107,31 +108,33 @@ const parseMultipart = (body: string, boundary: string) => {
107
108
const contentTypeMatch = / C o n t e n t - T y p e : ( .+ ) / i. exec ( data ) !
108
109
const fileContent = data . slice ( contentTypeMatch [ 0 ] . length + 2 )
109
110
110
- return Object . assign ( parsedBody , {
111
- [ name ] : new File ( [ fileContent ] , filename [ 1 ] , { type : contentTypeMatch [ 1 ] } )
112
- } )
111
+ const file = new File ( [ fileContent ] , filename [ 1 ] , { type : contentTypeMatch [ 1 ] } )
112
+
113
+ parsedBody [ name ] = parsedBody [ name ] ? [ ...parsedBody [ name ] , file ] : [ file ]
114
+ return
113
115
}
114
116
// This is a regular field
115
- return Object . assign ( parsedBody , { [ name ] : data } )
117
+ parsedBody [ name ] = parsedBody [ name ] ? [ ...parsedBody [ name ] , data ] : [ data ]
118
+ return
116
119
} )
117
120
118
121
return parsedBody
119
122
}
120
-
121
123
type MultipartOptions = Partial < {
122
124
fileCountLimit : number
123
125
fileSizeLimit : number
124
126
} >
125
127
126
128
const multipart =
127
129
( opts : MultipartOptions = { } ) =>
128
- async ( req : ReqWithBody , res : Response , next : NextFunction ) => {
129
- if ( hasBody ( req . method ! ) ) {
130
- req . body = await p ( ( x ) => {
131
- const boundary = getBoundary ( req . headers [ 'content-type' ] ! )
132
- if ( boundary ) return parseMultipart ( x , boundary )
133
- } ) ( req , res , next )
134
- } else next ( )
135
- }
130
+ async ( req : ReqWithBody , res : Response , next : NextFunction ) => {
131
+ if ( hasBody ( req . method ! ) ) {
132
+ req . body = await p ( ( x ) => {
133
+ const boundary = getBoundary ( req . headers [ 'content-type' ] ! )
134
+ if ( boundary ) return parseMultipart ( x , boundary )
135
+ } ) ( req , res , next )
136
+ next ( )
137
+ } else next ( )
138
+ }
136
139
137
140
export { custom , json , raw , text , urlencoded , multipart }
0 commit comments