Skip to content

Commit 03722ec

Browse files
committed
Simplify createBoundary string generator.
1 parent 96275aa commit 03722ec

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

lib/Encoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class Encoder {
6565
*
6666
* const encoder = new Encoder(fd)
6767
*/
68-
constructor(form: FormDataLike, boundary: string = createBoundary(8)) {
68+
constructor(form: FormDataLike, boundary: string = createBoundary(16)) {
6969
if (!isFormData(form)) {
7070
throw new TypeError("Expected first argument to be a FormData instance.")
7171
}

lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from "./Encoder"
44

55
export {default as isFileLike} from "./util/isFile"
66
export {default as isFormDataLike} from "./util/isFormData"
7+
export {default as createBoundary} from "./util/createBoundary"

lib/util/createBoundary.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import {randomBytes} from "crypto"
1+
const alphabet
2+
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
23

34
/**
45
* Generates a boundary string for FormData encoder.
56
*/
6-
const createBoundary = (size: number): string => (
7-
randomBytes(size).toString("hex")
8-
)
7+
function createBoundary(size: number): string {
8+
let res = ""
9+
10+
while (size--) {
11+
res += alphabet[Math.floor(Math.random() * alphabet.length)]
12+
}
13+
14+
return res
15+
}
916

1017
export default createBoundary

0 commit comments

Comments
 (0)