-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit.mjs
105 lines (97 loc) · 3.35 KB
/
audit.mjs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env zx
/* eslint-disable no-tabs */
import fs from 'node:fs';
import minimist from 'minimist';
/** @type Record<thirdParty|lighthouseTest|PSITests: any> */
const flags = minimist(process.argv.slice(2));
const key = 'AIzaSyCTbtKPZqZbUjNka6sX_Bt6vl9YcR_H9bw';
const availableFlags = ['thirdParty', 'lighthouseTest', 'PSITests', 'url'];
const url = flags.url;
if (process.argv.length < 4) {
console.log(`
Usage: Flags Available. Atleast 1 flag is required.
thirdParty: Print Third Party Domains.
url: URL of the page
lighthouseTest: Run lighthouse test that would update lighthouse-latest.json which is used to compute Third Party Domains.
PSITests: Runs PSI Tests for all the changes 1 by 1.
`)
process.exit(0);
}
Object.keys(flags).forEach(flag => {
if (flag === '_' || flag === '__') {
return;
}
if (!availableFlags.includes(flag)) {
throw new Error('Unknown Flag:' + flag);
}
});
if (flags.lighthouseTest) {
let PSIScore = await $`./node_modules/.bin/lighthouse ${url} --output json --output-path ./lighthouse-latest.json --only-categories="performance" --chrome-flags="--headless" > /dev/null 2>&1 && ./node_modules/node-jq/bin/jq ".categories.performance.score" lighthouse-latest.json`;
console.log('PSI Score is:', PSIScore.stdout);
}
if (flags.thirdParty) {
const report = JSON.parse(fs.readFileSync('lighthouse-latest.json'));
const networkRequests = report.audits['network-requests'].details.items;
const uniqueDomains = new Set();
const pageUrl = new URL(report.finalUrl);
networkRequests.forEach((req) => {
const { url } = req;
const parsedUrl = new URL(url);
if (!parsedUrl.hostname) {
// console.error('Can\'t determine domain for', url);
return;
}
if (pageUrl.hostname === parsedUrl.hostname) {
return;
}
uniqueDomains.add(parsedUrl.hostname);
});
let domains = [];
for (var domain of uniqueDomains.values()) {
domains.push(domain);
}
console.log('Following are the list of Third-Party-Domains present on the page', domains);
}
if (flags.PSITests) {
const promises = [];
for (let i = -1; i <= 11; i++) {
const parsedUrl = new URL(url);
// Keep the first test without Optimizations
if (i > 0) {
parsedUrl.searchParams.set('__optimization', '1');
parsedUrl.searchParams.set('__perframe_only_changes', i);
}
if (i === -1) {
parsedUrl.searchParams.set('__optimization', '1');
}
const urlWithGivenChange = encodeURIComponent(parsedUrl.toString());
(function closureChangeId(changeId) {
const psiTestUrl = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?strategy=mobile&url=${urlWithGivenChange}&key=${key}`;
const reportWrapper = fetch(psiTestUrl)
.then((resp) => {
if (resp.status !== 200) {
throw new Error(`Request failed:${resp.statusText}`);
}
return resp.json();
}).then((psiResult) => ({
psiResult,
url: urlWithGivenChange,
changeId
}));
promises.push(reportWrapper);
}(i));
}
const auditReport = {
changes: {}
};
Promise.all(promises).then((reportsWrappers) => {
reportsWrappers.forEach((reportWrapper) => {
auditReport.changes[reportWrapper.changeId] = {
url: reportWrapper.url,
score: reportWrapper.psiResult.lighthouseResult.categories.performance.score
};
fs.writeFileSync(`./auto-audits/${reportWrapper.changeId}.json`, JSON.stringify(reportWrapper.psiResult.lighthouseResult));
});
console.log(auditReport);
});
}