-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigest.js
29 lines (23 loc) · 1.03 KB
/
digest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const axios = require('axios');
var crypto = require('crypto');
async function calculateDigest(algorithm, encoding, url) {
algorithm = (typeof algorithm !== 'undefined') ? algorithm : 'sha256'
encoding = (typeof encoding !== 'undefined') ? encoding : 'hex'
// TODO support go modules hashing algo
const download = await axios.get(url, {
timeout: 1000*5,
maxContentLength: 50*1024*1024,
transformResponse: res => res
});
// TODO only digest if response is a success (example: 403 with body - https://rubygems.org/downloads/sorbet-static-0.4.5125.gem)
if (download.headers['content-length']){
var bytes = download.headers['content-length']
} else {
var downloadClone = await download.clone();
var bytes = (await downloadClone.text()).length
}
const digest = crypto.createHash(algorithm).update(download.data).digest(encoding);
const sri = `${algorithm}-${digest}`
return {algorithm, encoding, digest, url, bytes, sri}
}
module.exports = calculateDigest;