2026-02-14 09:53:14 +01:00
|
|
|
import crypto from 'node:crypto';
|
|
|
|
|
import stream from 'node:stream';
|
2025-08-13 18:38:56 +05:30
|
|
|
|
2026-02-14 09:53:14 +01:00
|
|
|
const TransformStream = stream.Transform;
|
2025-08-13 18:38:56 +05:30
|
|
|
|
|
|
|
|
class HashStream extends TransformStream {
|
|
|
|
|
#hasher;
|
|
|
|
|
#digest;
|
|
|
|
|
#size;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.#hasher = crypto.createHash('sha256');
|
|
|
|
|
this.#digest = null;
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
digest() {
|
|
|
|
|
return this.#digest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size() {
|
|
|
|
|
return this.#size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_transform(chunk, encoding, callback) {
|
|
|
|
|
this.#hasher.update(chunk);
|
|
|
|
|
this.#size += chunk.length;
|
|
|
|
|
callback(null, chunk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_flush(callback) {
|
|
|
|
|
this.#digest = this.#hasher.digest('hex');
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 09:53:14 +01:00
|
|
|
export default HashStream;
|