-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (61 loc) · 1.68 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
'use strict';
const lighthouse = require('lighthouse');
const log = require('lighthouse-logger');
const chromium = require('chrome-aws-lambda');
exports.handler = async (event) => {
let response = null;
let browser = null;
let body = null;
log.setLevel('error');
// Parse the JSON Body.
if (event.body) {
body = JSON.parse(event.body);
}
try {
// Instantiate our browser.
browser = await chromium.puppeteer.launch({
args: [...chromium.args, '--remote-debugging-port=9222'],
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
// Lighthouse options.
const options = {
output: 'html',
preset: body.preset || 'mobile',
onlyCategories: body.onlyCategories || ['performance', 'seo', 'accessibility', 'best-practices'],
port: 9222,
}
// Generate the report.
const report = await lighthouse(body.url, options);
// Only return what we need, otherwise we might exceed the max response size of a Lambda.
let responseBody = {
json: report.lhr,
html: report.report
};
response = {
statusCode: 200,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(responseBody)
};
} catch (error) {
console.log(error);
response = {
statusCode: 500,
headers: {
"Content-Type": "application/json"
},
body: error.message
}
} finally {
// Destroy our browser.
if (browser !== null) {
await browser.close();
}
}
console.log("response: " + JSON.stringify(response))
return response;
};