@@ -5,12 +5,6 @@ import isFile from "./util/isFile"
5
5
6
6
import { FormDataLike } from "./FormDataLike"
7
7
8
- const DASHES = "-" . repeat ( 2 )
9
-
10
- const CRLF = "\r\n"
11
- const CRLF_BYTES = new TextEncoder ( ) . encode ( CRLF )
12
- const CRLF_BYTES_LENGTH = CRLF_BYTES . byteLength
13
-
14
8
export class Encoder {
15
9
/**
16
10
* Returns boundary string
@@ -30,6 +24,16 @@ export class Encoder {
30
24
"Content-Length" : number
31
25
}
32
26
27
+ readonly #CRLF: string
28
+
29
+ readonly #CRLF_BYTES: Uint8Array
30
+
31
+ readonly #CRLF_BYTES_LENGTH: number
32
+
33
+ readonly #DASHES = "-" . repeat ( 2 )
34
+
35
+ readonly #encoder: TextEncoder
36
+
33
37
/**
34
38
* Returns field's footer
35
39
*/
@@ -69,9 +73,15 @@ export class Encoder {
69
73
this . boundary = boundary
70
74
this . contentType = `multipart/form-data; boundary=${ this . boundary } `
71
75
76
+ this . #encoder = new TextEncoder ( )
77
+
78
+ this . #CRLF = "\r\n"
79
+ this . #CRLF_BYTES = this . #encoder. encode ( this . #CRLF)
80
+ this . #CRLF_BYTES_LENGTH = this . #CRLF_BYTES. byteLength
81
+
72
82
this . #form = form
73
- this . #footer = new TextEncoder ( ) . encode (
74
- `${ DASHES } ${ this . boundary } ${ DASHES } ${ CRLF . repeat ( 2 ) } `
83
+ this . #footer = this . #encoder . encode (
84
+ `${ this . # DASHES} ${ this . boundary } ${ this . # DASHES} ${ this . # CRLF. repeat ( 2 ) } `
75
85
)
76
86
77
87
this . headers = Object . freeze ( {
@@ -83,15 +93,15 @@ export class Encoder {
83
93
private _getFieldHeader ( name : string , value : unknown ) : Uint8Array {
84
94
let header = ""
85
95
86
- header += `${ DASHES } ${ this . boundary } ${ CRLF } `
96
+ header += `${ this . # DASHES} ${ this . boundary } ${ this . # CRLF} `
87
97
header += `Content-Disposition: form-data; name="${ name } "`
88
98
89
99
if ( isFile ( value ) ) {
90
- header += `; filename="${ value . name } "${ CRLF } `
100
+ header += `; filename="${ value . name } "${ this . # CRLF} `
91
101
header += `Content-Type: ${ value . type || getMime ( value . name ) } `
92
102
}
93
103
94
- return new TextEncoder ( ) . encode ( `${ header } ${ CRLF . repeat ( 2 ) } ` )
104
+ return this . #encoder . encode ( `${ header } ${ this . # CRLF. repeat ( 2 ) } ` )
95
105
}
96
106
97
107
/**
@@ -105,9 +115,9 @@ export class Encoder {
105
115
106
116
length += isFile ( value )
107
117
? value . size
108
- : new TextEncoder ( ) . encode ( String ( value ) ) . byteLength
118
+ : this . #encoder . encode ( String ( value ) ) . byteLength
109
119
110
- length += CRLF_BYTES_LENGTH
120
+ length += this . # CRLF_BYTES_LENGTH
111
121
}
112
122
113
123
return length + this . #footer. byteLength
@@ -150,10 +160,10 @@ export class Encoder {
150
160
if ( isFile ( value ) ) {
151
161
yield * value . stream ( )
152
162
} else {
153
- yield new TextEncoder ( ) . encode ( String ( value ) )
163
+ yield this . #encoder . encode ( String ( value ) )
154
164
}
155
165
156
- yield CRLF_BYTES
166
+ yield this . # CRLF_BYTES
157
167
}
158
168
159
169
yield this . #footer
0 commit comments