@@ -7,7 +7,8 @@ import {FormDataLike} from "./FormDataLike"
7
7
8
8
const DASHES = "-" . repeat ( 2 )
9
9
const CRLF = "\r\n"
10
- const CRLF_BYTES_LENGTH = Buffer . byteLength ( CRLF )
10
+ const CRLF_BYTES = new TextEncoder ( ) . encode ( CRLF )
11
+ const CRLF_BYTES_LENGTH = CRLF_BYTES . byteLength
11
12
12
13
export class Encoder {
13
14
/**
@@ -23,7 +24,7 @@ export class Encoder {
23
24
/**
24
25
* Returns field's footer
25
26
*/
26
- readonly #footer: string
27
+ readonly #footer: Uint8Array
27
28
28
29
/**
29
30
* FormData instance
@@ -43,7 +44,8 @@ export class Encoder {
43
44
this . contentType = `multipart/form-data; boundary=${ this . boundary } `
44
45
45
46
this . #form = form
46
- this . #footer = `${ DASHES } ${ this . boundary } ${ DASHES } ${ CRLF . repeat ( 2 ) } `
47
+ this . #footer = new TextEncoder ( )
48
+ . encode ( `${ DASHES } ${ this . boundary } ${ DASHES } ${ CRLF . repeat ( 2 ) } ` )
47
49
}
48
50
49
51
/**
@@ -56,7 +58,7 @@ export class Encoder {
56
58
}
57
59
}
58
60
59
- private _getFieldHeader ( name : string , value : unknown ) {
61
+ private _getFieldHeader ( name : string , value : unknown ) : Uint8Array {
60
62
let header = ""
61
63
62
64
header += `${ DASHES } ${ this . boundary } ${ CRLF } `
@@ -67,7 +69,7 @@ export class Encoder {
67
69
header += `Content-Type: ${ value . type || getMime ( value . name ) } `
68
70
}
69
71
70
- return `${ header } ${ CRLF . repeat ( 2 ) } `
72
+ return new TextEncoder ( ) . encode ( `${ header } ${ CRLF . repeat ( 2 ) } ` )
71
73
}
72
74
73
75
/**
@@ -77,39 +79,34 @@ export class Encoder {
77
79
let length = 0
78
80
79
81
for ( const [ name , value ] of this . #form) {
80
- length += Buffer . byteLength ( this . _getFieldHeader ( name , value ) )
81
- length += isFile ( value ) ? value . size : Buffer . byteLength ( String ( value ) )
82
+ length += this . _getFieldHeader ( name , value ) . byteLength
83
+
84
+ length += isFile ( value )
85
+ ? value . size
86
+ : new TextEncoder ( ) . encode ( String ( value ) ) . byteLength
87
+
82
88
length += CRLF_BYTES_LENGTH
83
89
}
84
90
85
- return length + Buffer . byteLength ( this . #footer)
91
+ return length + this . #footer. byteLength
86
92
}
87
93
88
- private async * _getField ( ) : AsyncGenerator < Buffer | string , void , undefined > {
94
+ async * encode ( ) : AsyncGenerator < Uint8Array , void , undefined > {
89
95
for ( const [ name , value ] of this . #form) {
90
96
yield this . _getFieldHeader ( name , value )
91
97
92
98
if ( isFile ( value ) ) {
93
99
yield * value . stream ( )
94
100
} else {
95
- yield value
101
+ yield new TextEncoder ( ) . encode ( String ( value ) )
96
102
}
97
103
98
- yield CRLF
104
+ yield CRLF_BYTES
99
105
}
100
106
101
107
yield this . #footer
102
108
}
103
109
104
- /**
105
- * Returns async generator allowing to encode FormData content into the spec format
106
- */
107
- async * encode ( ) : AsyncGenerator < Buffer , void , undefined > {
108
- for await ( const chunk of this . _getField ( ) ) {
109
- yield Buffer . isBuffer ( chunk ) ? chunk : Buffer . from ( String ( chunk ) )
110
- }
111
- }
112
-
113
110
[ Symbol . asyncIterator ] ( ) {
114
111
return this . encode ( )
115
112
}
0 commit comments