Skip to content

Commit b71e9d6

Browse files
chore: Improve decrypt performance for large payloads (#1501)
1 parent e248a96 commit b71e9d6

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

Diff for: modules/decrypt-node/src/verify_stream.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ export class VerifyStream extends PortableTransformWithType {
129129
const { currentFrame } = state
130130
if (!currentFrame) {
131131
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+
133137
const frameHeader = decodeBodyHeader(frameBuffer, this._headerInfo, 0)
134138
if (!frameHeader) {
135139
// Need more data
@@ -192,13 +196,18 @@ export class VerifyStream extends PortableTransformWithType {
192196
if (chunk.length && tagLengthBytes > authTagBuffer.length) {
193197
const left = tagLengthBytes - authTagBuffer.length
194198
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
196204
return callback()
197205
} 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)
202211
if (this._verify) {
203212
this._verify.update(finalAuthTagBuffer)
204213
}

0 commit comments

Comments
 (0)