@@ -11,14 +11,14 @@ import {isFile} from "./util/isFile.js"
11
11
12
12
type FormDataEntryValue = string | FileLike
13
13
14
- type RawHeaders = Readonly < {
14
+ interface RawHeaders {
15
15
"Content-Type" : string
16
- "Content-Length" : string
17
- } >
16
+ "Content-Length" ? : string
17
+ }
18
18
19
19
export type FormDataEncoderHeaders =
20
- & RawHeaders
21
- & LowercaseObjectKeys < RawHeaders >
20
+ & Readonly < RawHeaders >
21
+ & Readonly < LowercaseObjectKeys < RawHeaders > >
22
22
23
23
export interface FormDataEncoderOptions {
24
24
/**
@@ -83,7 +83,7 @@ export class FormDataEncoder {
83
83
/**
84
84
* Returns Content-Length header
85
85
*/
86
- readonly contentLength : string
86
+ readonly contentLength : string | undefined
87
87
88
88
/**
89
89
* Returns headers object with Content-Type and Content-Length header
@@ -174,12 +174,17 @@ export class FormDataEncoder {
174
174
`${ this . #DASHES} ${ this . boundary } ${ this . #DASHES} ${ this . #CRLF. repeat ( 2 ) } `
175
175
)
176
176
177
- this . contentLength = this . #getContentLength( )
177
+ const headers : RawHeaders = {
178
+ "Content-Type" : this . contentType
179
+ }
180
+
181
+ const contentLength = this . #getContentLength( )
182
+ if ( contentLength ) {
183
+ this . contentLength = contentLength
184
+ headers [ "Content-Length" ] = contentLength
185
+ }
178
186
179
- this . headers = proxyHeaders ( Object . freeze ( {
180
- "Content-Type" : this . contentType ,
181
- "Content-Length" : this . contentLength
182
- } ) )
187
+ this . headers = proxyHeaders ( Object . freeze ( headers ) )
183
188
184
189
// Make sure following properties read-only in runtime.
185
190
Object . defineProperties ( this , {
@@ -214,17 +219,24 @@ export class FormDataEncoder {
214
219
/**
215
220
* Returns form-data content length
216
221
*/
217
- #getContentLength( ) : string {
222
+ #getContentLength( ) : string | undefined {
218
223
let length = 0
219
224
220
225
for ( const [ name , raw ] of this . #form) {
221
226
const value = isFile ( raw ) ? raw : this . #encoder. encode (
222
227
normalizeValue ( raw )
223
228
)
224
229
230
+ const size = isFile ( value ) ? value . size : value . byteLength
231
+
232
+ // Return `undefined` if encountered part without known size
233
+ if ( size == null || Number . isNaN ( size ) ) {
234
+ return undefined
235
+ }
236
+
225
237
length += this . #getFieldHeader( name , value ) . byteLength
226
238
227
- length += isFile ( value ) ? value . size : value . byteLength
239
+ length += size
228
240
229
241
length += this . #CRLF_BYTES_LENGTH
230
242
}
@@ -237,8 +249,8 @@ export class FormDataEncoder {
237
249
*
238
250
* @deprecated Use FormDataEncoder.contentLength or FormDataEncoder.headers["Content-Length"] instead
239
251
*/
240
- getContentLength ( ) : number {
241
- return Number ( this . contentLength )
252
+ getContentLength ( ) : number | undefined {
253
+ return this . contentLength == null ? undefined : Number ( this . contentLength )
242
254
}
243
255
244
256
/**
0 commit comments