Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
turned readFile to run asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
adon committed Oct 5, 2015
1 parent 9f0034d commit 372dac9
Showing 1 changed file with 29 additions and 53 deletions.
82 changes: 29 additions & 53 deletions bin/html-purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,46 @@ var Purifier = require('../src/html-purify'),
(function() {
var fs = require('fs'),
file,
// lineByLine = false,
benchmark = false,
noofargs = process.argv.length;
argv = process.argv;

process.argv.forEach(function(val, index) {
if (index === 2) {
file = val;
} else if (index === 3) {
if (val === "--benchmark") {
benchmark = true;
}
// else if (val === "-l") {
// lineByLine = true;
// }
}
});


if (noofargs < 3) {
console.log("Usage: html-purifier <html_filepath> [--benchmark]");
process.exit(1);
}

if (!fs.existsSync(file)) {
console.log("[ERROR] "+file+" not exist");
process.exit(1);
// pick up the parameters
switch (argv.length) {
case 4:
benchmark = argv[3] === '--benchmark';
case 3:
file = argv[2];
break;
default:
console.log("Usage: html-purifier <html_filepath> [--benchmark]");
process.exit(1);
return;
}

var data = fs.readFileSync(file, 'utf-8'), i, output = '';

// The following is disabled as it might generate insecure html,
// as it could violate the assumption that purify() must start with the data state
// if (lineByLine) {
// // reading and processing line by line
// var data2 = data.split(/\n/);
// for (i = 0; i < data2.length; i++) {
// if (data2[i].length !== 0) {
// output = (new Purifier()).purify(data2[i]);
// console.log("*****");
// console.log("input ==> " + data2[i]);
// console.log("output ==> " + output);
// }
// }
// process.exit(0);
// }
// read the given file path for processing
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
throw err;
}

if (!benchmark) {
console.log((new Purifier()).purify(data));
process.exit(0);
}
else if (benchmark) {
// print the processed file
if (!benchmark) {
console.log((new Purifier()).purify(data));
return;
}

// benchmarking
console.log('Benchmarking...');
var suite = new Benchmark.Suite;
var purifier1a = new Purifier();
var purifier1b = new Purifier({enableTagBalancing:false});
// var purifier1b = new Purifier({enableTagBalancing:false});

suite.add('default', function() {
purifier1a.purify(data);
})
.add('disabled tag balancing', function() {
purifier1b.purify(data);
})
// add listeners
// .add('disabled tag balancing', function() {
// purifier1b.purify(data);
// })
.on('cycle', function(event) {
console.log(String(event.target));
})
Expand All @@ -86,11 +63,10 @@ var Purifier = require('../src/html-purify'),
var t = this.filter('fastest')[0].stats.mean;
console.log('Speed/Time is ', data.length/1000000/t + 'MB/s', t + 's');
})
// run async
.run({
// 'minSamples': 10,
'async': true
});
}
});

}).call(this);

0 comments on commit 372dac9

Please sign in to comment.