This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscreenshot.js
More file actions
112 lines (107 loc) · 3.79 KB
/
screenshot.js
File metadata and controls
112 lines (107 loc) · 3.79 KB
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
"use strict";
// Environment variables ///////////////////////////////////////////////////////
require('dotenv').config({
path: ".env"
})
const argv = require('yargs')
.option('port', {
describe: "Port to expose screenshot server endpoint",
demandOption: true,
type: "number",
default: 9031
}).help()
.argv;
const http = require('http');
const process = require('process');
const puppeteer = require('puppeteer');
const server = http.createServer();
const config = require('./config');
const { logger } = require('./units/logger');
const util = require('util');
const hasha = require('hasha');
const request = require('request');
const localconfig = require('./localconfig');
const nodeurl = require('url');
const sharp = require('sharp');
const launchOptions = {
headless: true,
ignoreHTTPSErrors: true,
devtools: false,
args: [
'--disable-web-security',
'--disable-features=IsolateOrigins',
'--disable-site-isolation-trials'
]
};
(async () => {
const payloadToReturn = {};
// create browser and keep it open
let browser = await puppeteer.launch(launchOptions);
process.on('SIGQUIT', async () => {
await browser.close();
logger.info('Now I will exit because of SIGQUIT');
process.exit(0);
});
server.on('request', async (req, res) => {
logger.trace(`${new Date().toISOString()} - ${req.method} ${req.url}`);
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', async () => {
res.setHeader('Content-Type','text/plain');
try {
if (browser === undefined) {
browser = await puppeteer.launch(launchOptions);
}
let page = await browser.newPage();
body = Buffer.concat(body).toString();
// body completed
let jsonBody = JSON.parse(body);
logger.trace(jsonBody);
// visit map page with internal url
let url = util.format('%s%s', localconfig.internalUrl, jsonBody.mapargs);
logger.debug(url);
// pass hidecontrols but not save it
await page.goto(
util.format("%s%s", url, '&noControls=1'), {
"waitUntil": ["load","domcontentloaded","networkidle0"],
"timeout": 0
}
);
// Wait until loader disappear from map
await page.waitForFunction(async () => {
if (!jQuery) return false;
return !jQuery(".massive.loader").is(":visible");
});
// within milliseconds below or drop
await page.waitForTimeout(15000);
const options = {
type: "png",
encoding: "binary"
};
// use path instead of full url to be protocol agnostic
let urlob = nodeurl.parse(url);
let scrBuf = await page.screenshot(options);
logger.debug(scrBuf);
payloadToReturn.path = util.format(config.screenshot.pathPattern, hasha(urlob.path));
payloadToReturn.type = options.type;
await sharp(scrBuf).png({
progressive: true, // display low-res images before hi-res during loading
compressionLevel: 9 // max compression
}).toFile(payloadToReturn.path);
payloadToReturn.error = false;
logger.debug(payloadToReturn);
await page.close();
} catch (error) {
logger.error(error);
await browser.close();
browser = undefined;
payloadToReturn.error = true;
} finally {
res.end(JSON.stringify(payloadToReturn, null, ''));
}
});
});
server.listen(argv.port);
logger.info('Screenshot server listening on port %d', argv.port);
})();