I would like to compute the hash of a resource (e.g., a PDF) from a URL. To this end, I wrote
const computeHash = co.wrap(function* main(url) {
const response = yield promisify(request)(url);
// assume response.status === 200
const buf = new Uint8Array(response.arrayBuffer);
const hash = crypto.createHash('sha1');
hash.update(buf, 'binary');
return hash.digest('hex');
});
to be used
const hash = yield computeHash('http://arxiv.org/pdf/1001.1234v3.pdf');
What I like about the code:
yield it. Just a step away from async/await.What I don't like:
request is completed and the response body as a whole piped into the hash function.
I'd rather pipe the output of request into the hash function.Any hints?
crypto.createHash() provides a Hash instance that currently supports two interfaces: legacy (update() and digest()) and streaming. You don't have to do anything special to use either one, so to stream the response to the hashing stream it's as simple as:
var hasher = crypto.createHash('sha1');
hasher.setEncoding('hex');
request(url).pipe(hasher).on('finish', function() {
console.log('Hash is', hasher.read());
});
That's how you'd do it with normal callbacks, but I am not sure how you'd work yield into that as I'm not familiar enough with generators and the like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With