Skip to content

Commit 1f6186c

Browse files
committed
Implement custom boundary generator using crypto API (node and browser)
1 parent 4ef7183 commit 1f6186c

File tree

5 files changed

+15
-24
lines changed

5 files changed

+15
-24
lines changed

lib/Encoder.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test("Accepts custom boundary as the second argument", t => {
2222

2323
const encoder = new Encoder(new FormData(), expected)
2424

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

2828
test("Has content-type string", t => {
@@ -36,7 +36,10 @@ test("Has content-type string with custom boundary string", t => {
3636

3737
const encoder = new Encoder(new FormData(), expected)
3838

39-
t.is(encoder.boundary, expected)
39+
t.is(
40+
encoder.contentType,
41+
`multipart/form-data; boundary=form-data-boundary-${expected}`
42+
)
4043
})
4144

4245
test("Has correct headers", async t => {
@@ -246,7 +249,8 @@ test("Yields every appended File", async t => {
246249
test(
247250
"Throws TypeError when the first argument is not a FormData instance",
248251
t => {
249-
const trap = () => new Encoder({} as any)
252+
// @ts-expect-error
253+
const trap = () => new Encoder({})
250254

251255
t.throws(trap, {
252256
instanceOf: TypeError,
@@ -256,7 +260,8 @@ test(
256260
)
257261

258262
test("Throws TypeError when given boundary is not a string", t => {
259-
const trap = () => new Encoder(new FormData(), 42 as any)
263+
// @ts-expect-error
264+
const trap = () => new Encoder(new FormData(), 42)
260265

261266
t.throws(trap, {
262267
instanceOf: TypeError,

lib/Encoder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Encoder {
6464
*
6565
* const encoder = new Encoder(fd)
6666
*/
67-
constructor(form: FormDataLike, boundary: string = createBoundary()) {
67+
constructor(form: FormDataLike, boundary: string = createBoundary(8)) {
6868
if (!isFormData(form)) {
6969
throw new TypeError("Expected first argument to be a FormData instance.")
7070
}
@@ -73,7 +73,7 @@ export class Encoder {
7373
throw new TypeError("Expected boundary to be a string.")
7474
}
7575

76-
this.boundary = boundary
76+
this.boundary = `form-data-boundary-${boundary}`
7777
this.contentType = `multipart/form-data; boundary=${this.boundary}`
7878

7979
this.#encoder = new TextEncoder()

lib/util/createBoundary.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import {customAlphabet} from "nanoid"
2-
3-
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
4-
const generate = customAlphabet(alphabet, 16)
1+
import {randomBytes} from "crypto"
52

63
/**
74
* Generates a boundary string for FormData encoder.
85
*/
9-
const createBoundary = (): string => `form-data-boundary-${generate()}`
6+
const createBoundary = (size: number): string => (
7+
randomBytes(size).toString("hex")
8+
)
109

1110
export default createBoundary

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,5 @@
5858
"ts-node": "10.0.0",
5959
"ttypescript": "1.5.12",
6060
"typescript": "4.3.4"
61-
},
62-
"dependencies": {
63-
"nanoid": "3.1.23"
6461
}
6562
}

pnpm-lock.yaml

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)