-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
109 lines (94 loc) · 3.49 KB
/
server.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
// Copyright 2018 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// server.js
// where your node app starts
// init project
const express = require( 'express' );
var app = express();
const fs = require( 'fs' );
const lighthouse = require( 'lighthouse' );
const puppeteer = require('puppeteer')
const axios = require( 'axios' );
app.get( '/', async ( req, res ) => {
try {
let target = req.headers.referer;
let responseTarget = req.query.response;
let demoName = req.query.demo;
if ( ! target || '' === target || ! responseTarget || '' === responseTarget ) {
res.status( 403 ).send( 'No domain passed on.' );
return;
}
const chromeFlags = ['--headless', '--disable-dev-shm-usag', '--disable-gpu', '--disable-extensions', '--disable-popup-blocking', '--no-sandbox', '--single-process' ];
res.send( 'Domain is being analysed.' );
console.log( 'Launching Browser' );
const chrome = await puppeteer.launch( { args: ['--no-sandbox'] } ).catch( error => { console.error( 'Chrome failed...', error ); } );
const options = {
logLevel: 'info',
disableStorageReset: true,
maxWaitForLoad: 60000,
chromeFlags: chromeFlags,
onlyAudits: [
'first-contentful-paint',
'largest-contentful-paint',
'max-potential-fid',
'cumulative-layout-shift',
],
port: ( new URL( chrome.wsEndpoint() ) ).port,
};
const config = {
extends: 'lighthouse:default',
settings: {
onlyAudits: [
'first-contentful-paint',
'largest-contentful-paint',
'max-potential-fid',
'cumulative-layout-shift',
],
},
};
console.log( 'Launching Lighthouse' );
const runnerResult = await lighthouse( target, options, config ).catch( error => { console.error( 'Lighthouse failed...', error ); } );
let data = {
fcp : {
score: runnerResult.lhr.audits['first-contentful-paint'].score,
display: runnerResult.lhr.audits['first-contentful-paint'].displayValue,
},
lcp: {
score: runnerResult.lhr.audits['largest-contentful-paint'].score,
display: runnerResult.lhr.audits['largest-contentful-paint'].displayValue,
},
fid: {
score: runnerResult.lhr.audits['max-potential-fid'].score,
display: runnerResult.lhr.audits['max-potential-fid'].displayValue,
},
cls: {
score: runnerResult.lhr.audits['cumulative-layout-shift'].score,
display: runnerResult.lhr.audits['cumulative-layout-shift'].displayValue,
},
score: runnerResult.lhr.categories.performance.score,
demo_name: demoName,
}
await chrome.close().catch( error => { console.error( 'Closing chrome failed...', error ); } );
axios.post( responseTarget, data ).then( ( res ) => {
return;
} ).catch( ( error ) => {
console.error( error )
} );
return;
} catch ( e ) {
if ( 'undefined' !== typeof chrome ) {
await chrome.close().catch( error => { console.error( 'Closing chrome failed...', error ); } );
}
console.log( e );
} finally {
if ( 'undefined' !== typeof chrome ) {
await chrome.close().catch( error => { console.error( 'Closing chrome failed...', error ); } );
}
}
} );
// listen for requests :)
let port = process.env.PORT;
// let port = 64291;
var listener = app.listen( port, function() {
console.log( 'Your app is listening on port ' + listener.address().port );
} );