Skip to content

Commit

Permalink
Compare integrity with ground truth
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Dec 6, 2023
1 parent b49736b commit 6683aae
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (diff.length > 0) {
console.log('The following packages are updated in the Pull Request.');
console.log(diff.map(item => '- ' + item).join('\n'));
console.log('\nThe affected CDN links are listed below.');
main();
printCDNTable();
} else {
console.log('No packages are updated in the Pull Request.');
}
Expand All @@ -44,6 +44,7 @@ async function checkIntegrity(url) {
return new Promise((resolve, reject) => {
const req = https.get(url, response => {
ssri.fromStream(response, { algorithms: ['sha256'] })
.then(integrity => integrity.toString())
.then(resolve);
});
req.on('error', error => {
Expand All @@ -52,13 +53,19 @@ async function checkIntegrity(url) {
});
}

async function format(name, links) {
async function formatTable(name, links, groundTruth) {
let content = '';
for (const [key, url] of Object.entries(links)) {
if (!['jsdelivr', 'unpkg', 'cdnjs'].includes(key)) continue;
const statusCode = await request(url);
const integrity = (statusCode === 200) ? await checkIntegrity(url) : '❌';
content += `| [${key}](${url}) | ${statusCode === 200 ? '✅ 200' : '❌ ' + statusCode} | ${integrity} |\n`;
let integrityMatch;
if (statusCode === 200) {
const integrity = await checkIntegrity(url);
integrityMatch = integrity === groundTruth ? '✅' : '❌';
} else {
integrityMatch = '❌';
}
content += `| [${key}](${url}) | ${statusCode === 200 ? '✅' : '❌'} ${statusCode} | ${integrityMatch} |\n`;
}
console.log(`
## ${name}
Expand All @@ -68,12 +75,15 @@ async function format(name, links) {
${content}`);
}

async function main() {
async function printCDNTable() {
for (const [key, value] of Object.entries(dependencies)) {
const { name, file } = value;
if (!diff.includes(name) || !file) continue;
const version = newPlugins[name];
const links = getVendors({ ...value, version, minified: file });
await format(key, links);
const integrity = await ssri
.fromStream(fs.createReadStream(`./node_modules/${name}/${file}`), { algorithms: ['sha256'] })
.toString();
await formatTable(key, links, integrity);
}
}

0 comments on commit 6683aae

Please sign in to comment.