-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigest.js
More file actions
31 lines (23 loc) · 1.01 KB
/
digest.js
File metadata and controls
31 lines (23 loc) · 1.01 KB
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
30
31
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
var controller = new AbortController();
var timeout = setTimeout(() => controller.abort(), 1000*5);
try {
var response = await fetch(url, { signal: controller.signal });
} finally {
clearTimeout(timeout);
}
// TODO only digest if response is a success (example: 403 with body - https://rubygems.org/downloads/sorbet-static-0.4.5125.gem)
var bytes = response.headers.get('content-length');
var body = await response.text();
if (!bytes) {
bytes = String(Buffer.byteLength(body));
}
var digest = crypto.createHash(algorithm).update(body).digest(encoding);
var sri = `${algorithm}-${digest}`
return {algorithm, encoding, digest, url, bytes, sri}
}
module.exports = calculateDigest;