-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (63 loc) · 1.6 KB
/
app.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
const capture = require("./capture");
const OUTPUT_FORMATS = ["jpeg", "pdf", "png"];
const createErrorResponse = (statusCode, error) => ({
statusCode,
headers: {
"content-type": "application/json",
},
body: JSON.stringify({ error }),
});
const handler = async (event) => {
const url = event.url;
const exclude = event.exclude || [];
const format = event.format || OUTPUT_FORMATS[0];
const fullpage = !!event.fullpage;
const selector = event.selector;
const width = parseInt(event.width || 1920);
const height = parseInt(event.height || 1080);
if (!url) {
return createErrorResponse(422, "URL field is required");
}
if (!OUTPUT_FORMATS.includes(format)) {
return createErrorResponse(
422,
"Format field value must be one of " + OUTPUT_FORMATS.join(","),
);
}
if (
format !== "pdf" &&
(isNaN(width) || isNaN(height) || width <= 0 || height <= 0)
) {
return createErrorResponse(
422,
"Field width and height must be valid dimensions",
);
}
try {
const buffer = await capture({
url,
exclude,
format,
fullpage,
selector,
width,
height,
});
return {
statusCode: 200,
headers: {
"content-type":
format === "pdf" ? "application/pdf" : `image/${format}`,
"content-length": buffer.length,
},
body: buffer.toString("base64"),
isBase64Encoded: true,
};
} catch (err) {
return createErrorResponse(
500,
err.message || "Error encountered while capturing screenshot.",
);
}
};
module.exports = { handler };