Fix performance issue in WebExtension driver

main
Elbert Alias 2 years ago
parent 8ec975cc99
commit a8f64ee566

@ -21,8 +21,6 @@ const xhrDebounce = []
let xhrAnalyzed = {} let xhrAnalyzed = {}
const scriptsPending = []
function getRequiredTechnologies(name, categoryId) { function getRequiredTechnologies(name, categoryId) {
return name return name
? Wappalyzer.requires.find(({ name: _name }) => _name === name).technologies ? Wappalyzer.requires.find(({ name: _name }) => _name === name).technologies
@ -421,19 +419,29 @@ const Driver = {
return return
} }
if (scriptsPending.includes(request.url)) { const { hostname } = new URL(request.documentUrl)
scriptsPending.splice(scriptsPending.indexOf(request.url), 1)
} else if (request.statusCode === 200) {
scriptsPending.push(request.url)
const response = await fetch(request.url) if (!Driver.cache.hostnames[hostname]) {
Driver.cache.hostnames[hostname] = {}
}
const scripts = await response.text() if (!Driver.cache.hostnames[hostname].analyzedScripts) {
Driver.cache.hostnames[hostname].analyzedScripts = []
}
Driver.onDetect(request.documentUrl, analyze({ scripts })).catch( if (Driver.cache.hostnames[hostname].analyzedScripts.length > 50) {
Driver.error return
)
} }
Driver.cache.hostnames[hostname].analyzedScripts.push(request.url)
const response = await fetch(request.url)
const scripts = (await response.text()).slice(0, 500000)
Driver.onDetect(request.documentUrl, analyze({ scripts })).catch(
Driver.error
)
}, },
/** /**
@ -554,16 +562,15 @@ const Driver = {
const { hostname } = new URL(url) const { hostname } = new URL(url)
// Cache detections // Cache detections
const cache = (Driver.cache.hostnames[hostname] = Driver.cache.hostnames[ const cache = (Driver.cache.hostnames[hostname] = {
hostname
] || {
detections: [], detections: [],
hits: incrementHits ? 0 : 1, hits: incrementHits ? 0 : 1,
https: url.startsWith('https://'), https: url.startsWith('https://'),
analyzedScripts: [],
...(Driver.cache.hostnames[hostname] || []),
dateTime: Date.now(),
}) })
cache.dateTime = Date.now()
// Remove duplicates // Remove duplicates
cache.detections = cache.detections cache.detections = cache.detections
.concat(detections) .concat(detections)