forked from AzureAD/microsoft-authentication-library-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdn-sri.js
75 lines (60 loc) · 2.73 KB
/
cdn-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const ssri = require("ssri");
const fs = require("fs");
const { version } = require("./package.json");
const readmeMdFilePath = "./README.md";
const cdnUsageFilePath = "./docs/cdn-usage.md";
const sriTableMarker = "<!-- SRI_TABLE_START -->";
const sriTableMarkerOffset = 3;
const latestVersionMarker = "<!-- CDN_LATEST -->";
const latestVersionMarkerOffset = 2;
const latestVersionString = `<script type="text/javascript" src="https://alcdn.msauth.net/browser/${version}/js/msal-browser.min.js"></script>`;
async function generateSris() {
// Read contents of README md file
const readmeMd = await fs.promises.readFile(readmeMdFilePath, "utf8");
const readMeMdLines = readmeMd.toString().split(/\r?\n/);
// Update REAMDE
const readmeInsertLineIndex = readMeMdLines.indexOf(latestVersionMarker);
const readMeInsertIndex = readmeInsertLineIndex + latestVersionMarkerOffset;
readMeMdLines.splice(readMeInsertIndex, 1, latestVersionString);
// Write new README to disk
const newReadMd = readMeMdLines.join("\n");
await fs.promises.writeFile(readmeMdFilePath, newReadMd);
// Read contents of cdn md file
const cdnMd = await fs.promises.readFile(cdnUsageFilePath, "utf8");
const cdnMdLines = cdnMd.toString().split(/\r?\n/);
// Update basic usage
const latestVersionInsertLineIndex = cdnMdLines.indexOf(latestVersionMarker);
const latestVersionInsertIndex = latestVersionInsertLineIndex + latestVersionMarkerOffset;
cdnMdLines.splice(latestVersionInsertIndex, 1, latestVersionString);
// Generate entries for each file
const unminifiedVersionSri = await generateSriTableEntry("msal-browser.js", " ");
const minifiedVersionSri = await generateSriTableEntry("msal-browser.min.js", "");
// Add entries to table
const sriInsertLineIndex = cdnMdLines.indexOf(sriTableMarker);
const tableInsertIndex = sriInsertLineIndex + sriTableMarkerOffset;
cdnMdLines.splice(tableInsertIndex, 0, minifiedVersionSri);
cdnMdLines.splice(tableInsertIndex, 0, unminifiedVersionSri);
// Write new file to disk
const newCdnMd = cdnMdLines.join("\n");
await fs.promises.writeFile(cdnUsageFilePath, newCdnMd);
}
async function generateSriTableEntry(file, padding) {
const filePath = `./lib/${file}`;
const fileStream = fs.createReadStream(filePath);
const sri = await ssri.fromStream(fileStream, {
algorithms: [ "sha384" ]
});
return `${version} | ${file}${padding} | \`${sri.toString()}\``;
}
generateSris()
.then(() => {
process.exit(0);
})
.catch(error => {
console.error(error);
process.exit(1);
});