-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgather-single-telemetry.js
More file actions
45 lines (37 loc) · 1.2 KB
/
gather-single-telemetry.js
File metadata and controls
45 lines (37 loc) · 1.2 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
const puppeteer = require('puppeteer');
const { setTelemetryWithKey } = require('../utils/telemetry');
const DEFAULT_PUPPETEER_ARGS = {
ignoreHTTPSErrors: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
};
module.exports = async function gatherSingleTelemetry(url, options = {}, gatherFn, ...args) {
const browser = await puppeteer.launch(DEFAULT_PUPPETEER_ARGS);
const page = await browser.newPage();
await page.goto(url);
await page.exposeFunction('logErrorInNodeProcess', message => {
console.error(message); // eslint-disable-line no-console
});
const telemetry = await bridgeEvaluate(
page,
async (gFn, ...supportFns) => {
supportFns.forEach(fn => {
this[fn.name] = fn;
});
let telemetry = {};
telemetry = await gFn(...supportFns);
return telemetry;
},
gatherFn,
...args
);
setTelemetryWithKey(options.telemetryKey, telemetry);
await browser.close();
async function bridgeEvaluate(page, fn, ...rawArgs) {
const args = await Promise.all(
rawArgs.map(arg => {
return typeof arg === 'function' ? page.evaluateHandle(`(${arg.toString()})`) : arg;
})
);
return page.evaluate(fn, ...args);
}
};