import crypto from 'node:crypto';
import { Transform } from 'node:stream';
/**
* A pass-through stream that records the checksum and byte size of what flows through it on the
* given object once the stream ends. Throws when the hash algorithm is not available. With `algo`
* set to `false` nothing is hashed and only the size is recorded.
*/
class StreamHash extends Transform {
constructor(attachment, algo) {
super();
this.attachment = attachment;
this.algo = algo === false ? false : (algo || 'md5').toLowerCase();
this.hash = this.algo === false ? null : crypto.createHash(this.algo);
this.byteCount = 0;
}
_transform(chunk, encoding, done) {
this.hash?.update(chunk);
this.byteCount += chunk.length;
done(null, chunk);
}
_flush(done) {
if (this.hash) {
this.attachment.checksum = this.hash.digest('hex');
}
this.attachment.size = this.byteCount;
done();
}
}
export default StreamHash;