This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 784
/
main.js
183 lines (160 loc) · 5.33 KB
/
main.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
/*
* 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 assert = require('assert');
const renderer = require('./renderer');
const chromeLauncher = require('chrome-launcher');
const express = require('express');
const fs = require('fs');
const compression = require('compression');
const path = require('path');
const https = require('https');
const app = express();
const cache = require('./cache');
const now = require('performance-now');
const uuidv4 = require('uuid/v4');
// Load config from config.json if it exists.
let config = {};
const configPath = path.resolve(__dirname, '../config.json');
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath));
assert(config instanceof Object);
}
// Only start a cache if configured and not in testing.
if (!module.parent && !!config['cache']) {
app.get('/render/:url(*)', cache.middleware());
app.get('/screenshot/:url(*)', cache.middleware());
// Always clear the cache for now, while things are changing.
cache.clearCache();
}
// Allows the config to be overriden
app.setConfig = (newConfig) => {
const oldConfig = config;
config = newConfig;
config.chrome = oldConfig.chrome;
config.port = oldConfig.port;
};
app.use(compression());
app.use('/node_modules', express.static(path.resolve(__dirname, '../node_modules')));
app.get('/', (request, response) => {
response.sendFile(path.resolve(__dirname, 'index.html'));
});
function isRestricted(url) {
if (!config['renderOnly'])
return false;
for (let i = 0; i < config['renderOnly'].length; i++) {
if (url.startsWith(config['renderOnly'][i])) {
return false;
}
}
return true;
}
// If configured, report action & time to Google Analytics.
function track(action, time) {
if (config['analyticsTrackingId']) {
const postOptions = {
host: 'www.google-analytics.com',
path: '/collect',
method: 'POST'
};
const post = https.request(postOptions);
post.write(`v=1&t=event&ec=render&ea=${action}&ev=${Math.round(time)}&tid=${config['analyticsTrackingId']}&cid=${uuidv4()}`);
post.end();
}
}
app.get('/render/:url(*)', async(request, response) => {
if (isRestricted(request.params.url)) {
response.status(403).send('Render request forbidden, domain excluded');
return;
}
try {
const start = now();
const result = await renderer.serialize(request.params.url, request.query, config);
response.status(result.status).send(result.body);
track('render', now() - start);
} catch (err) {
let message = `Cannot render ${request.params.url}`;
if (err && err.message)
message += ` - "${err.message}"`;
response.status(400).send(message);
}
});
app.get('/screenshot/:url(*)', async(request, response) => {
if (isRestricted(request.params.url)) {
response.status(403).send('Render request forbidden, domain excluded');
return;
}
try {
const start = now();
const result = await renderer.captureScreenshot(request.params.url, request.query, config).catch((err) => console.error(err));
const img = new Buffer(result, 'base64');
response.set({
'Content-Type': 'image/jpeg',
'Content-Length': img.length
});
response.end(img);
track('screenshot', now() - start);
} catch (err) {
let message = `Cannot render ${request.params.url}`;
if (err && err.message)
message += ` - "${err.message}"`;
response.status(400).send(message);
}
});
app.get('/_ah/health', (request, response) => response.send('OK'));
app.get('/_ah/stop', async(request, response) => {
await config.chrome.kill();
response.send('OK');
});
const appPromise = chromeLauncher.launch({
chromeFlags: ['--headless', '--disable-gpu', '--remote-debugging-address=0.0.0.0'],
port: 0
}).then((chrome) => {
console.log('Chrome launched with debugging on port', chrome.port);
config.chrome = chrome;
config.port = chrome.port;
// Don't open a port when running from inside a module (eg. tests). Importing
// module can control this.
const port = process.env.PORT || '3000';
if (!module.parent) {
app.listen(port, function() {
console.log('Listening on port', port);
});
}
return app;
}).catch((error) => {
console.error(error);
// Critical failure, exit with error code.
process.exit(1);
});
let exceptionCount = 0;
async function logUncaughtError(error) {
console.error('Uncaught exception');
console.error(error);
exceptionCount++;
// Restart instance due to several failures.
if (exceptionCount > 5) {
console.log(`Detected ${exceptionCount} errors, shutting instance down`);
if (config && config.chrome)
await config.chrome.kill();
process.exit(1);
}
}
if (!module.parent) {
process.on('uncaughtException', logUncaughtError);
process.on('unhandledRejection', logUncaughtError);
}
module.exports = appPromise;