-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
27 lines (23 loc) · 909 Bytes
/
parser.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
const assert = require('assert');
function js2json(nearlyJSON) {
return nearlyJSON.replace(/(?<=[{,])([0-9]+)(?=:)/g, '"$1"');
}
function extractChunks(html) {
const m = /"([^"]+)"\+\((\{[^}]*\})[^)]+\)+\+"\."\+(\{[^}]*\}).*?\+"(\.chunk\.js)"/.exec(html);
const res = [];
if (m) {
const [, prefix, namesNearlyJSON, chunksNearlyJSON, suffix] = m;
const names = JSON.parse(js2json(namesNearlyJSON));
const chunks = JSON.parse(js2json(chunksNearlyJSON));
res.push(... Object.entries(chunks).map(
([chunkNum, chunkHash]) => (
'/' + prefix + (names[chunkNum] || chunkNum) + '.' + chunkHash + suffix)));
}
res.push(... Array.from(html.matchAll(/<script src="([^"]+)"/g), m => m[1]));
assert(res.length > 0, `Could not find any chunks in ${html}`);
res.sort();
return res;
}
module.exports = {
extractChunks,
};