This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
routes.js
194 lines (177 loc) · 5.37 KB
/
routes.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const filesize = require('filesize');
const fs = require('fs');
const URL = require('url');
global.Intl = require('intl');
const pageSpeedInsights = require('./pagespeed.js');
const mobileFriendlyTest = require('./mobilefriendly.js');
const screenshot = require('./screenshot.js');
const routes = {
getIndex(req, res) {
return res.render('index');
},
getPageSpeedRaw(req, res) {
const query = req.query;
return pageSpeedInsights.run(query)
.then((raw) => {
return res.send(raw);
})
.catch((err) => {
res.status(500).render('error', {error: err.stack});
});
},
getPageSpeedProcessed(req, res) {
const query = req.query;
return pageSpeedInsights.run(query)
.then(pageSpeedInsights.format)
.then(pageSpeedInsights.determineResourceTypes)
.then(pageSpeedInsights.beautifyResources)
.then((insights) => {
return res.send(insights);
})
.catch((err) => {
res.status(500).render('error', {error: err.stack});
});
},
getPageSpeedReport(req, res) {
const query = req.query;
return pageSpeedInsights.run(query)
.then(pageSpeedInsights.format)
.then(pageSpeedInsights.determineResourceTypes)
.then(pageSpeedInsights.beautifyResources)
.then((insights) => {
res.render('report', {insights: insights});
})
.catch((err) => {
res.status(500).render('error', {error: err.stack});
});
},
getPageSpeedSlides(req, res) {
const query = req.query;
return Promise.all([
pageSpeedInsights.run(query)
.then(pageSpeedInsights.format)
.then(pageSpeedInsights.determineResourceTypes)
.then(pageSpeedInsights.beautifyResources)
.then(pageSpeedInsights.getWaterfall),
mobileFriendlyTest.run(query),
])
.then((results) => {
let insights = results[0];
insights.mobileFriendly = results[1];
res.render('dynamic', {
insights: insights,
filesize: filesize,
URL: URL,
encoded: req.app.locals.encoded,
__dirname: __dirname,
});
})
.catch((err) => {
res.status(500).render('error', {error: err.stack});
});
},
getScreenshot(req, res) {
// url
const parsedUrl = URL.parse(req.query.url);
if (!parsedUrl.host) {
return res.status(400).send('Invalid URL.');
}
const url = req.query.url;
// format
const supportedFormats = {
png: true,
jpeg: true,
jpg: true,
gif: true,
pdf: true,
};
const format = req.query.format ? req.query.format.toLowerCase() : 'pdf';
if (!supportedFormats[format]) {
return res.status(400).send('Unsupported format, use one of ' +
Object.keys(supportedFormats).join(', ') + '.');
}
// view port
let viewPort = req.query.viewPort ? req.query.viewPort : false;
if (viewPort) {
if (!/^\d+x\d+$/i.test(viewPort)) {
return res.status(400).send('Invalid view port, use, e.g., 800x600.');
}
viewPort = {
width: parseInt(viewPort.split(/x/i)[0], 10),
height: parseInt(viewPort.split(/x/i)[1], 10),
};
}
// page size
const supportedPageSizes = {
Letter: true,
Legal: true,
A3: true,
A4: true,
A5: true,
Tabloid: true,
};
const pageSize = req.query.pageSize;
if (pageSize) {
if (!supportedPageSizes[pageSize]) {
return res.status(400).send('Unsupported page size, use one of ' +
Object.keys(supportedPageSizes).join(', ') + '.');
}
}
// page orientation
const pageOrientation = req.query.pageOrientation || false;
if (pageOrientation) {
if (pageOrientation !== 'landscape' && pageOrientation !== 'portrait') {
return res.status(400).send('Unsupported page orientation, use ' +
'portrait or landscape.');
}
}
// page margin in cm
const pageMarginInCm = req.query.pageMarginInCm;
if (pageMarginInCm) {
if (!/^\d+(?:\.\d+)?$/.test(pageMarginInCm)) {
return res.status(400).send('Unsupported page margins, use integer ' +
'or floating point number.');
}
}
// get screenshot
screenshot.getScreenshot(url, {
format: format,
viewPort: viewPort,
pageSize: pageSize,
pageOrientation: pageOrientation,
pageMarginInCm: pageMarginInCm,
})
.then(function(fileName) {
const options = {
root: __dirname,
};
return res.sendFile(fileName, options, function(err) {
console.log('Deleting ' + fileName);
fs.unlink(fileName);
if (err) {
console.error(err);
return res.status(err.status).end();
}
});
}).catch(function(err) {
return res.status(400).send('Uncaught error ' + err);
});
},
};
module.exports = routes;