Skip to content

Commit

Permalink
Revert headers changes due to #15
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream committed Apr 5, 2023
1 parent ccbd012 commit cf92e3d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 83 deletions.
62 changes: 0 additions & 62 deletions lib/FormDataEncoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,6 @@ test("Has contentLength property", async t => {
)
})

test(
"contentLength property is undefined if there's file without known length",

t => {
const form = new FormData()

form.set("stream", {
[Symbol.toStringTag]: "File",
name: "file.txt",
stream() {
return Readable.from([Buffer.from("foo")])
}
})

const encoder = new FormDataEncoder(form)

t.is(encoder.contentLength, undefined)
}
)

test("contentLength property is read-only", t => {
const encoder = new FormDataEncoder(new FormData())

Expand Down Expand Up @@ -157,28 +137,6 @@ test("Has correct headers", async t => {
})
})

test(
"Has only Content-Type header if there's file without known length",

t => {
const form = new FormData()

form.set("stream", {
[Symbol.toStringTag]: "File",
name: "file.txt",
stream() {
return Readable.from([Buffer.from("foo")])
}
})

const encoder = new FormDataEncoder(form)

t.deepEqual(encoder.headers, {
"Content-Type": `multipart/form-data; boundary=${encoder.boundary}`
})
}
)

test("Yields correct footer for empty FormData", async t => {
const encoder = new FormDataEncoder(new FormData())

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

test(
".getContentLength() returns undefined if there's file without known length",

t => {
const form = new FormData()

form.set("stream", {
[Symbol.toStringTag]: "File",
name: "file.txt",
stream() {
return Readable.from([Buffer.from("foo")])
}
})

const encoder = new FormDataEncoder(form)

t.is(encoder.getContentLength(), undefined)
}
)

test(".values() yields headers as Uint8Array", t => {
const form = new FormData()

Expand Down
30 changes: 9 additions & 21 deletions lib/FormDataEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {FileLike} from "./FileLike"

interface Headers {
"Content-Type": string
"Content-Length"?: string
"Content-Length": string
}

export interface FormDataEncoderOptions {
Expand Down Expand Up @@ -74,7 +74,7 @@ export class FormDataEncoder {
/**
* Returns Content-Length header
*/
readonly contentLength: string | undefined
readonly contentLength: string

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

const contentLength = this.getContentLength()
const headers: Headers = {
"Content-Type": this.contentType
}
this.contentLength = String(this.getContentLength())

if (contentLength != null) {
this.contentLength = String(contentLength)
headers["Content-Length"] = this.contentLength
}

this.headers = Object.freeze(headers)
this.headers = Object.freeze<Headers>({
"Content-Length": this.contentLength,
"Content-Type": this.contentType
})

// Make sure following properties read-only in runtime.
Object.defineProperties(this, {
Expand Down Expand Up @@ -213,22 +208,15 @@ export class FormDataEncoder {
/**
* Returns form-data content length
*/
getContentLength(): number | undefined {
getContentLength(): number {
let length = 0

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

const size = isFileLike(value) ? value.size : value.byteLength

// Return `undefined` if encountered part without known size
if (size == null || isNaN(size)) {
return undefined
}

length += this.#getFieldHeader(name, value).byteLength

length += size
length += isFileLike(value) ? value.size : value.byteLength

length += this.#CRLF_BYTES_LENGTH
}
Expand Down

0 comments on commit cf92e3d

Please sign in to comment.