-
Notifications
You must be signed in to change notification settings - Fork 9
/
trace.js
40 lines (34 loc) · 1.08 KB
/
trace.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
const puppeteer = require("puppeteer-core");
const chrome = require("chrome-aws-lambda");
const fs = require("fs");
module.exports = async (req, res) => {
try {
const url = req.query.url;
const browser = await puppeteer.launch({
args: [...chrome.args, "--hide-scrollbars", "--disable-web-security"],
defaultViewport: chrome.defaultViewport,
executablePath: await chrome.executablePath,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
const trace = `/tmp/trace-${new URL(url).hostname}.json`;
await page.tracing.start({ path: trace, screenshots: true });
await page.goto(url, {
waitUntil: "networkidle0",
});
await page.tracing.stop();
await browser.close();
res.statusCode = 200;
const rs = fs.createReadStream(trace);
res.setHeader("Content-Type", `application/json`);
res.setHeader("Content-Disposition", `attachment; ${trace}`);
rs.pipe(res);
} catch (err) {
console.log(err);
res.statusCode = 500;
res.json({
error: err.toString(),
});
res.end();
}
};