Created
February 19, 2019 23:32
-
-
Save doesdev/d185200e6977107f00b5f12b29b717b3 to your computer and use it in GitHub Desktop.
Get hash and filesize in stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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