-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (103 loc) · 3.23 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//const log = require('lighthouse-logger');
const lighthouse = require('lighthouse');
const fetch = require('node-fetch');
const _ = require('lodash');
const chromeLauncher = require('chrome-launcher');
let chromePath = undefined;
// This lets us support invoke local
if (!process.env.IS_LOCAL) {
chromePath = '/opt/bin/chromium';
// https://github.com/alixaxel/chrome-aws-lambda/blob/3779715fdc197a245af662725977133b2d676bf9/source/index.js#L6
// required for node10 support - makes sure fonts and shared libraries are loaded correctly
if (process.env.FONTCONFIG_PATH === undefined) {
process.env.FONTCONFIG_PATH = '/opt/lib';
}
if (
process.env.LD_LIBRARY_PATH &&
process.env.LD_LIBRARY_PATH.startsWith('/opt/lib:') !== true
) {
process.env.LD_LIBRARY_PATH = [
...new Set(['/opt/lib', ...process.env.LD_LIBRARY_PATH.split(':')]),
].join(':');
}
}
const {
CHROME_FLAGS,
ONLY_CATEGORIES
} = process.env;
let chromeFlags = CHROME_FLAGS.split(',');
let lighthouseFlags = {
output: "json",
disableDeviceEmulation: false,
disableStorageReset: false,
throttlingMethod: "simulate",
};
let lighthouseConfig = {
extends: 'lighthouse:default',
settings: {
onlyCategories: ONLY_CATEGORIES.split(','),
}
};
exports.handler = async (event) => {
console.log("BEGIN LAMBDA");
console.log(event.body);
let params = {};
params.cmd = JSON.parse(event.body);
const {
url,
webhook,
testId,
onlyChromeFlags,
onlyCategories,
} = _.get(params, "cmd");
await checkJsonRequestForCustomProcessingOnLighthouse(onlyChromeFlags, onlyCategories);
const results = await launchChromeAndRunLighthouse(url);
const payload = {
url,
webhook,
testId,
testType: "lighthouse",
results
};
if (webhook) {
console.log(`Sending payload to ${webhook}`);
fetch(webhook, { method: 'POST', body: JSON.stringify(payload) });
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ message: `sent payload to ${webhook}` })
};
} else {
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(payload)
};
}
}
async function checkJsonRequestForCustomProcessingOnLighthouse(onlyChromeFlags, onlyCategories, onlyAudits) {
console.log("Check Json Request For Custom Processing On Lighthouse");
if (onlyChromeFlags) {
chromeFlags = onlyChromeFlags;
}
if (onlyCategories) {
lighthouseConfig.settings.onlyCategories = onlyCategories;
}
}
async function launchChromeAndRunLighthouse(url) {
console.log("Launch Chrome And Run Lighthouse");
try {
const chrome = await chromeLauncher.launch({ chromeFlags, chromePath });
lighthouseFlags.port = chrome.port;
const results = await lighthouse(url, lighthouseFlags, lighthouseConfig);
await chrome.kill();
return results;
} catch (error) {
console.error(error);
throw error;
}
}