Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 4b8d6c9

Browse files
committed
feat: ability to supply a custom boundary
1 parent bf3e01a commit 4b8d6c9

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
import { isErrored as streamIsErrored } from 'node:stream';
33
import { inspect } from 'node:util';
44

5+
interface Options {
6+
/**
7+
* A custom boundary to use in your generated FormData string. Will default to an internal
8+
* `undici` identifying boundary if not supplied.
9+
*/
10+
boundary?: string;
11+
}
12+
513
/**
614
* @see {@link https://stackoverflow.com/a/63361543/105698}
715
*/
@@ -38,11 +46,13 @@ function isBuffer(buffer) {
3846
* @license https://github.com/nodejs/undici/blob/e39a6324c4474c6614cac98b8668e3d036aa6b18/LICENSE
3947
* @see {@link https://github.com/nodejs/undici/blob/e39a6324c4474c6614cac98b8668e3d036aa6b18/lib/fetch/body.js#L31}
4048
*/
41-
function extractBody(object) {
49+
function extractBody(object, opts?: Options) {
4250
let source = null;
4351
let length = null;
4452

45-
const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}`;
53+
const boundary = opts?.boundary
54+
? opts.boundary
55+
: `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}`;
4656
const prefix = `--${boundary}\r\nContent-Disposition: form-data`;
4757

4858
/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
@@ -140,10 +150,10 @@ function extractBody(object) {
140150
* Convert an instance of the `FormData` API into a raw string.
141151
*
142152
*/
143-
export default async function formDataToString(form: FormData) {
153+
export default async function formDataToString(form: FormData, opts: Options = {}) {
144154
const {
145155
body: { stream },
146-
} = await extractBody(form);
156+
} = await extractBody(form, opts);
147157

148158
return streamToString(stream);
149159
}

test/__snapshots__/index.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`#formdata-to-string > options > #boundary > should support supplying a custom boundary 1`] = `
4+
"--CUSTOM-BOUNDARY
5+
Content-Disposition: form-data; name=\\"dog\\"
6+
7+
buster
8+
--CUSTOM-BOUNDARY--"
9+
`;
10+
311
exports[`#formdata-to-string > should convert a basic instance into a string 1`] = `
412
"------formdata-undici-TIMESTAMP
513
Content-Disposition: form-data; name=\\"dog\\"

test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@ describe('#formdata-to-string', () => {
4040
const output = await formDataToString(form).then(prepareOutputForSnapshot);
4141
expect(output).toMatchSnapshot();
4242
});
43+
44+
describe('options', () => {
45+
describe('#boundary', () => {
46+
it('should support supplying a custom boundary', async () => {
47+
const form = new FormData();
48+
form.append('dog', 'buster');
49+
50+
const output = await formDataToString(form, { boundary: 'CUSTOM-BOUNDARY' }).then(prepareOutputForSnapshot);
51+
expect(output).toMatchSnapshot();
52+
});
53+
});
54+
});
4355
});

0 commit comments

Comments
 (0)