-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (100 loc) · 3.27 KB
/
server.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
const puppeteer = require('puppeteer');
const http = require('http');
const fs = require('fs');
const url = require('url');
const config = require('config');
const uuidv1 = require('uuid/v1');
const winston = require('winston');
require('winston-daily-rotate-file');
// Logging
const transport = new (winston.transports.DailyRotateFile)
(config.get('winstonDailyRotateLog'));
const _log = new (winston.Logger)({
transports: [ transport ]
});
http.createServer(function (req, res) {
const requestId = uuidv1();
// Session loggers
const logMsg = msg => `${requestId} ${msg}`;
const log = {
debug: msg => _log.debug(logMsg(msg)),
info: msg => _log.info(logMsg(msg)),
warn: msg => _log.warn(logMsg(msg)),
error: msg => _log.error(logMsg(msg))
};
log.info(`Begin request: ${req.url}`);
const options = url.parse(req.url, true).query;
const targetUrl = decodeURI(options.url);
const windowWidth = +options.ww;
const windowHeight = +options.wh;
const timeout = +options.timeout || config.get('timeout');
const viewPort = {
width: windowWidth || config.get('defaultWidth'),
height: windowHeight || config.get('defaultHeight')
};
const wait = options.wait;
function handleError(e){
log.error(e);
res.writeHead(500, 'Server Error');
res.end();
console.log('Server Error', req.url, e);
}
puppeteer.launch({
headless: false
})
.then(browser => browser.newPage()
.then(page => {
if(wait){
log.debug('Waiting for screenshotTrigger');
return new Promise(function(resolve, reject) {
const waitTimeout = setTimeout(() => {
log.error('Wait timed out');
reject(new Error('Wait timed out'));
}, timeout);
const handleTrigger = event => () => {
log.debug(`Trigger detected: ${event}`);
clearTimeout(waitTimeout);
log.debug('Taking screenshot');
resolve(page.screenshot({
fullPage: true
}));
}
page.exposeFunction('triggerScreenshot', handleTrigger('triggerScreenshot'))
.then(() => page.exposeFunction('callPhantom', handleTrigger('callPhantom')))
.then(() => page.setViewport(viewPort))
.then(() => page.goto(targetUrl))
.catch(handleError);
});
}
else{
log.debug('No waiting branch');
return page.setViewport(viewPort)
.then(() => {
log.debug(`Page goto: ${targetUrl}`);
return page.goto(targetUrl);
})
.then(() => {
log.debug('Taking screenshot');
return page.screenshot({
fullPage: true
});
});
}
})
.then(buffer => {
if(!buffer){
handleError(new Error('Buffer is null'));
return;
}
log.debug('Writing buffer');
res.write(buffer);
res.end();
log.info('Success');
browser.close();
})
.catch(e => {
browser.close();
handleError(e);
}))
.catch(handleError);
}).listen(process.env.PORT || 9876);