conductor

CI task system
git clone git://git.finwo.net/app/conductor
Log | Files | Refs | README | LICENSE

stream.js (848B)


      1 // src/lib/stream.js - stream helpers
      2 //
      3 // Note for anyone tempted to hash with a 'data' listener on a PassThrough:
      4 // attaching one switches the stream into flowing mode immediately, so the
      5 // bytes are gone before the real consumer attaches and the upload silently
      6 // stores nothing. A Transform stays paused until something reads it, and
      7 // keeps backpressure intact.
      8 
      9 import crypto from 'node:crypto';
     10 import { Transform } from 'node:stream';
     11 
     12 export function hashingTransform(algorithm = 'sha256') {
     13   const hash = crypto.createHash(algorithm);
     14   let bytes = 0;
     15 
     16   const transform = new Transform({
     17     transform(chunk, encoding, callback) {
     18       hash.update(chunk);
     19       bytes += chunk.length;
     20       callback(null, chunk);
     21     },
     22   });
     23 
     24   transform.digest = () => hash.digest('hex');
     25   transform.bytes = () => bytes;
     26   return transform;
     27 }