The crypto hash.update method also accepts a buffer, so there is no need to do a crawl through FileReader . Just do
fetch(url).then(function(response) { return response.arrayBuffer(); }).then(function(arrayBuffer) { var buffer = require('buffer')(new Uint8Array(arrayBuffer)); var hash = require('crypto').createHash(hashType); hash.update(buffer, 'binary'); return hash.digest('hex'); })
If this does not work, you can easily promise FileReader :
function getResult(reader) { return new Promise(function(resolve, reject) { reader.onload = function() { resolve(this.result); }; reader.onerror = reader.onabort = reject; }); }
and use it as follows:
fetch(url).then(function(response) { return response.blob(); }).then(function(data) { var a = new FileReader(); a.readAsBinaryString(data); return getResult(a); }).then(function(result) { var hash = crypto.createHash(hashType); hash.update(result, 'binary'); return hash.digest('hex'); })
source share