@@ -37,63 +37,65 @@ export const p =
37
37
payloadLimit = defaultPayloadLimit ,
38
38
payloadLimitErrorFn : LimitErrorFn = defaultErrorFn
39
39
) =>
40
- async ( req : ReqWithBody < T > , _res : Response , next ?: ( err ?: any ) => void ) => {
41
- try {
42
- const body : Buffer [ ] = [ ]
43
-
44
- for await ( const chunk of req ) {
45
- const totalSize = body . reduce ( ( total , buffer ) => total + buffer . byteLength , 0 )
46
- if ( totalSize > payloadLimit ) throw payloadLimitErrorFn ( payloadLimit )
47
- body . push ( chunk as Buffer )
48
- }
49
-
50
- return fn ( Buffer . concat ( body ) )
51
- } catch ( e ) {
52
- next ?.( e )
40
+ async ( req : ReqWithBody < T > , _res : Response , next ?: ( err ?: any ) => void ) => {
41
+ try {
42
+ const body : Buffer [ ] = [ ]
43
+
44
+ for await ( const chunk of req ) {
45
+ const totalSize = body . reduce ( ( total , buffer ) => total + buffer . byteLength , 0 )
46
+ if ( totalSize > payloadLimit ) throw payloadLimitErrorFn ( payloadLimit )
47
+ body . push ( chunk as Buffer )
53
48
}
49
+
50
+ return fn ( Buffer . concat ( body ) )
51
+ } catch ( e ) {
52
+ next ?.( e )
54
53
}
54
+ }
55
55
56
56
/**
57
57
* Parse payload with a custom function
58
58
* @param fn
59
59
*/
60
60
const custom =
61
61
< T = any > ( fn : ( body : Buffer ) => any ) =>
62
- async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
63
- if ( hasBody ( req . method ! ) ) req . body = await p < T > ( fn ) ( req , _res , next )
64
- next ?.( )
65
- }
62
+ async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
63
+ if ( hasBody ( req . method ! ) ) req . body = await p < T > ( fn ) ( req , _res , next )
64
+ next ?.( )
65
+ }
66
66
67
67
/**
68
68
* Parse JSON payload
69
69
* @param options
70
70
*/
71
71
const json =
72
72
( { payloadLimit, payloadLimitErrorFn } : ParserOptions = { } ) =>
73
- async ( req : ReqWithBody , res : Response , next ?: NextFunction ) => {
74
- if ( hasBody ( req . method ! ) ) {
75
- req . body = await p (
76
- ( x ) => {
77
- const str = td . decode ( x )
78
- return str ? JSON . parse ( str ) : { }
79
- } ,
80
- payloadLimit ,
81
- payloadLimitErrorFn
82
- ) ( req , res , next )
83
- } next ?.( )
73
+ async ( req : ReqWithBody , res : Response , next ?: NextFunction ) => {
74
+ if ( hasBody ( req . method ! ) ) {
75
+ req . body = await p (
76
+ ( x ) => {
77
+ const str = td . decode ( x )
78
+ return str ? JSON . parse ( str ) : { }
79
+ } ,
80
+ payloadLimit ,
81
+ payloadLimitErrorFn
82
+ ) ( req , res , next )
84
83
}
84
+ next ?.( )
85
+ }
85
86
86
87
/**
87
88
* Parse raw payload
88
89
* @param options
89
90
*/
90
91
const raw =
91
92
( { payloadLimit, payloadLimitErrorFn } : ParserOptions = { } ) =>
92
- async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
93
- if ( hasBody ( req . method ! ) ) {
94
- req . body = await p ( ( x ) => x , payloadLimit , payloadLimitErrorFn ) ( req , _res , next )
95
- } next ?.( )
93
+ async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
94
+ if ( hasBody ( req . method ! ) ) {
95
+ req . body = await p ( ( x ) => x , payloadLimit , payloadLimitErrorFn ) ( req , _res , next )
96
96
}
97
+ next ?.( )
98
+ }
97
99
98
100
const td = new TextDecoder ( )
99
101
/**
@@ -103,27 +105,29 @@ const td = new TextDecoder()
103
105
*/
104
106
const text =
105
107
( { payloadLimit, payloadLimitErrorFn } : ParserOptions = { } ) =>
106
- async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
107
- if ( hasBody ( req . method ! ) ) {
108
- req . body = await p ( ( x ) => td . decode ( x ) , payloadLimit , payloadLimitErrorFn ) ( req , _res , next )
109
- } next ?.( )
108
+ async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
109
+ if ( hasBody ( req . method ! ) ) {
110
+ req . body = await p ( ( x ) => td . decode ( x ) , payloadLimit , payloadLimitErrorFn ) ( req , _res , next )
110
111
}
112
+ next ?.( )
113
+ }
111
114
112
115
/**
113
116
* Parse urlencoded payload
114
117
* @param options
115
118
*/
116
119
const urlencoded =
117
120
( { payloadLimit, payloadLimitErrorFn } : ParserOptions = { } ) =>
118
- async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
119
- if ( hasBody ( req . method ! ) ) {
120
- req . body = await p (
121
- ( x ) => Object . fromEntries ( new URLSearchParams ( x . toString ( ) ) . entries ( ) ) ,
122
- payloadLimit ,
123
- payloadLimitErrorFn
124
- ) ( req , _res , next )
125
- } next ?.( )
121
+ async ( req : ReqWithBody , _res : Response , next ?: NextFunction ) => {
122
+ if ( hasBody ( req . method ! ) ) {
123
+ req . body = await p (
124
+ ( x ) => Object . fromEntries ( new URLSearchParams ( x . toString ( ) ) . entries ( ) ) ,
125
+ payloadLimit ,
126
+ payloadLimitErrorFn
127
+ ) ( req , _res , next )
126
128
}
129
+ next ?.( )
130
+ }
127
131
128
132
const getBoundary = ( contentType : string ) => {
129
133
const match = / b o u n d a r y = ( .+ ) ; ? / . exec ( contentType )
@@ -195,18 +199,19 @@ type MultipartOptions = Partial<{
195
199
*/
196
200
const multipart =
197
201
( { payloadLimit = Number . POSITIVE_INFINITY , payloadLimitErrorFn, ...opts } : MultipartOptions & ParserOptions = { } ) =>
198
- async ( req : ReqWithBody , res : Response , next ?: NextFunction ) => {
199
- if ( hasBody ( req . method ! ) ) {
200
- req . body = await p (
201
- ( x ) => {
202
- const boundary = getBoundary ( req . headers [ 'content-type' ] ! )
203
- if ( boundary ) return parseMultipart ( td . decode ( x ) , boundary , opts )
204
- return { }
205
- } ,
206
- payloadLimit ,
207
- payloadLimitErrorFn
208
- ) ( req , res , next )
209
- } next ?.( )
202
+ async ( req : ReqWithBody , res : Response , next ?: NextFunction ) => {
203
+ if ( hasBody ( req . method ! ) ) {
204
+ req . body = await p (
205
+ ( x ) => {
206
+ const boundary = getBoundary ( req . headers [ 'content-type' ] ! )
207
+ if ( boundary ) return parseMultipart ( td . decode ( x ) , boundary , opts )
208
+ return { }
209
+ } ,
210
+ payloadLimit ,
211
+ payloadLimitErrorFn
212
+ ) ( req , res , next )
210
213
}
214
+ next ?.( )
215
+ }
211
216
212
217
export { custom , json , raw , text , urlencoded , multipart }
0 commit comments