Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
util: added checksum-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Feb 14, 2017
1 parent abbcc02 commit 7d23d1d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/util/checksum-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var crypto = require('crypto')
var through = require('mississippi').through

module.exports = checksumStream
function checksumStream (digest, algorithm) {
var hash = crypto.createHash(algorithm || 'sha1')
var stream = through(function (chunk, enc, cb) {
hash.update(chunk, enc)
cb(null, chunk, enc)
}, function (cb) {
var streamDigest = hash.digest('hex')
if (digest && streamDigest !== digest) {
var err = new Error('checksum failed')
err.code = 'EBADCHECKSUM'
err.expected = digest
err.found = streamDigest
return cb(err)
} else {
stream.emit('digest', streamDigest)
cb()
}
})
return stream
}

0 comments on commit 7d23d1d

Please sign in to comment.