-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (66 loc) · 2.17 KB
/
index.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
76
77
78
79
80
81
const chromeLauncher = require("chrome-launcher");
const chromium = require('chrome-aws-lambda');
const lighthouse = require('lighthouse');
async function runLighthouse(req) {
const flags = {
port: undefined,
output: 'json',
logLevel: 'warning'
};
const defaultFlags = [
'--headless',
'--disable-dev-shm-usage',
'--no-sandbox',
'--single-process'
];
const url = req.url;
const lhConfig = {extends: 'lighthouse:default'};
const launcherOptions = {
chromeFlags: defaultFlags
};
//Only set the chromePath if actually executing in a lambda
if (process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV) {
launcherOptions['chromePath'] = await chromium.executablePath;
}
console.log('Generating report for', url)
console.time('Lighthouse-' + url);
const chrome = await chromeLauncher.launch({
"chromeFlags":[
"--headless",
"--disable-dev-shm-usage",
"--no-sandbox",
"--single-process"
],
"chromePath":"/tmp/chromium"
});
const runnerResult = await lighthouse(url, {
logLevel: 'warning',
output: 'json',
port: chrome.port
});
await chrome.kill();
console.log('Report is done for', runnerResult.lhr.finalUrl);
const rtn = {}
for (const [key, value] of Object.entries(runnerResult.lhr.audits)) {
delete value.id;
delete value.title;
delete value.description;
if(key === 'full-page-screenshot' && value.details && value.details.nodes) {
delete value.details.nodes;
}
rtn[key] = value;
}
rtn['category-scores'] = {};
rtn['total-score'] = 0;
for (const [key, value] of Object.entries(runnerResult.lhr.categories)) {
rtn['category-scores'][key] = value.score;
rtn['total-score'] += value.score;
}
rtn['total-score'] = rtn['total-score'] / Object.keys(runnerResult.lhr.categories).length;
console.timeEnd('Lighthouse-' + url);
console.log('Lighthouse result end');
return rtn;
}
module.exports = {
runLighthouse: runLighthouse
};