-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrendererInit.js
38 lines (31 loc) · 1.19 KB
/
rendererInit.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
const spawn = require('child_process').spawn;
function rendererInit(url, sizeX, sizeY, clientScripts) {
return new Promise((resolve, reject) => {
const output = [];
const startTime = Date.now();
const args = [__dirname + '/renderer.js', url, sizeX, sizeY];
if (clientScripts !== undefined) {
args.push(clientScripts);
}
const start = spawn('casperjs', args);
start.stdout.on('data', data => {
output.push(data);
});
start.stderr.on('data', data => {
console.log(`stderr: ${data}`);
});
start.on('close', code => {
const endTime = Date.now();
const duration = new Date(endTime - startTime).getSeconds();
console.log(`Finished rendering in ${duration} seconds`);
let imageData = JSON.parse(output.join(''));
let bufferData = imageData.map((data) => {
let base64Data = data.split(',')[1];
let buffer = Buffer.from(base64Data, 'base64');
return new Buffer(buffer, 'base64')
});
resolve(bufferData);
});
});
}
module.exports = rendererInit;