Skip to content

Commit

Permalink
updated checkurls to be optional. This needs to be run for production…
Browse files Browse the repository at this point in the history
… code, but prevents casual DDOSing of caniuse or any other url while building. Use node . checkurls when pushing to production.
  • Loading branch information
Divya Manian committed Jan 22, 2012
1 parent 6203bb8 commit 47531c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
28 changes: 12 additions & 16 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var request = require('request');
var Backbone = require('backbone');
var Handlebars = require('handlebars');
var Markdown = require('node-markdown').Markdown;
var async = require('async');

//polyfill or no
Handlebars.registerHelper('polyfillaction', function(tags) {
Expand Down Expand Up @@ -116,42 +117,37 @@ exports.Markup = Backbone.View.extend({
// property that can be accessed in render.
var source = fs.readFileSync(paths.template).toString();
this.template = Handlebars.compile(source);
this.collection.bind('change', this.updatefile, this);
},

updateurls: function() {
// Triggered only when argument checkurls is passed to CLI
updateurls: function(updateUrlCallback) {
console.log('updating caniuse data');
var collectionlength = this.collection.length;
var eventcount = 0;
var callback = callback;

this.collection.map(function(feature) {
async.forEachSeries(
this.collection.toArray(),
function(feature, callback) {
var req = request({
method: 'GET',
uri: feature.get('moreurl'),
headers: {
'User-Agent': 'html5please'
}
}, function(error, response) {
eventcount++;
if(response.statusCode == 404) {
console.log('url not found: ', feature.get('moreurl'));
feature.unset('moreurl', {silent: true});
}

if(eventcount === collectionlength) {
feature.change();
}
callback();
});
},
function() {
updateUrlCallback();
});
},

updatefile: function() {
if(this.eventcount < this.collection.length) {
this.eventcount++;
} else {
this.render();
}
},

render: function() {
console.log("rendering to the file…");

Expand Down
14 changes: 9 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
var fs = require('fs');

function update() {
//Stolen from Tim Branyen
}

// Require the internal application structures
var app = require("./app");

// Create new structures to fetch/render
var features = new app.Features();
var markup = new app.Markup({ collection: features });

// Process arguments
var myArgs = process.argv.slice(2);

// Reset is triggered whenever fetch happens
features.bind("reset", function() {
markup.updateurls();
//triggered by 'node . checkurls'
if(myArgs[0] == 'checkurls') {
markup.updateurls(function() { markup.render(); });
} else {
markup.render();
}
});

// Kick off
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"underscore": "1.2.2",
"handlebars": "*",
"node-markdown": "*",
"request": "*"
"request": "*",
"async": "*"
},
"devDependencies": {}
}

0 comments on commit 47531c8

Please sign in to comment.