-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (105 loc) · 3.46 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
/* global document */
const express = require('express');
const functions = require('firebase-functions');
// const mime = require('mime');
const puppeteer = require('puppeteer');
const PostsHandler = require('./posts-handler');
let posts = new PostsHandler();
const app = express();
app.use(function cors(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
// res.header('Content-Type', 'application/json;charset=utf-8');
// res.header('Cache-Control', 'private, max-age=300');
next();
});
// Init code that gets run before all request handlers.
app.all('*', async (req, res, next) => {
res.locals.browser = await puppeteer.launch({args: ['--no-sandbox']});
next(); // pass control on to router.
});
app.get('/render', async function renderHandler(req, res) {
const url = req.query.url;
if (!url) {
return res.status(400).send(
'Please provide a URL. Example: ?url=https://example.com');
}
const browser = res.locals.browser;
try {
const page = await browser.newPage();
const response = await page.goto(url, {waitUntil: 'networkidle2'});
// Inject <base> on page to relative resources load properly.
await page.evaluate(url => {
const base = document.createElement('base');
base.href = url;
document.head.prepend(base); // Add to top of head, before all other resources.
}, url);
// Remove scripts and html imports. They've already executed.
await page.evaluate(() => {
const elements = document.querySelectorAll('script, link[rel="import"]');
elements.forEach(e => e.remove());
});
const html = await page.content();
// await page.close();
res.status(response.status).send(html);
} catch (e) {
res.status(500).send(e.toString());
}
await browser.close();
});
app.get('/screenshot', async function screenshotHandler(req, res) {
const url = req.query.url;
if (!url) {
return res.status(400).send(
'Please provide a URL. Example: ?url=https://example.com');
}
const viewport = {
width: 1280,
height: 1024,
deviceScaleFactor: 1
};
let fullPage = false;
const size = req.query.size;
if (size) {
const [width, height] = size.split(',').map(item => Number(item));
if (!(isFinite(width) && isFinite(height))) {
return res.status(400).send('Malformed size parameter. Example: ?size=800,600');
}
viewport.width = width;
viewport.height = height;
} else {
fullPage = true;
}
const browser = res.locals.browser;
try {
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle2'});
const opts = {
fullPage,
clip: {
x: 0,
y: 0,
width: viewport.width,
height: viewport.height
}
};
if (fullPage) {
delete opts.clip;
}
const buffer = await page.screenshot(opts);
res.type('image/png').send(buffer);
} catch (e) {
res.status(500).send(e.toString());
}
await browser.close();
});
app.get('/posts', posts.getPosts);
app.get('/version', async function versionHandler(req, res) {
const browser = res.locals.browser;
res.status(200).send(await browser.version());
await browser.close();
});
const beefyOpts = {memory: '2GB', timeoutSeconds: 60};
exports.screenshot = functions.runWith(beefyOpts).https.onRequest(app);
exports.render = functions.runWith(beefyOpts).https.onRequest(app);
exports.version = functions.https.onRequest(app);
exports.posts = functions.runWith(beefyOpts).https.onRequest(app);