Skip to content

Commit d49d588

Browse files
committed
Replace Buffer with TextEncoder + Uint8Array in Encoder implementation.
1 parent 51f68be commit d49d588

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

lib/Encoder.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {FormDataLike} from "./FormDataLike"
77

88
const DASHES = "-".repeat(2)
99
const CRLF = "\r\n"
10-
const CRLF_BYTES_LENGTH = Buffer.byteLength(CRLF)
10+
const CRLF_BYTES = new TextEncoder().encode(CRLF)
11+
const CRLF_BYTES_LENGTH = CRLF_BYTES.byteLength
1112

1213
export class Encoder {
1314
/**
@@ -23,7 +24,7 @@ export class Encoder {
2324
/**
2425
* Returns field's footer
2526
*/
26-
readonly #footer: string
27+
readonly #footer: Uint8Array
2728

2829
/**
2930
* FormData instance
@@ -43,7 +44,8 @@ export class Encoder {
4344
this.contentType = `multipart/form-data; boundary=${this.boundary}`
4445

4546
this.#form = form
46-
this.#footer = `${DASHES}${this.boundary}${DASHES}${CRLF.repeat(2)}`
47+
this.#footer = new TextEncoder()
48+
.encode(`${DASHES}${this.boundary}${DASHES}${CRLF.repeat(2)}`)
4749
}
4850

4951
/**
@@ -56,7 +58,7 @@ export class Encoder {
5658
}
5759
}
5860

59-
private _getFieldHeader(name: string, value: unknown) {
61+
private _getFieldHeader(name: string, value: unknown): Uint8Array {
6062
let header = ""
6163

6264
header += `${DASHES}${this.boundary}${CRLF}`
@@ -67,7 +69,7 @@ export class Encoder {
6769
header += `Content-Type: ${value.type || getMime(value.name)}`
6870
}
6971

70-
return `${header}${CRLF.repeat(2)}`
72+
return new TextEncoder().encode(`${header}${CRLF.repeat(2)}`)
7173
}
7274

7375
/**
@@ -77,39 +79,34 @@ export class Encoder {
7779
let length = 0
7880

7981
for (const [name, value] of this.#form) {
80-
length += Buffer.byteLength(this._getFieldHeader(name, value))
81-
length += isFile(value) ? value.size : Buffer.byteLength(String(value))
82+
length += this._getFieldHeader(name, value).byteLength
83+
84+
length += isFile(value)
85+
? value.size
86+
: new TextEncoder().encode(String(value)).byteLength
87+
8288
length += CRLF_BYTES_LENGTH
8389
}
8490

85-
return length + Buffer.byteLength(this.#footer)
91+
return length + this.#footer.byteLength
8692
}
8793

88-
private async* _getField(): AsyncGenerator<Buffer | string, void, undefined> {
94+
async* encode(): AsyncGenerator<Uint8Array, void, undefined> {
8995
for (const [name, value] of this.#form) {
9096
yield this._getFieldHeader(name, value)
9197

9298
if (isFile(value)) {
9399
yield* value.stream()
94100
} else {
95-
yield value
101+
yield new TextEncoder().encode(String(value))
96102
}
97103

98-
yield CRLF
104+
yield CRLF_BYTES
99105
}
100106

101107
yield this.#footer
102108
}
103109

104-
/**
105-
* Returns async generator allowing to encode FormData content into the spec format
106-
*/
107-
async* encode(): AsyncGenerator<Buffer, void, undefined> {
108-
for await (const chunk of this._getField()) {
109-
yield Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))
110-
}
111-
}
112-
113110
[Symbol.asyncIterator]() {
114111
return this.encode()
115112
}

0 commit comments

Comments
 (0)