Skip to content

Commit

Permalink
Added redirect chain detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Jul 7, 2014
1 parent bc4e236 commit 13f6da7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
65 changes: 62 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
var _ = require('lodash');
var AssetGraph = require('assetgraph');
var async = require('async');
var request = require('request');
var chalk = require('chalk');

function colorStatus(status) {
if (status < 300) {
return chalk.green(status);
} else if (status < 400) {
return chalk.yellow(status);
} else {
return chalk.red(status);
}
}

function logHttpResult(status, url, redirects) {
redirects.forEach(function (redirect) {
process.stdout.write(colorStatus(redirect.statusCode) + ' ' + redirect.redirectUri + chalk.yellow(' --> '));
});

console.log(colorStatus(status), url);
}

function httpStatus(href) {
return function (callback) {
request({
url: href,
strictSSL: true,
gzip: true
}, function (error, res) {
if (error) {
console.error('httpError', href, error);
}

var status = res.statusCode;

logHttpResult(status, href, res.request.redirects);
callback(error);
});
};
}


module.exports = function (options) {
Expand All @@ -11,17 +51,36 @@ module.exports = function (options) {
});

ag.on('addRelation', function (rel) {
console.log(rel.toString());
//console.log(rel.toString());
});


ag.logEvents()
.loadAssets(options.initialAssets)
.populate({
followRelations: {
type: ['relative', 'rootRelative']
hrefType: ['relative', 'rootRelative']
}
})
.queue(function (assetGraph, callback) {

var outgoingRelations = assetGraph.findRelations({
type: 'HtmlAnchor',
hrefType: ['absolute', 'protocolRelative']
}, true);

var hrefs = _.uniq(outgoingRelations.map(function (relation) {
return relation.to.url;
}));

console.error('Crawling ' + hrefs.length + ' outgoing links:');
async.parallelLimit(hrefs.map(function (href) {
return httpStatus(href);
}),
20,
callback
);
})
.writeStatsToStderr()
.run(options.callback);
};

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"homepage": "https://github.com/Munter/hyperlink",
"dependencies": {
"assetgraph": "^1.6.42",
"async": "^0.9.0",
"chalk": "^0.5.0",
"lodash": "^2.4.1",
"request": "^2.36.0"
}
}

0 comments on commit 13f6da7

Please sign in to comment.