@@ -129,7 +129,11 @@ export class VerifyStream extends PortableTransformWithType {
129
129
const { currentFrame } = state
130
130
if ( ! currentFrame ) {
131
131
const { buffer } = state
132
- const frameBuffer = Buffer . concat ( [ buffer , chunk ] )
132
+
133
+ // Buffer.concat can be expensive. If buffer is empty, just use the chunk.
134
+ const frameBuffer =
135
+ buffer . length > 0 ? Buffer . concat ( [ buffer , chunk ] ) : chunk
136
+
133
137
const frameHeader = decodeBodyHeader ( frameBuffer , this . _headerInfo , 0 )
134
138
if ( ! frameHeader ) {
135
139
// Need more data
@@ -192,13 +196,18 @@ export class VerifyStream extends PortableTransformWithType {
192
196
if ( chunk . length && tagLengthBytes > authTagBuffer . length ) {
193
197
const left = tagLengthBytes - authTagBuffer . length
194
198
if ( left > chunk . length ) {
195
- state . authTagBuffer = Buffer . concat ( [ authTagBuffer , chunk ] )
199
+ // Buffer.concat can be expensive. If buffer is empty, just use the chunk.
200
+ state . authTagBuffer =
201
+ authTagBuffer . length > 0
202
+ ? Buffer . concat ( [ authTagBuffer , chunk ] )
203
+ : chunk
196
204
return callback ( )
197
205
} else {
198
- const finalAuthTagBuffer = Buffer . concat (
199
- [ authTagBuffer , chunk ] ,
200
- tagLengthBytes
201
- )
206
+ // Buffer.concat can be expensive. If buffer is empty, just use the chunk.
207
+ const finalAuthTagBuffer =
208
+ authTagBuffer . length > 0
209
+ ? Buffer . concat ( [ authTagBuffer , chunk ] , tagLengthBytes )
210
+ : chunk . slice ( 0 , tagLengthBytes )
202
211
if ( this . _verify ) {
203
212
this . _verify . update ( finalAuthTagBuffer )
204
213
}
0 commit comments