-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (47 loc) · 1.97 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
const procStats = require('process-stats')()
const prettyBytes = require('pretty-bytes')
const defaultHeadlessArgs = "--disable-setuid-sandbox --disable-dev-shm-usage --disable-accelerated-2d-canvas --no-first-run --enable-font-antialiasing --font-render-hinting=none --disable-gpu --single-process --no-zygote --no-sandbox --hide-scrollbars"
const args = (process.env.HEADLESS_ARGS || defaultHeadlessArgs).split(' ')
const waitUntil = (process.env.WAIT_UNTIL || "networkidle0").split(' ')
// const browserless = require('browserless')()
const createBrowserless = require('@browserless/pool')
const pool = createBrowserless({
max: process.env.WEB_CONCURRENCY || 2,
timeout: 30000
},
{
ignoreHTTPSErrors: true,
args: args
}
)
process.on('exit', async () => {
await pool.drain()
await pool.clear()
})
const express = require('express')
const PORT = process.env.PORT || 5000
const takeScreenshot = async (url, element = '.screenshot', res) => {
const { cpu, uptime, memUsed} = procStats()
console.log(`screenshot-starts url=${url} element=${element} time=${uptime.pretty} mem=${memUsed.pretty} cpu=${cpu}`)
const buffer = await pool.screenshot(url,
{
waitUntil: waitUntil, device: 'iPhone X', element: element,
viewport: { deviceScaleFactor: 2}
})
res.set('Cache-Control', 'max-age=0, no-cache, no-store, must-revalidate').type('png').end(buffer, 'binary')
console.log(`screenshot-end size=${prettyBytes(buffer.byteLength)} time=${uptime.pretty} mem=${memUsed.pretty} cpu=${cpu}`)
}
express().get('/', async(request, res) => {
const { url, element } = request.query
if (url === undefined) {
return res.status(400).end('Missing url')
}
try {
return await takeScreenshot(url, element, res)
} catch(e) {
console.log('Error: ', e)
} finally {
return res.status(500).end('Screenshot failed')
}
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))