Skip to content

Instantly share code, notes, and snippets.

@doesdev
Created February 19, 2019 23:32
Show Gist options
  • Save doesdev/d185200e6977107f00b5f12b29b717b3 to your computer and use it in GitHub Desktop.
Save doesdev/d185200e6977107f00b5f12b29b717b3 to your computer and use it in GitHub Desktop.
Get hash and filesize in stream
const fs = require('fs')
const crypto = require('crypto')
const getHashAndSize = (file, alg = 'sha1') => new Promise((resolve, reject) => {
let size = 0
const hasher = crypto.createHash(alg, { encoding: 'hex' })
const filestream = fs.createReadStream(file)
filestream.pipe(hasher)
filestream.on('data', (d) => { size += d.length })
filestream.on('end', () => {
hasher.end()
const hash = hasher.read()
return resolve({ hash, size })
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment