@@ -23,7 +23,11 @@ export async function action({ request, params }: ActionFunctionArgs) {
23
23
}
24
24
25
25
const contentLength = request . headers . get ( "content-length" ) ;
26
- if ( contentLength && parseInt ( contentLength ) > env . TASK_PAYLOAD_MAXIMUM_SIZE ) {
26
+ if ( ! contentLength ) {
27
+ return json ( { error : "Content-Length header is required" } , { status : 411 } ) ;
28
+ }
29
+
30
+ if ( parseInt ( contentLength ) > env . TASK_PAYLOAD_MAXIMUM_SIZE ) {
27
31
return json ( { error : "Request body too large" } , { status : 413 } ) ;
28
32
}
29
33
@@ -58,16 +62,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
58
62
} ) ;
59
63
}
60
64
61
- let body ;
62
- try {
63
- body = await readJsonWithLimit ( request , env . TASK_PAYLOAD_MAXIMUM_SIZE ) ;
64
- } catch ( e ) {
65
- return json ( { error : "Request body too large" } , { status : 413 } ) ;
66
- }
67
-
68
- if ( ! body ) {
69
- body = { } ;
70
- }
65
+ // If the request body is not valid JSON, return an empty object
66
+ const body = await request . json ( ) . catch ( ( ) => ( { } ) ) ;
71
67
72
68
const stringifiedData = await stringifyIO ( body ) ;
73
69
const finalData = await conditionallyExportPacket (
@@ -93,27 +89,3 @@ export async function action({ request, params }: ActionFunctionArgs) {
93
89
throw json ( { error : "Failed to complete HTTP callback" } , { status : 500 } ) ;
94
90
}
95
91
}
96
-
97
- async function readJsonWithLimit ( request : Request , maxSize : number ) {
98
- const reader = request . body ?. getReader ( ) ;
99
- if ( ! reader ) throw new Error ( "No body" ) ;
100
- let received = 0 ;
101
- let chunks : Uint8Array [ ] = [ ] ;
102
- while ( true ) {
103
- const { done, value } = await reader . read ( ) ;
104
- if ( done ) break ;
105
- received += value . length ;
106
- if ( received > maxSize ) {
107
- throw new Error ( "Request body too large" ) ;
108
- }
109
- chunks . push ( value ) ;
110
- }
111
- const full = new Uint8Array ( received ) ;
112
- let offset = 0 ;
113
- for ( const chunk of chunks ) {
114
- full . set ( chunk , offset ) ;
115
- offset += chunk . length ;
116
- }
117
- const text = new TextDecoder ( ) . decode ( full ) ;
118
- return JSON . parse ( text ) ;
119
- }
0 commit comments