Comparing version 4.7.0 to 4.7.1
# Changelog | ||
## [4.7.1](https://github.com/postalsys/mailauth/compare/v4.7.0...v4.7.1) (2024-10-02) | ||
### Bug Fixes | ||
* **dkim:** New class BodyHashStream ([88d2fad](https://github.com/postalsys/mailauth/commit/88d2fad329a9a6fc8ebc1da4efc1c4844ae49507)) | ||
## [4.7.0](https://github.com/postalsys/mailauth/compare/v4.6.9...v4.7.0) (2024-10-02) | ||
@@ -4,0 +11,0 @@ |
'use strict'; | ||
let { SimpleHash } = require('./simple'); | ||
let { RelaxedHash } = require('./relaxed'); | ||
const { SimpleHash } = require('./simple'); | ||
const { RelaxedHash } = require('./relaxed'); | ||
const { Transform } = require('node:stream'); | ||
const { MessageParser } = require('../message-parser'); | ||
@@ -21,2 +23,70 @@ const dkimBody = (canonicalization, ...options) => { | ||
module.exports = { dkimBody }; | ||
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.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); | ||
done(); | ||
} | ||
_transform(chunk, encoding, done) { | ||
if (!chunk || !chunk.length) { | ||
return done(); | ||
} | ||
if (typeof chunk === 'string') { | ||
chunk = Buffer.from(chunk, encoding); | ||
} | ||
if (this.messageHasher.write(chunk) === false) { | ||
// wait for drain | ||
return this.messageHasher.once('drain', done); | ||
} | ||
this.push(chunk); | ||
done(); | ||
} | ||
_flush(done) { | ||
this.finishCb = done; | ||
this.messageHasher.end(); | ||
} | ||
} | ||
module.exports = { dkimBody, BodyHashStream }; |
{ | ||
"name": "mailauth", | ||
"version": "4.7.0", | ||
"version": "4.7.1", | ||
"description": "Email authentication library for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/mailauth.js", |
Sorry, the diff of this file is not supported yet
290693
5641