Skip to content

Commit

Permalink
refactor: allow running null-parser with a set buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Jun 20, 2019
1 parent a0216cd commit 28845a0
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions examples/null-parser.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
"use strict";

/* eslint-disable no-console */

const fs = require("fs");
const saxes = require("../lib/saxes");

const xml = fs.readFileSync(process.argv[2]);
const start = Date.now();
const parser = new saxes.SaxesParser({ xmlns: true });
const { argv } = process;
let bs;
const first = argv[2];
let filePath = first;
if (first.startsWith("--bs=")) {
bs = Number(first.substring(first.indexOf("=") + 1));
if (Number.isNaN(bs)) {
throw new Error("bs is not a number");
}
// eslint-disable-next-line prefer-destructuring
filePath = argv[3];
}

if (bs === undefined) {
const xml = fs.readFileSync(filePath);
const start = Date.now();
const parser = new saxes.SaxesParser({ xmlns: true });
parser.onerror = (err) => {
console.error(err);
};

parser.write(xml);
parser.close();
console.log(`Parsing time: ${Date.now() - start}`);
}
else {
const input = fs.createReadStream(filePath);
const start = Date.now();
const parser = new saxes.SaxesParser({ xmlns: true });
parser.onerror = (err) => {
console.error(err);
};

input.on("readable", () => {
// eslint-disable-next-line no-constant-condition
while (true) {
const chunk = input.read(bs);
if (chunk === null) {
return;
}

parser.onerror = (err) => {
console.error(err);
};
parser.write(chunk);
}
});

parser.write(xml);
parser.close();
console.log(`Parsing time: ${Date.now() - start}`);
input.on("end", () => {
parser.close();
console.log(`Parsing time: ${Date.now() - start}`);
});
}

0 comments on commit 28845a0

Please sign in to comment.