Remove async/await in NPM driver for ES6 compatibility

main
Elbert Alias 8 years ago
parent 3bda8533b2
commit 50818de3b8

@ -20,6 +20,13 @@ class Driver {
userAgent: 'Mozilla/5.0 (compatible; Wappalyzer)', userAgent: 'Mozilla/5.0 (compatible; Wappalyzer)',
}, options || {}); }, options || {});
this.options.debug = Boolean(this.options.debug);
this.options.recursive = Boolean(this.options.recursive);
this.options.delay = this.options.recursive ? parseInt(this.options.delay, 10) : 0;
this.options.maxDepth = parseInt(this.options.maxDepth, 10);
this.options.maxUrls = parseInt(this.options.maxUrls, 10);
this.options.maxWait = parseInt(this.options.maxWait, 10);
this.origPageUrl = url.parse(pageUrl); this.origPageUrl = url.parse(pageUrl);
this.analyzedPageUrls = []; this.analyzedPageUrls = [];
this.apps = []; this.apps = [];
@ -38,9 +45,7 @@ class Driver {
} }
log(message, source, type) { log(message, source, type) {
if ( Boolean(this.options.debug) ) { this.options.debug && console.log('[wappalyzer ' + type + ']', '[' + source + ']', message);
console.log('[wappalyzer ' + type + ']', '[' + source + ']', message);
}
} }
displayApps(detected) { displayApps(detected) {
@ -71,7 +76,7 @@ class Driver {
} }
fetch(pageUrl, index, depth) { fetch(pageUrl, index, depth) {
return new Promise(async resolve => { return new Promise(resolve => {
// Return when the URL is a duplicate or maxUrls has been reached // Return when the URL is a duplicate or maxUrls has been reached
if ( this.analyzedPageUrls.indexOf(pageUrl.href) !== -1 || this.analyzedPageUrls.length >= this.options.maxUrls ) { if ( this.analyzedPageUrls.indexOf(pageUrl.href) !== -1 || this.analyzedPageUrls.length >= this.options.maxUrls ) {
return resolve(); return resolve();
@ -81,16 +86,13 @@ class Driver {
this.wappalyzer.log('depth: ' + depth + '; delay: ' + ( this.options.delay * index ) + 'ms; url: ' + pageUrl.href, 'driver'); this.wappalyzer.log('depth: ' + depth + '; delay: ' + ( this.options.delay * index ) + 'ms; url: ' + pageUrl.href, 'driver');
// Be nice
if ( this.options.delay ) {
await this.sleep(this.options.delay * index);
}
const browser = new Browser({ const browser = new Browser({
userAgent: this.options.userAgent, userAgent: this.options.userAgent,
waitDuration: this.options.maxWait + 'ms', waitDuration: this.options.maxWait + 'ms',
}); });
this.sleep(this.options.delay * index)
.then(() => {
browser.visit(pageUrl.href, error => { browser.visit(pageUrl.href, error => {
if ( !browser.resources['0'] || !browser.resources['0'].response ) { if ( !browser.resources['0'] || !browser.resources['0'].response ) {
this.wappalyzer.log('No response from server', 'browser', 'error'); this.wappalyzer.log('No response from server', 'browser', 'error');
@ -131,30 +133,29 @@ class Driver {
}); });
}); });
}); });
});
} }
async crawl(pageUrl, index = 1, depth = 1) { crawl(pageUrl, index = 1, depth = 1) {
try { return new Promise(resolve => {
var links = await this.fetch(pageUrl, index, depth); this.fetch(pageUrl, index, depth)
.then(links => {
if ( this.options.recursive && depth < this.options.maxDepth && links ) { if ( links && Boolean(this.options.recursive) && depth < this.options.maxDepth ) {
links = Array.from(links).filter(link => link.hostname === this.origPageUrl.hostname); links = Array.from(links)
.filter(link => link.hostname === this.origPageUrl.hostname)
await Promise.all(links.map(async (link, index) => { .map(link => { link.hash = ''; return link });
link.hash = '';
return Promise.all(links.map((link, index) => this.crawl(link, index + 1, depth + 1)));
return this.crawl(link, index + 1, depth + 1); } else {
})); return Promise.resolve();
}
return Promise.resolve(this.apps);
} catch (error) {
return Promise.reject(error);
} }
})
.then(() => resolve(this.apps));
});
} }
sleep(ms) { sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return ms ? new Promise(resolve => setTimeout(resolve, ms)) : Promise.resolve();
} }
}; };