Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions modules/decrypt-node/src/verify_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ export class VerifyStream extends PortableTransformWithType {
const { currentFrame } = state
if (!currentFrame) {
const { buffer } = state
const frameBuffer = Buffer.concat([buffer, chunk])

// Buffer.concat can be expensive. If buffer is empty, just use the chunk.
const frameBuffer =
buffer.length > 0 ? Buffer.concat([buffer, chunk]) : chunk

const frameHeader = decodeBodyHeader(frameBuffer, this._headerInfo, 0)
if (!frameHeader) {
// Need more data
Expand Down Expand Up @@ -192,13 +196,18 @@ export class VerifyStream extends PortableTransformWithType {
if (chunk.length && tagLengthBytes > authTagBuffer.length) {
const left = tagLengthBytes - authTagBuffer.length
if (left > chunk.length) {
state.authTagBuffer = Buffer.concat([authTagBuffer, chunk])
// Buffer.concat can be expensive. If buffer is empty, just use the chunk.
state.authTagBuffer =
authTagBuffer.length > 0
? Buffer.concat([authTagBuffer, chunk])
: chunk
return callback()
} else {
const finalAuthTagBuffer = Buffer.concat(
[authTagBuffer, chunk],
tagLengthBytes
)
// Buffer.concat can be expensive. If buffer is empty, just use the chunk.
const finalAuthTagBuffer =
authTagBuffer.length > 0
? Buffer.concat([authTagBuffer, chunk], tagLengthBytes)
: chunk.slice(0, tagLengthBytes)
if (this._verify) {
this._verify.update(finalAuthTagBuffer)
}
Expand Down