@@ -5,32 +5,28 @@ import { read_only_form_data } from './read_only_form_data.js';
55 * @param {import('types/helper').Headers } headers
66 */
77export function parse_body ( raw , headers ) {
8- if ( ! raw ) return raw ;
8+ if ( ! raw || typeof raw !== 'string' ) return raw ;
99
10- if ( typeof raw === 'string' ) {
11- const [ type , ...directives ] = headers [ 'content-type' ] . split ( / ; \s * / ) ;
10+ const [ type , ...directives ] = headers [ 'content-type' ] . split ( / ; \s * / ) ;
1211
13- switch ( type ) {
14- case 'text/plain' :
15- return raw ;
12+ switch ( type ) {
13+ case 'text/plain' :
14+ return raw ;
1615
17- case 'application/json' :
18- return JSON . parse ( raw ) ;
16+ case 'application/json' :
17+ return JSON . parse ( raw ) ;
1918
20- case 'application/x-www-form-urlencoded' :
21- return get_urlencoded ( raw ) ;
19+ case 'application/x-www-form-urlencoded' :
20+ return get_urlencoded ( raw ) ;
2221
23- case 'multipart/form-data' : {
24- const boundary = directives . find ( ( directive ) => directive . startsWith ( 'boundary=' ) ) ;
25- if ( ! boundary ) throw new Error ( 'Missing boundary' ) ;
26- return get_multipart ( raw , boundary . slice ( 'boundary=' . length ) ) ;
27- }
28- default :
29- throw new Error ( `Invalid Content-Type ${ type } ` ) ;
22+ case 'multipart/form-data' : {
23+ const boundary = directives . find ( ( directive ) => directive . startsWith ( 'boundary=' ) ) ;
24+ if ( ! boundary ) throw new Error ( 'Missing boundary' ) ;
25+ return get_multipart ( raw , boundary . slice ( 'boundary=' . length ) ) ;
3026 }
27+ default :
28+ throw new Error ( `Invalid Content-Type ${ type } ` ) ;
3129 }
32-
33- return raw ;
3430}
3531
3632/** @param {string } text */
0 commit comments