-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
149 lines (130 loc) · 3.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
////////////////////////////////////////////
// //
// Author : Dimitri DERTHE //
// //
////////////////////////////////////////////
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
const puppeteer = require('puppeteer');
const md5 = require('md5');
//If set to true, take a screenshot and save the HAR trace
var getTraces = process.env.TRACES;
if(getTraces){
const PuppeteerHar = require('puppeteer-har');
}
// Creates a client
const storage = new Storage();
var bucketsList = [];
let page;
//Google Cloud Functions Webcheck
exports.webcheck = async (req, res) => {
//Get URL to test
const url = req.query.url;
//Generate an UUID for the url
const uuid = md5(url);
//Generate a timestamp
const timestamp = Date.now();
//Declare the path where to store the screenshot and HAR files.
if(getTraces){
const img = '/tmp/'+ uuid + '_' + timestamp + '.png';
const harFile = '/tmp/' + uuid + '_' + timestamp + '.har';
}
//Check if the url parameter is set
if (!url) {
return res.send('Please provide URL as GET parameter, for example: <a href="?url=https://example.com">?url=https://example.com</a>');
}
if (!page) {
page = await getBrowserPage();
}
//Define the resolution screen to simulate
await page.setViewport({
width: 1920,
height: 1080
})
//Start HAR trace
if(getTraces){
const har = new PuppeteerHar(page);
await har.start({ path: harFile });
}
//Start navigation
await page.goto(url);
const performanceTiming = JSON.parse(
await page.evaluate(() => JSON.stringify(window.performance.timing))
)
//Stop HAR trace
if(getTraces){
await har.stop();
}
//Take a screenshot
if(getTraces){
await page.screenshot({
path: img
})
}
//Upload data to Google Cloud Storage
if(getTraces){
toStorage(uuid, [harFile,img]);
}
//Send performance timing to the client
res.send(performanceTiming);
};
async function getBrowserPage() {
// Launch headless Chrome. Turn off sandbox so Chrome can run under root.
const browser = await puppeteer.launch({args: ['--no-sandbox'],ignoreHTTPSErrors: true});
return browser.newPage();
}
//Google Storage upload function
async function uploadFile(bucketName,datas){
// Uploads files to bucket
datas.forEach(fileName => {
storage
.bucket(bucketName)
.upload(fileName, {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
},
})
.then(() => {
console.log(`${fileName} uploaded to ${bucketName}.`);
})
.catch(err => {
console.error('ERROR:', err);
});
});
}
//Google Storage check buckets function
function toStorage(bucketName,datas){
//List buckets
storage
.getBuckets()
.then(results => {
const buckets = results[0];
//List all buckets
console.log('Buckets:');
buckets.forEach(bucket => {
console.log(bucket.name);
bucketsList.push(bucket.name);
});
//Check if bucket don't exist then create it
if (bucketsList.indexOf(bucketName) == -1){
storage
.createBucket(bucketName)
.then(() => {
console.log(`Bucket ${bucketName} created.`);
uploadFile(bucketName,datas);
})
.catch(err => {
console.error('ERROR:', err);
});
}
else{
//Bucket exist then upload data
console.log(`Bucket ${bucketName} already exist.`);
uploadFile(bucketName,datas);
}
})
.catch(err => {
console.error('ERROR:', err);
});
}