-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: allow running null-parser with a set buffer size
- Loading branch information
Showing
1 changed file
with
50 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
} |