From 3da03d23baa90acb119c7946c2cd740a72ba069d Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Mon, 21 Oct 2024 10:41:00 +0300 Subject: [PATCH] fix(BodyHashStream): Skip header --- lib/dkim/body/index.js | 51 +++++------------------------------------- 1 file changed, 6 insertions(+), 45 deletions(-) diff --git a/lib/dkim/body/index.js b/lib/dkim/body/index.js index 15328ad..0234215 100644 --- a/lib/dkim/body/index.js +++ b/lib/dkim/body/index.js @@ -3,7 +3,6 @@ const { SimpleHash } = require('./simple'); const { RelaxedHash } = require('./relaxed'); const { Transform } = require('node:stream'); -const { MessageParser } = require('../message-parser'); const dkimBody = (canonicalization, ...options) => { canonicalization = (canonicalization || 'simple/simple').toString().split('/').pop().toLowerCase().trim(); @@ -20,50 +19,15 @@ const dkimBody = (canonicalization, ...options) => { } }; -class MessageHasher extends MessageParser { - constructor(canonicalization, ...options) { - super(); - this.hasher = dkimBody(canonicalization, ...options); - this.bodyHash = null; - } - - async nextChunk(chunk) { - this.hasher.update(chunk); - } - - async finalChunk() { - this.bodyHash = this.hasher.digest('base64'); - } -} - class BodyHashStream extends Transform { constructor(canonicalization, ...options) { super(); - this.finished = false; - this.finishCb = null; - this.byteLength = 0; - this.messageHasher = new MessageHasher(canonicalization, ...options); - this.bodyHash = null; - this.messageHasher.once('finish', () => this.finishHashing()); - this.messageHasher.once('end', () => this.finishHashing()); - this.messageHasher.once('error', err => this.destroy(err)); - } - - finishHashing() { - if (this.finished || !this.finishCb) { - return; - } - this.finished = true; - let done = this.finishCb; - this.finishCb = null; - - this.bodyHash = this.messageHasher.bodyHash; - this.emit('hash', this.bodyHash); + this.hasher = dkimBody(canonicalization, ...options); - done(); + this.bodyHash = null; } _transform(chunk, encoding, done) { @@ -77,19 +41,16 @@ class BodyHashStream extends Transform { this.byteLength += chunk.length; + this.hasher.update(chunk); this.push(chunk); - if (this.messageHasher.write(chunk) === false) { - // wait for drain - return this.messageHasher.once('drain', done); - } - done(); } _flush(done) { - this.finishCb = done; - this.messageHasher.end(); + this.bodyHash = this.hasher.digest('base64'); + this.emit('hash', this.bodyHash); + done(); } }