-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
109 lines (92 loc) · 2.88 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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const puppeteer = require("puppeteer");
const app = express();
// Authorization header check middleman
app.use((req, res, next) => {
if (req.headers.authorization !== process.env.AUTH) return res.writeHead(401), res.end();
next();
});
// Use the body-parser Json
app.use(bodyParser.json());
(async () => {
let browser = await puppeteer.launch({
headless: "new",
args: ["--no-sandbox"],
protocolTimeout: 180_000 * 2, // Timeout setting for individual protocol (CDP) calls. (https://pptr.dev/api/puppeteer.browserconnectoptions#properties)
});
browser.on("disconnected", async () => {
browser = await puppeteer.launch({
headless: "new",
args: ["--no-sandbox"],
protocolTimeout: 180_000 * 2, // Timeout setting for individual protocol (CDP) calls. (https://pptr.dev/api/puppeteer.browserconnectoptions#properties)
});
});
app.post("/", async (req, res) => {
// Bring variable outside the try-catch scope so it can be closed at the end
let page;
try {
if (req.headers["content-type"] !== "application/json") return res.writeHead(415);
// Required parameters
if (!req.body.url || !req.body.screenWidth || !req.body.screenHeight) return req.writeHead(400);
page = await browser.newPage();
await page.setJavaScriptEnabled(req.body.enableJavaScript ?? true);
await page.setViewport({
width: req.body.screenWidth,
height: req.body.screenHeight,
deviceScaleFactor: req.body.scale || 1,
});
await page.goto(req.body.url);
// Timeout parameter, end the process early and don't return the image because it's 'taking too long'
let operationsComplete = false;
if (req.body.timeout) {
setTimeout(async () => {
if (!operationsComplete) {
await page.close();
page = null;
if (res.writable) {
res.writeHead(408);
}
if (!res.closed) {
res.end();
}
}
}, req.body.timeout);
}
if (req.body.waitForNetworkIdle && page) {
await page.waitForNetworkIdle();
}
if (req.body.waitForSelector && page) {
await page.waitForSelector(req.body.waitForSelector);
}
operationsComplete = true;
if (!page) return;
let options = {
type: req.body.type || "png",
fullPage: !!req.body.full,
clip: req.body.clip,
omitBackground: !!req.body.omitBackground,
};
if (req.body.quality && req.body.type !== "png") {
options.quality = req.body.quality;
}
const screenshot = await page.screenshot(options);
if (res.writable) {
res.write(screenshot);
}
} catch (err) {
console.error(err);
if (res.writable) {
res.writeHead(500);
}
} finally {
res.end();
if (page) {
// Close the page as it won't be used anymore, catch the error and do nothing with it
page.close().catch(() => {});
}
}
});
})();
app.listen(process.env.PORT);