-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
168 lines (144 loc) · 4.5 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const puppeteer = require('puppeteer');
const apng = require('node-apng');
const jimp = require('jimp');
module.exports = function (setupOrUrl, method = 'screencast') {
const setup = typeof setupOrUrl === 'string' ? (start, stop) => doBasicSetup(setupOrUrl, start, stop) : setupOrUrl;
switch (method) {
case 'screencast': {
return recordUsingScreencast(setup);
}
case 'screenshot': {
return recordUsingScreenshot(setup);
}
case 'trace': {
return recordUsingTrace(setup);
}
}
throw new Error(`Invalid method "${method}"! Expected screencast, screenshot or trace.`);
}
async function doBasicSetup(url, start, stop) {
const browser = await puppeteer.launch();
const [page] = await browser.pages();
await page.goto(url);
await start(page);
await page.waitFor(5000);
await stop();
await browser.close();
}
async function recordUsingScreencast(setup) {
let buffers;
let cuts;
let timestamp;
let session;
let resolve;
// TODO: Hook up the reject using try-catch blocks in start and stop
let reject;
const deffered = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
async function start(page) {
// Clear the buffers and cuts and reset the timestamp from the previous recording
buffers = [];
cuts = [];
timestamp = Date.now();
session = await page.target().createCDPSession();
await session.send('Page.startScreencast');
session.on('Page.screencastFrame', event => {
const buffer = Buffer.from(event.data, 'base64');
buffers.push(buffer);
cuts.push(Date.now());
});
}
async function stop() {
await session.send('Page.stopScreencast');
// Drop the first frame because it always has wrong dimensions
buffers.shift(0);
cuts.shift(0);
resolve(makeApng(buffers, cuts, timestamp));
}
await setup(start, stop);
return deffered;
}
async function recordUsingScreenshot(setup) {
let buffers;
let cuts;
let timestamp;
let record;
let resolve;
// TODO: Hook up the reject using try-catch blocks in start and stop
let reject;
const deffered = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
async function start(page) {
// Clear the buffers and cuts and reset the timestamp from the previous recording
buffers = [];
cuts = [];
timestamp = Date.now();
record = true;
void function snap() {
if (!record) {
return;
}
page.screenshot()
.then(buffer => {
buffers.push(buffer);
cuts.push(Date.now());
snap();
})
// Ignore target having closed, we just stop collecting then
.catch(error => { });
}()
}
async function stop() {
record = false;
resolve(makeApng(buffers, cuts, timestamp));
}
await setup(start, stop);
return deffered;
}
async function recordUsingTrace(setup) {
let timestamp;
let tracing;
let resolve;
// TODO: Hook up the reject using try-catch blocks in start and stop
let reject;
const deffered = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
async function start(page) {
// Reset the timestamp from the previous recording
timestamp = Date.now();
await page.tracing.start({ screenshots: true });
tracing = page.tracing;
}
async function stop() {
const buffer = await tracing.stop();
const trace = JSON.parse(await String(buffer));
const buffers = [];
const cuts = [];
for (const event of trace.traceEvents) {
if (event.name === 'Screenshot') {
const jpgBuffer = Buffer.from(event.args.snapshot, 'base64');
const image = await jimp.read(jpgBuffer);
const pngBuffer = await image.getBufferAsync('image/png');
buffers.push(pngBuffer);
// TODO: Pull the correct timestamp from the event -> event.ts/event.tts
cuts.push(Date.now());
}
}
// Drop the first frame because it always has wrong dimensions
buffers.shift(0);
cuts.shift(0);
resolve(makeApng(buffers, cuts, timestamp));
}
await setup(start, stop);
return deffered;
}
function makeApng(buffers, cuts, timestamp) {
const delays = cuts.reduce((a, c, i) => { a.push(c - (cuts[i - 1] || timestamp)); return a; }, []);
return apng(buffers, index => ({ numerator: delays[index], denominator: 1000 }));
}