forked from smiegles/extract-relative-url-heapsnapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
74 lines (56 loc) · 1.75 KB
/
main.js
File metadata and controls
74 lines (56 loc) · 1.75 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
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
const puppeteer = require('puppeteer');
const parser = require('heapsnapshot-parser');
const {url} = require('yargs')
.option('url', {
alias: 'u',
type: 'string',
describe: 'url address'
})
.demandOption(['url'])
.help()
.argv
init(new URL(url));
async function getHeapSnapshot(page, browser) {
const client = await page.target().createCDPSession();
const chunks = [];
client.on('HeapProfiler.addHeapSnapshotChunk', ({chunk}) => {
chunks.push(chunk);
});
await client.send('HeapProfiler.takeHeapSnapshot', {reportProgress: false});
await browser.close();
const snapshot = parser.parse(chunks.join(''));
return snapshot;
}
function parsePathsFromSnapshot(snapshot) {
const paths = new Set()
const regex = /[^/]([\/][a-zA-Z0-9_.-]+)+(?!([gimuy]*[,;\s])|\/\2)/g;
for (let i = 0; i < snapshot.nodes.length; i++) {
const node = snapshot.nodes[i];
const search = node.name.match(regex);
if (search != null) {
search.forEach((path) => {
paths.add(path.substr(1))
})
}
}
return paths;
}
function outputResult(data) {
const sortedData = Array.from(data).sort();
sortedData.forEach((path) => {
console.log(path)
})
}
async function init(url) {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {timeout: 12000});
const snapshot = await getHeapSnapshot(page, browser);
const paths = parsePathsFromSnapshot(snapshot);
outputResult(paths);
} catch (error) {
console.error('Unable to parse paths on this URL. Error message: ', error);
process.exit(-1);
}
}