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

Commit 372dac9

Browse files
author
adon
committed
turned readFile to run asynchronously
1 parent 9f0034d commit 372dac9

File tree

1 file changed

+29
-53
lines changed

1 file changed

+29
-53
lines changed

bin/html-purify.js

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,46 @@ var Purifier = require('../src/html-purify'),
1414
(function() {
1515
var fs = require('fs'),
1616
file,
17-
// lineByLine = false,
1817
benchmark = false,
19-
noofargs = process.argv.length;
18+
argv = process.argv;
2019

21-
process.argv.forEach(function(val, index) {
22-
if (index === 2) {
23-
file = val;
24-
} else if (index === 3) {
25-
if (val === "--benchmark") {
26-
benchmark = true;
27-
}
28-
// else if (val === "-l") {
29-
// lineByLine = true;
30-
// }
31-
}
32-
});
33-
34-
35-
if (noofargs < 3) {
36-
console.log("Usage: html-purifier <html_filepath> [--benchmark]");
37-
process.exit(1);
38-
}
39-
40-
if (!fs.existsSync(file)) {
41-
console.log("[ERROR] "+file+" not exist");
42-
process.exit(1);
20+
// pick up the parameters
21+
switch (argv.length) {
22+
case 4:
23+
benchmark = argv[3] === '--benchmark';
24+
case 3:
25+
file = argv[2];
26+
break;
27+
default:
28+
console.log("Usage: html-purifier <html_filepath> [--benchmark]");
29+
process.exit(1);
30+
return;
4331
}
4432

45-
var data = fs.readFileSync(file, 'utf-8'), i, output = '';
46-
47-
// The following is disabled as it might generate insecure html,
48-
// as it could violate the assumption that purify() must start with the data state
49-
// if (lineByLine) {
50-
// // reading and processing line by line
51-
// var data2 = data.split(/\n/);
52-
// for (i = 0; i < data2.length; i++) {
53-
// if (data2[i].length !== 0) {
54-
// output = (new Purifier()).purify(data2[i]);
55-
// console.log("*****");
56-
// console.log("input ==> " + data2[i]);
57-
// console.log("output ==> " + output);
58-
// }
59-
// }
60-
// process.exit(0);
61-
// }
33+
// read the given file path for processing
34+
fs.readFile(file, 'utf8', function (err, data) {
35+
if (err) {
36+
throw err;
37+
}
6238

63-
if (!benchmark) {
64-
console.log((new Purifier()).purify(data));
65-
process.exit(0);
66-
}
67-
else if (benchmark) {
39+
// print the processed file
40+
if (!benchmark) {
41+
console.log((new Purifier()).purify(data));
42+
return;
43+
}
6844

45+
// benchmarking
46+
console.log('Benchmarking...');
6947
var suite = new Benchmark.Suite;
7048
var purifier1a = new Purifier();
71-
var purifier1b = new Purifier({enableTagBalancing:false});
49+
// var purifier1b = new Purifier({enableTagBalancing:false});
7250

7351
suite.add('default', function() {
7452
purifier1a.purify(data);
7553
})
76-
.add('disabled tag balancing', function() {
77-
purifier1b.purify(data);
78-
})
79-
// add listeners
54+
// .add('disabled tag balancing', function() {
55+
// purifier1b.purify(data);
56+
// })
8057
.on('cycle', function(event) {
8158
console.log(String(event.target));
8259
})
@@ -86,11 +63,10 @@ var Purifier = require('../src/html-purify'),
8663
var t = this.filter('fastest')[0].stats.mean;
8764
console.log('Speed/Time is ', data.length/1000000/t + 'MB/s', t + 's');
8865
})
89-
// run async
9066
.run({
9167
// 'minSamples': 10,
9268
'async': true
9369
});
94-
}
70+
});
9571

9672
}).call(this);

0 commit comments

Comments
 (0)