@@ -8,6 +8,8 @@ import {isFormData} from "./util/isFormData.js"
8
8
import { FormDataLike } from "./FormDataLike.js"
9
9
import { FileLike } from "./FileLike.js"
10
10
11
+ type FormDataEntryValue = string | FileLike
12
+
11
13
export interface FormDataEncoderOptions {
12
14
/**
13
15
* When enabled, the encoder will emit additional per part headers, such as `Content-Length`.
@@ -49,8 +51,11 @@ export class FormDataEncoder {
49
51
/**
50
52
* FormData instance
51
53
*/
52
- readonly #form: FormDataLike
54
+ readonly #form: [ string , FormDataEntryValue ] [ ]
53
55
56
+ /**
57
+ * Instance options
58
+ */
54
59
readonly #options: FormDataEncoderOptions
55
60
56
61
/**
@@ -146,10 +151,7 @@ export class FormDataEncoder {
146
151
throw new TypeError ( "Expected options argument to be an object." )
147
152
}
148
153
149
- // ? Should I preserve FormData entries in array instead?
150
- // ? That way it will be immutable, but require to allocate a new array
151
- // ? and go through entries during initialization.
152
- this . #form = form
154
+ this . #form = Array . from ( form . entries ( ) )
153
155
154
156
this . #options = { ...defaultOptions , ...options }
155
157
@@ -163,7 +165,7 @@ export class FormDataEncoder {
163
165
`${ this . #DASHES} ${ this . boundary } ${ this . #DASHES} ${ this . #CRLF. repeat ( 2 ) } `
164
166
)
165
167
166
- this . contentLength = String ( this . getContentLength ( ) )
168
+ this . contentLength = String ( this . # getContentLength( ) )
167
169
168
170
this . headers = Object . freeze ( {
169
171
"Content-Type" : this . contentType ,
@@ -202,7 +204,7 @@ export class FormDataEncoder {
202
204
/**
203
205
* Returns form-data content length
204
206
*/
205
- getContentLength ( ) : number {
207
+ # getContentLength( ) : number {
206
208
let length = 0
207
209
208
210
for ( const [ name , raw ] of this . #form) {
@@ -218,6 +220,15 @@ export class FormDataEncoder {
218
220
return length + this . #footer. byteLength
219
221
}
220
222
223
+ /**
224
+ * Returns form-data content length
225
+ *
226
+ * @deprecated Use FormDataEncoder.contentLength or FormDataEncoder.headers["Content-Length"] instead
227
+ */
228
+ getContentLength ( ) : number {
229
+ return this . #getContentLength( )
230
+ }
231
+
221
232
/**
222
233
* Creates an iterator allowing to go through form-data parts (with metadata).
223
234
* This method **will not** read the files.
@@ -255,7 +266,7 @@ export class FormDataEncoder {
255
266
* console.log(await response.json())
256
267
*/
257
268
* values ( ) : Generator < Uint8Array | FileLike , void , undefined > {
258
- for ( const [ name , raw ] of this . #form. entries ( ) ) {
269
+ for ( const [ name , raw ] of this . #form) {
259
270
const value = isFile ( raw ) ? raw : this . #encoder. encode ( normalize ( raw ) )
260
271
261
272
yield this . #getFieldHeader( name , value )
0 commit comments