Skip to content

Commit cf92e3d

Browse files
committed
Revert headers changes due to #15
1 parent ccbd012 commit cf92e3d

File tree

2 files changed

+9
-83
lines changed

2 files changed

+9
-83
lines changed

lib/FormDataEncoder.test.ts

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,6 @@ test("Has contentLength property", async t => {
106106
)
107107
})
108108

109-
test(
110-
"contentLength property is undefined if there's file without known length",
111-
112-
t => {
113-
const form = new FormData()
114-
115-
form.set("stream", {
116-
[Symbol.toStringTag]: "File",
117-
name: "file.txt",
118-
stream() {
119-
return Readable.from([Buffer.from("foo")])
120-
}
121-
})
122-
123-
const encoder = new FormDataEncoder(form)
124-
125-
t.is(encoder.contentLength, undefined)
126-
}
127-
)
128-
129109
test("contentLength property is read-only", t => {
130110
const encoder = new FormDataEncoder(new FormData())
131111

@@ -157,28 +137,6 @@ test("Has correct headers", async t => {
157137
})
158138
})
159139

160-
test(
161-
"Has only Content-Type header if there's file without known length",
162-
163-
t => {
164-
const form = new FormData()
165-
166-
form.set("stream", {
167-
[Symbol.toStringTag]: "File",
168-
name: "file.txt",
169-
stream() {
170-
return Readable.from([Buffer.from("foo")])
171-
}
172-
})
173-
174-
const encoder = new FormDataEncoder(form)
175-
176-
t.deepEqual(encoder.headers, {
177-
"Content-Type": `multipart/form-data; boundary=${encoder.boundary}`
178-
})
179-
}
180-
)
181-
182140
test("Yields correct footer for empty FormData", async t => {
183141
const encoder = new FormDataEncoder(new FormData())
184142

@@ -215,26 +173,6 @@ test("Returns the length of the FormData content", async t => {
215173
t.is(encoder.getContentLength(), expected)
216174
})
217175

218-
test(
219-
".getContentLength() returns undefined if there's file without known length",
220-
221-
t => {
222-
const form = new FormData()
223-
224-
form.set("stream", {
225-
[Symbol.toStringTag]: "File",
226-
name: "file.txt",
227-
stream() {
228-
return Readable.from([Buffer.from("foo")])
229-
}
230-
})
231-
232-
const encoder = new FormDataEncoder(form)
233-
234-
t.is(encoder.getContentLength(), undefined)
235-
}
236-
)
237-
238176
test(".values() yields headers as Uint8Array", t => {
239177
const form = new FormData()
240178

lib/FormDataEncoder.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {FileLike} from "./FileLike"
1313

1414
interface Headers {
1515
"Content-Type": string
16-
"Content-Length"?: string
16+
"Content-Length": string
1717
}
1818

1919
export interface FormDataEncoderOptions {
@@ -74,7 +74,7 @@ export class FormDataEncoder {
7474
/**
7575
* Returns Content-Length header
7676
*/
77-
readonly contentLength: string | undefined
77+
readonly contentLength: string
7878

7979
/**
8080
* Returns headers object with Content-Type and Content-Length header
@@ -168,17 +168,12 @@ export class FormDataEncoder {
168168
`${this.#DASHES}${this.boundary}${this.#DASHES}${this.#CRLF.repeat(2)}`
169169
)
170170

171-
const contentLength = this.getContentLength()
172-
const headers: Headers = {
173-
"Content-Type": this.contentType
174-
}
171+
this.contentLength = String(this.getContentLength())
175172

176-
if (contentLength != null) {
177-
this.contentLength = String(contentLength)
178-
headers["Content-Length"] = this.contentLength
179-
}
180-
181-
this.headers = Object.freeze(headers)
173+
this.headers = Object.freeze<Headers>({
174+
"Content-Length": this.contentLength,
175+
"Content-Type": this.contentType
176+
})
182177

183178
// Make sure following properties read-only in runtime.
184179
Object.defineProperties(this, {
@@ -213,22 +208,15 @@ export class FormDataEncoder {
213208
/**
214209
* Returns form-data content length
215210
*/
216-
getContentLength(): number | undefined {
211+
getContentLength(): number {
217212
let length = 0
218213

219214
for (const [name, raw] of this.#form) {
220215
const value = isFileLike(raw) ? raw : this.#encoder.encode(normalize(raw))
221216

222-
const size = isFileLike(value) ? value.size : value.byteLength
223-
224-
// Return `undefined` if encountered part without known size
225-
if (size == null || isNaN(size)) {
226-
return undefined
227-
}
228-
229217
length += this.#getFieldHeader(name, value).byteLength
230218

231-
length += size
219+
length += isFileLike(value) ? value.size : value.byteLength
232220

233221
length += this.#CRLF_BYTES_LENGTH
234222
}

0 commit comments

Comments
 (0)