forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-sri.js
49 lines (45 loc) · 1.67 KB
/
update-sri.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const fs = require("fs");
const path = require("path");
const sriToolbox = require("sri-toolbox");
const version = process.argv[2];
function read(file, encoding) {
return new Promise((resolve, reject) =>
fs.readFile(file, encoding, (err, body) =>
err ? reject(err) : resolve(body)));
}
function write(file, data) {
return new Promise((resolve, reject) =>
fs.writeFile(file, data, (err) =>
err ? reject(err) : resolve()));
}
Promise.all(process.argv.slice(3).map(file =>
read(file, "utf8")
.then(body => {
// 1 - url prefix: "http…/KaTeX/
// 2 - opening quote: "
// 3 - preserved suffix: /katex.min.js" integrity="…"
// 4 - file name: katex.min.js
// 5 - integrity opening quote: "
// 6 - old hash: sha384-…
// 7 - integrity hash algorithm: sha384
const re = /((["'])https?:\/\/cdnjs.cloudflare.com\/ajax\/libs\/KaTeX\/)[^\/"']+(\/([^"']+)\2(?:\s+integrity=(["'])(([^-]+)-[^"']+)\5)?)/g;
const hashes = {};
body = body.replace(re, (m, pre, oq1, post, file, oq2, old, algo) => {
if (old) {
hashes[old] = { file, algo };
}
return pre + version + post;
});
return Promise.all(Object.keys(hashes).map(hash =>
read(path.join("dist", hashes[hash].file), null)
.then(data => {
body = body.replace(hash, sriToolbox.generate({
algorithms: [hashes[hash].algo],
}, data));
})
)).then(() => write(file, body));
})
)).then(() => process.exit(0), err => {
console.error(err.stack);
process.exit(1);
});