Skip to content

Commit 66afa0f

Browse files
committed
fix(encoder): Make boundary fully customizable
1 parent 72c6570 commit 66afa0f

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/FormDataEncoder.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import {readFile} from "node:fs/promises"
2-
import {Readable} from "node:stream"
32
import {EOL} from "node:os"
3+
import {Readable} from "node:stream"
44

55
import test from "ava"
66

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

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

12-
import skipSync from "./__helper__/skipIterationsSync.js"
12+
import readLine from "./__helper__/readLine.js"
1313
import readStream from "./__helper__/readStream.js"
1414
import skip from "./__helper__/skipIterations.js"
15-
import readLine from "./__helper__/readLine.js"
15+
import skipSync from "./__helper__/skipIterationsSync.js"
1616

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

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

26+
test("Default boundary starts with a prefix", t => {
27+
const encoder = new FormDataEncoder(new FormData())
28+
29+
t.true(encoder.boundary.startsWith("form-data-encoder-"))
30+
})
31+
2632
test("boundary property is read-only", t => {
2733
const encoder = new FormDataEncoder(new FormData())
2834

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

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

62-
t.is(encoder.boundary, `form-data-boundary-${expected}`)
68+
t.is(encoder.boundary, expected)
6369
})
6470

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

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

109-
t.is(
110-
encoder.contentType,
111-
`multipart/form-data; boundary=form-data-boundary-${expected}`
112-
)
115+
t.is(encoder.contentType, `multipart/form-data; boundary=${expected}`)
113116
})
114117

115118
test("Has contentLength property", async t => {

src/FormDataEncoder.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type {RawHeaders, FormDataEncoderHeaders} from "./util/Headers.js"
2-
import {getStreamIterator} from "./util/getStreamIterator.js"
3-
import {createBoundary} from "./util/createBoundary.js"
4-
import {normalizeValue} from "./util/normalizeValue.js"
5-
import {isPlainObject} from "./util/isPlainObject.js"
6-
import {proxyHeaders} from "./util/proxyHeaders.js"
1+
import type {FileLike} from "./FileLike.js"
72
import type {FormDataLike} from "./FormDataLike.js"
8-
import {isFormData} from "./util/isFormData.js"
3+
import type {FormDataEncoderHeaders, RawHeaders} from "./util/Headers.js"
4+
import {chunk} from "./util/chunk.js"
5+
import {createBoundary} from "./util/createBoundary.js"
96
import {escapeName} from "./util/escapeName.js"
10-
import type {FileLike} from "./FileLike.js"
7+
import {getStreamIterator} from "./util/getStreamIterator.js"
118
import {isFile} from "./util/isFile.js"
12-
import {chunk} from "./util/chunk.js"
9+
import {isFormData} from "./util/isFormData.js"
10+
import {isPlainObject} from "./util/isPlainObject.js"
11+
import {normalizeValue} from "./util/normalizeValue.js"
12+
import {proxyHeaders} from "./util/proxyHeaders.js"
1313

1414
type FormDataEntryValue = string | FileLike
1515

@@ -157,7 +157,7 @@ export class FormDataEncoder {
157157

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

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

178-
this.boundary = `form-data-boundary-${boundary}`
178+
this.boundary = boundary
179179
this.contentType = `multipart/form-data; boundary=${this.boundary}`
180180

181181
this.#footer = this.#encoder.encode(

0 commit comments

Comments
 (0)