33 * @typedef {import('./types.d.ts').FormData } FormData
44 */
55
6+ /**
7+ * @param {string } file
8+ * @returns {string }
9+ */
10+ const getFileName = ( file ) => {
11+ const fileName = file . split ( `filename="` )
12+ if ( fileName . length > 1 ) {
13+ return fileName [ 1 ] . split ( `"\r\n` ) [ 0 ]
14+ }
15+ throw new Error ( 'No file name' )
16+ }
17+
18+ /**
19+ * @param {string } file
20+ * @returns {string }
21+ */
22+ const getFileData = ( file ) => {
23+ const fileData = file . split ( `\r\n\r\n` )
24+ if ( fileData . length > 1 ) {
25+ return fileData [ 1 ] . split ( `\r\n` ) [ 0 ]
26+ }
27+ throw new Error ( 'No file data' )
28+ }
29+
30+ /**
31+ * @param {string } file
32+ * @returns {string }
33+ */
34+ const getField = ( file ) => {
35+ const fieldName = file . split ( `name="` )
36+ if ( fieldName . length > 1 ) {
37+ return fieldName [ 1 ] . split ( `";` ) [ 0 ]
38+ }
39+ throw new Error ( 'No field' )
40+ }
41+ /**
42+ * @param {string } file
43+ * @returns {string }
44+ */
45+ const getContentType = ( file ) => {
46+ const contentType = file . split ( `Content-Type: ` )
47+ if ( contentType . length > 1 ) {
48+ return contentType [ 1 ] . split ( `\r\n` ) [ 0 ]
49+ }
50+ throw new Error ( 'No content type' )
51+ }
52+
653/**
754 * Parse multipart/form-data
855 * @param {string } data
@@ -21,10 +68,10 @@ export default (data, header) => {
2168 return undefined
2269 }
2370 return {
24- fileName : file . split ( `filename="` ) [ 1 ] . split ( `"\r\n` ) [ 0 ] ,
25- fileData : file . split ( `\r\n\r\n` ) [ 1 ] . split ( `\r\n` ) [ 0 ] ,
26- field : file . split ( `name="` ) [ 1 ] . split ( `";` ) [ 0 ] ,
27- contentType : file . split ( `Content-Type: ` ) [ 1 ] . split ( `\r\n` ) [ 0 ] ,
71+ fileName : getFileName ( file ) ,
72+ fileData : getFileData ( file ) ,
73+ field : getField ( file ) ,
74+ contentType : getContentType ( file ) ,
2875 boundary
2976 }
3077 } ) . filter ( file => file !== undefined )
0 commit comments