I would like to use the Fetch API in a browser extension to download a resource and compute a hash thereof. The following works (using crypto through Browserify)
fetch(url).then(function(response) {
return response.blob();
}).then(function(data) {
var a = new FileReader();
a.readAsBinaryString(data);
a.onloadend = function() {
var hash = crypto.createHash(hashType);
hash.update(a.result, 'binary');
return hash.digest('hex');
};
})
but has the disadvantage that I have to wait for a.onloadend
while the context in which I'd like to embed it requires a Promise
to be returned. Also, it seems quite weird to first fetch the entire blob, then read it into a FileReader
just to dump it into createHash
afterwards.
Any hints?