-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.js
More file actions
39 lines (31 loc) · 1.12 KB
/
handler.js
File metadata and controls
39 lines (31 loc) · 1.12 KB
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
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();
module.exports.main = (event, context, callback) => {
const url = event.queryStringParameters.url;
const key = extractHostname(url) + `/test_${new Date().getTime()}.html`
// So the request doesn't hang we fire another lambda to work in the background
lambda.invoke({
FunctionName: process.env.LIGHTHOUSE_FUNC,
InvocationType: "Event",
Payload: JSON.stringify({url: url, key: key})
}, (error) => console.log(error));
// The url of the eventual report is returned.
callback(null, {
statusCode: 200,
body: JSON.stringify({ key: key, url: process.env.REPORT_BUCKET_URL + key, message: 'Your report will be live in ~60 seconds'})
});
};
/**
* Get the hostname of a url
* @param {String} url
*/
const extractHostname = (url) => {
let hostname;
// Find & remove protocol (http,https) and get hostname
url.indexOf("//") > -1 ? hostname = url.split('/')[2] : hostname = url.split('/')[0];
// Find & remove port number
hostname = hostname.split(':')[0];
// Find & remove "?"
hostname = hostname.split('?')[0];
return hostname;
}