Skip to content

Do not force form-data-boundary, make it fully customizable #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/forty-rules-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"form-data-encoder": minor
---

Make boundary fully customizable
21 changes: 12 additions & 9 deletions src/FormDataEncoder.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {readFile} from "node:fs/promises"
import {Readable} from "node:stream"
import {EOL} from "node:os"
import {Readable} from "node:stream"

import test from "ava"

// eslint-disable-next-line import/no-unresolved
import {fileFromPath} from "formdata-node/file-from-path"

import {FormData, Blob, File} from "formdata-node"
import {Blob, File, FormData} from "formdata-node"

import skipSync from "./__helper__/skipIterationsSync.js"
import readLine from "./__helper__/readLine.js"
import readStream from "./__helper__/readStream.js"
import skip from "./__helper__/skipIterations.js"
import readLine from "./__helper__/readLine.js"
import skipSync from "./__helper__/skipIterationsSync.js"

import {FormDataEncoder} from "./FormDataEncoder.js"

Expand All @@ -23,6 +23,12 @@ test("Has boundary string", t => {
t.is(typeof encoder.boundary, "string")
})

test("Default boundary starts with a prefix", t => {
const encoder = new FormDataEncoder(new FormData())

t.true(encoder.boundary.startsWith("form-data-encoder-"))
})

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

Expand Down Expand Up @@ -59,7 +65,7 @@ test("Accepts custom boundary as the second argument", t => {

const encoder = new FormDataEncoder(new FormData(), expected)

t.is(encoder.boundary, `form-data-boundary-${expected}`)
t.is(encoder.boundary, expected)
})

test("Has content-type string", t => {
Expand Down Expand Up @@ -106,10 +112,7 @@ test("Has content-type string with custom boundary string", t => {

const encoder = new FormDataEncoder(new FormData(), expected)

t.is(
encoder.contentType,
`multipart/form-data; boundary=form-data-boundary-${expected}`
)
t.is(encoder.contentType, `multipart/form-data; boundary=${expected}`)
})

test("Has contentLength property", async t => {
Expand Down
22 changes: 11 additions & 11 deletions src/FormDataEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type {RawHeaders, FormDataEncoderHeaders} from "./util/Headers.js"
import {getStreamIterator} from "./util/getStreamIterator.js"
import {createBoundary} from "./util/createBoundary.js"
import {normalizeValue} from "./util/normalizeValue.js"
import {isPlainObject} from "./util/isPlainObject.js"
import {proxyHeaders} from "./util/proxyHeaders.js"
import type {FileLike} from "./FileLike.js"
import type {FormDataLike} from "./FormDataLike.js"
import {isFormData} from "./util/isFormData.js"
import type {FormDataEncoderHeaders, RawHeaders} from "./util/Headers.js"
import {chunk} from "./util/chunk.js"
import {createBoundary} from "./util/createBoundary.js"
import {escapeName} from "./util/escapeName.js"
import type {FileLike} from "./FileLike.js"
import {getStreamIterator} from "./util/getStreamIterator.js"
import {isFile} from "./util/isFile.js"
import {chunk} from "./util/chunk.js"
import {isFormData} from "./util/isFormData.js"
import {isPlainObject} from "./util/isPlainObject.js"
import {normalizeValue} from "./util/normalizeValue.js"
import {proxyHeaders} from "./util/proxyHeaders.js"

type FormDataEntryValue = string | FileLike

Expand Down Expand Up @@ -157,7 +157,7 @@ export class FormDataEncoder {

// Use default generator when the boundary argument is not present
if (!boundary) {
boundary = createBoundary()
boundary = `form-data-encoder-${createBoundary()}`
}

if (typeof boundary !== "string") {
Expand All @@ -175,7 +175,7 @@ export class FormDataEncoder {
this.#CRLF_BYTES = this.#encoder.encode(this.#CRLF)
this.#CRLF_BYTES_LENGTH = this.#CRLF_BYTES.byteLength

this.boundary = `form-data-boundary-${boundary}`
this.boundary = boundary
this.contentType = `multipart/form-data; boundary=${this.boundary}`

this.#footer = this.#encoder.encode(
Expand Down