@@ -12,24 +12,24 @@ export const hasBody = (method: string) => ['POST', 'PUT', 'PATCH', 'DELETE'].in
12
12
13
13
const defaultPayloadLimit = 104857600 // 100KB
14
14
15
- export type LimitErrorFn = ( limit : number ) => string
15
+ export type LimitErrorFn = ( payloadLimit : number ) => string
16
16
17
17
export type ParserOptions = Partial < {
18
- limit : number
18
+ payloadLimit : number
19
19
errorFn : LimitErrorFn
20
20
} >
21
21
22
- const defaultErrorFn : LimitErrorFn = ( limit ) => `Payload too large. Limit: ${ limit } bytes`
22
+ const defaultErrorFn : LimitErrorFn = ( payloadLimit ) => `Payload too large. Limit: ${ payloadLimit } bytes`
23
23
24
24
// Main function
25
25
export const p =
26
- < T = any > ( fn : ( body : any ) => any , limit = defaultPayloadLimit , errorFn : LimitErrorFn = defaultErrorFn ) =>
26
+ < T = any > ( fn : ( body : any ) => any , payloadLimit = defaultPayloadLimit , errorFn : LimitErrorFn = defaultErrorFn ) =>
27
27
async ( req : ReqWithBody < T > , _res : Response , next : ( err ?: any ) => void ) => {
28
28
try {
29
29
let body = ''
30
30
31
31
for await ( const chunk of req ) {
32
- if ( body . length > limit ) throw new Error ( errorFn ( limit ) )
32
+ if ( body . length > payloadLimit ) throw new Error ( errorFn ( payloadLimit ) )
33
33
body += chunk
34
34
}
35
35
@@ -47,39 +47,39 @@ const custom =
47
47
}
48
48
49
49
const json =
50
- ( { limit , errorFn } : ParserOptions = { } ) =>
50
+ ( { payloadLimit , errorFn } : ParserOptions = { } ) =>
51
51
async ( req : ReqWithBody , res : Response , next : NextFunction ) => {
52
52
if ( hasBody ( req . method ! ) ) {
53
- req . body = await p ( ( x ) => ( x ? JSON . parse ( x . toString ( ) ) : { } ) , limit , errorFn ) ( req , res , next )
53
+ req . body = await p ( ( x ) => ( x ? JSON . parse ( x . toString ( ) ) : { } ) , payloadLimit , errorFn ) ( req , res , next )
54
54
} else next ( )
55
55
}
56
56
57
57
const raw =
58
- ( { limit , errorFn } : ParserOptions = { } ) =>
58
+ ( { payloadLimit , errorFn } : ParserOptions = { } ) =>
59
59
async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
60
60
if ( hasBody ( req . method ! ) ) {
61
- req . body = await p ( ( x ) => x , limit , errorFn ) ( req , _res , next )
61
+ req . body = await p ( ( x ) => x , payloadLimit , errorFn ) ( req , _res , next )
62
62
} else next ( )
63
63
}
64
64
65
65
const text =
66
- ( { limit , errorFn } : ParserOptions = { } ) =>
66
+ ( { payloadLimit , errorFn } : ParserOptions = { } ) =>
67
67
async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
68
68
if ( hasBody ( req . method ! ) ) {
69
- req . body = await p ( ( x ) => x . toString ( ) , limit , errorFn ) ( req , _res , next )
69
+ req . body = await p ( ( x ) => x . toString ( ) , payloadLimit , errorFn ) ( req , _res , next )
70
70
} else next ( )
71
71
}
72
72
73
73
const urlencoded =
74
- ( { limit , errorFn } : ParserOptions = { } ) =>
74
+ ( { payloadLimit , errorFn } : ParserOptions = { } ) =>
75
75
async ( req : ReqWithBody , _res : Response , next : NextFunction ) => {
76
76
if ( hasBody ( req . method ! ) ) {
77
77
req . body = await p (
78
78
( x ) => {
79
79
const urlSearchParam = new URLSearchParams ( x . toString ( ) )
80
80
return Object . fromEntries ( urlSearchParam . entries ( ) )
81
81
} ,
82
- limit ,
82
+ payloadLimit ,
83
83
errorFn
84
84
) ( req , _res , next )
85
85
} else next ( )
@@ -129,13 +129,13 @@ type MultipartOptions = Partial<{
129
129
} >
130
130
131
131
const multipart =
132
- ( { limit , errorFn, ...opts } : MultipartOptions & ParserOptions = { } ) =>
132
+ ( { payloadLimit , errorFn, ...opts } : MultipartOptions & ParserOptions = { } ) =>
133
133
async ( req : ReqWithBody , res : Response , next : NextFunction ) => {
134
134
if ( hasBody ( req . method ! ) ) {
135
135
req . body = await p ( ( x ) => {
136
136
const boundary = getBoundary ( req . headers [ 'content-type' ] ! )
137
137
if ( boundary ) return parseMultipart ( x , boundary , opts )
138
- } , limit , errorFn ) ( req , res , next )
138
+ } , payloadLimit , errorFn ) ( req , res , next )
139
139
next ( )
140
140
} else next ( )
141
141
}
0 commit comments