From a0ecb0a98ee70673d134c0070c86d44d2b8f9365 Mon Sep 17 00:00:00 2001 From: Johann du Toit Date: Fri, 17 Jul 2015 13:14:23 +0200 Subject: [PATCH] Updated npm driver to actually return information from HTML and not hit a URL --- src/drivers/npm/index.js | 86 ++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/src/drivers/npm/index.js b/src/drivers/npm/index.js index 35ffd2a35..153273a70 100644 --- a/src/drivers/npm/index.js +++ b/src/drivers/npm/index.js @@ -4,26 +4,65 @@ var request = require('request'); var fs = require('fs'); var path = require('path'); -//TODO -exports.detectFromHTML = function(options) {}; +/** +* Does the actual detection with information passed +**/ +exports.detect = function(options, data, cb) { + // run the wrapper function that will + // trigger the actual library to run + runWrappalyer(options, data, cb); + +}; + +/** +* Wraps the detect method, just kept to reuse old method names +* and not break anything. Although this was just stubbed out. +**/ +exports.detectFromHTML = function(options, data, cb) { + + // run the detect method + exports.detect(options, data, cb); + +}; + +/** +* Do a actual request for the body & headers, then +* run through detection +**/ exports.detectFromUrl = function(options, cb) { - var url = options.url; + // ensure options and url were + if(!options || !options.url) { - if (options.debug) { - console.log('Fetching the page'); - } + // send back a error ... + cb(new Error("\"url\" is a required option to run" + + " wappalyzer and get the page content")) - getHTMLFromUrl(url, function(err, data) { - if (err || data === null) { - cb(err, null); - } else { - runWrappalyer(options, data, function(err, detected, appInfo) { - cb(null, detected, appInfo); - }); - } - }); + } else { + + // local variables to + var url = options.url; + + // get the body content from the url + getHTMLFromUrl(url, function(err, data) { + + // check for error or if we got no data back + if (err || data === null) { + + // handle the error and don't do anything else .. + cb(err, null); + + } else { + + // run actual detection + exports.detect(options, data, cb); + + } + + }); + + } }; function getHTMLFromUrl(url, cb) { @@ -42,7 +81,7 @@ function getHTMLFromUrl(url, cb) { } function getAppsJson(cb) { - fs.readFile(path.resolve(__dirname, 'apps.json'), 'utf8', function(err, data) { + fs.readFile(path.resolve(__dirname, '../../apps.json'), 'utf8', function(err, data) { if (err) throw err; return cb(null, JSON.parse(data)); }); @@ -51,7 +90,20 @@ function getAppsJson(cb) { function runWrappalyer(options, data, cb) { var debug = options.debug || false; - var wappalyzer = require('./wappalyzer').wappalyzer; + // according to environment check it + var wappalyzer = null; + + // change depending on the environment + if( process.env.NODE_ENV ) { + + wappalyzer = require('../../wappalyzer').wappalyzer; + + } else { + + wappalyzer = require('./wappalyzer').wappalyzer; + + } + getAppsJson(function(err, apps) { var w = wappalyzer; w.driver = {