@@ -5,12 +5,6 @@ import isFile from "./util/isFile"
55
66import { FormDataLike } from "./FormDataLike"
77
8- const DASHES = "-" . repeat ( 2 )
9-
10- const CRLF = "\r\n"
11- const CRLF_BYTES = new TextEncoder ( ) . encode ( CRLF )
12- const CRLF_BYTES_LENGTH = CRLF_BYTES . byteLength
13-
148export class Encoder {
159 /**
1610 * Returns boundary string
@@ -30,6 +24,16 @@ export class Encoder {
3024 "Content-Length" : number
3125 }
3226
27+ readonly #CRLF: string
28+
29+ readonly #CRLF_BYTES: Uint8Array
30+
31+ readonly #CRLF_BYTES_LENGTH: number
32+
33+ readonly #DASHES = "-" . repeat ( 2 )
34+
35+ readonly #encoder: TextEncoder
36+
3337 /**
3438 * Returns field's footer
3539 */
@@ -69,9 +73,15 @@ export class Encoder {
6973 this . boundary = boundary
7074 this . contentType = `multipart/form-data; boundary=${ this . boundary } `
7175
76+ this . #encoder = new TextEncoder ( )
77+
78+ this . #CRLF = "\r\n"
79+ this . #CRLF_BYTES = this . #encoder. encode ( this . #CRLF)
80+ this . #CRLF_BYTES_LENGTH = this . #CRLF_BYTES. byteLength
81+
7282 this . #form = form
73- this . #footer = new TextEncoder ( ) . encode (
74- `${ DASHES } ${ this . boundary } ${ DASHES } ${ CRLF . repeat ( 2 ) } `
83+ this . #footer = this . #encoder . encode (
84+ `${ this . # DASHES} ${ this . boundary } ${ this . # DASHES} ${ this . # CRLF. repeat ( 2 ) } `
7585 )
7686
7787 this . headers = Object . freeze ( {
@@ -83,15 +93,15 @@ export class Encoder {
8393 private _getFieldHeader ( name : string , value : unknown ) : Uint8Array {
8494 let header = ""
8595
86- header += `${ DASHES } ${ this . boundary } ${ CRLF } `
96+ header += `${ this . # DASHES} ${ this . boundary } ${ this . # CRLF} `
8797 header += `Content-Disposition: form-data; name="${ name } "`
8898
8999 if ( isFile ( value ) ) {
90- header += `; filename="${ value . name } "${ CRLF } `
100+ header += `; filename="${ value . name } "${ this . # CRLF} `
91101 header += `Content-Type: ${ value . type || getMime ( value . name ) } `
92102 }
93103
94- return new TextEncoder ( ) . encode ( `${ header } ${ CRLF . repeat ( 2 ) } ` )
104+ return this . #encoder . encode ( `${ header } ${ this . # CRLF. repeat ( 2 ) } ` )
95105 }
96106
97107 /**
@@ -105,9 +115,9 @@ export class Encoder {
105115
106116 length += isFile ( value )
107117 ? value . size
108- : new TextEncoder ( ) . encode ( String ( value ) ) . byteLength
118+ : this . #encoder . encode ( String ( value ) ) . byteLength
109119
110- length += CRLF_BYTES_LENGTH
120+ length += this . # CRLF_BYTES_LENGTH
111121 }
112122
113123 return length + this . #footer. byteLength
@@ -150,10 +160,10 @@ export class Encoder {
150160 if ( isFile ( value ) ) {
151161 yield * value . stream ( )
152162 } else {
153- yield new TextEncoder ( ) . encode ( String ( value ) )
163+ yield this . #encoder . encode ( String ( value ) )
154164 }
155165
156- yield CRLF_BYTES
166+ yield this . # CRLF_BYTES
157167 }
158168
159169 yield this . #footer
0 commit comments