Skip to content

Commit 1b62adb

Browse files
committed
Preserve FormData entries into array in FormDataEncoder instance
1 parent e6526b3 commit 1b62adb

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/FormDataEncoder.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {isFormData} from "./util/isFormData.js"
88
import {FormDataLike} from "./FormDataLike.js"
99
import {FileLike} from "./FileLike.js"
1010

11+
type FormDataEntryValue = string | FileLike
12+
1113
export interface FormDataEncoderOptions {
1214
/**
1315
* When enabled, the encoder will emit additional per part headers, such as `Content-Length`.
@@ -49,8 +51,11 @@ export class FormDataEncoder {
4951
/**
5052
* FormData instance
5153
*/
52-
readonly #form: FormDataLike
54+
readonly #form: [string, FormDataEntryValue][]
5355

56+
/**
57+
* Instance options
58+
*/
5459
readonly #options: FormDataEncoderOptions
5560

5661
/**
@@ -146,10 +151,7 @@ export class FormDataEncoder {
146151
throw new TypeError("Expected options argument to be an object.")
147152
}
148153

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())
153155

154156
this.#options = {...defaultOptions, ...options}
155157

@@ -163,7 +165,7 @@ export class FormDataEncoder {
163165
`${this.#DASHES}${this.boundary}${this.#DASHES}${this.#CRLF.repeat(2)}`
164166
)
165167

166-
this.contentLength = String(this.getContentLength())
168+
this.contentLength = String(this.#getContentLength())
167169

168170
this.headers = Object.freeze({
169171
"Content-Type": this.contentType,
@@ -202,7 +204,7 @@ export class FormDataEncoder {
202204
/**
203205
* Returns form-data content length
204206
*/
205-
getContentLength(): number {
207+
#getContentLength(): number {
206208
let length = 0
207209

208210
for (const [name, raw] of this.#form) {
@@ -218,6 +220,15 @@ export class FormDataEncoder {
218220
return length + this.#footer.byteLength
219221
}
220222

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+
221232
/**
222233
* Creates an iterator allowing to go through form-data parts (with metadata).
223234
* This method **will not** read the files.
@@ -255,7 +266,7 @@ export class FormDataEncoder {
255266
* console.log(await response.json())
256267
*/
257268
* values(): Generator<Uint8Array | FileLike, void, undefined> {
258-
for (const [name, raw] of this.#form.entries()) {
269+
for (const [name, raw] of this.#form) {
259270
const value = isFile(raw) ? raw : this.#encoder.encode(normalize(raw))
260271

261272
yield this.#getFieldHeader(name, value)

0 commit comments

Comments
 (0)