From cf92e3dee8b71e1fa5f1a46592b04b6520690863 Mon Sep 17 00:00:00 2001 From: Nick K Date: Wed, 5 Apr 2023 21:56:51 +0300 Subject: [PATCH] Revert headers changes due to #15 --- lib/FormDataEncoder.test.ts | 62 ------------------------------------- lib/FormDataEncoder.ts | 30 ++++++------------ 2 files changed, 9 insertions(+), 83 deletions(-) diff --git a/lib/FormDataEncoder.test.ts b/lib/FormDataEncoder.test.ts index 1e62ba3..8c20f3a 100644 --- a/lib/FormDataEncoder.test.ts +++ b/lib/FormDataEncoder.test.ts @@ -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()) @@ -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()) @@ -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() diff --git a/lib/FormDataEncoder.ts b/lib/FormDataEncoder.ts index 8629b5f..ac8756d 100644 --- a/lib/FormDataEncoder.ts +++ b/lib/FormDataEncoder.ts @@ -13,7 +13,7 @@ import {FileLike} from "./FileLike" interface Headers { "Content-Type": string - "Content-Length"?: string + "Content-Length": string } export interface FormDataEncoderOptions { @@ -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 @@ -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({ + "Content-Length": this.contentLength, + "Content-Type": this.contentType + }) // Make sure following properties read-only in runtime. Object.defineProperties(this, { @@ -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 }