Initial commit

master
Sven Slootweg 3 years ago
commit 7c7a6acaaf

@ -0,0 +1,3 @@
{
"extends": "@joepie91/eslint-config"
}

1
.gitignore vendored

@ -0,0 +1 @@
node_modules

@ -0,0 +1,65 @@
"use strict";
// FIXME: Document that this *may* not strip whitespace around text elements, and that it includes potentially-whitespace-only text elements between tags!
const Promise = require("bluebird");
const saxes = require("saxes");
const consumable = require("@joepie91/consumable");
const pEvent = require("p-event");
const debug = require("debug")("promistream:parse-xml");
const pipe = require("@promistream/pipe");
const map = require("@promistream/map");
const buffer = require("@promistream/buffer");
const lastWill = require("@promistream/last-will");
const { validateOptions, validateValue } = require("@validatem/core");
const required = require("@validatem/required");
const defaultTo = require("@validatem/default-to");
const isString = require("@validatem/is-string");
const allowExtraProperties = require("@validatem/allow-extra-properties");
const arrayOf = require("@validatem/array-of");
module.exports = function parseXML(_options) {
let options = validateOptions(arguments, [
defaultTo({}),
allowExtraProperties({
events: [ required, arrayOf(isString) ]
})
]);
debug("Creating Saxes parser");
let parser = new saxes.SaxesParser(options);
let queue = consumable([]);
let endEventPromise = pEvent(parser, "end");
for (let eventName of options.events) {
parser.on(eventName, (value) => {
debug(`${eventName} event emitted by parser`);
queue.peek().push({ type: eventName, value: value });
});
}
return pipe([
map((value) => {
validateValue(value, [ isString ]); // FIXME: Turn this into a clearer error
debug("Writing chunk to parser");
parser.write(value);
debug(`Returning ${queue.peek().length} parser items`);
return queue.replace([]);
}),
lastWill({ onAllEnds: () => {
return Promise.try(() => {
parser.close();
return endEventPromise;
}).then(() => {
debug("Parser finished processing");
return queue.consume();
});
}}),
buffer()
]);
};

@ -0,0 +1,32 @@
{
"name": "@promistream/parse-xml",
"version": "0.1.0",
"main": "index.js",
"keywords": [
"promistream"
],
"repository": "http://git.cryto.net/promistream/parse-xml.git",
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"devDependencies": {
"@joepie91/eslint-config": "^1.1.0",
"eslint": "^6.8.0"
},
"dependencies": {
"@joepie91/consumable": "^1.0.1",
"@promistream/buffer": "^0.1.0",
"@promistream/last-will": "^0.1.0",
"@promistream/map": "^0.1.0",
"@promistream/pipe": "^0.1.1",
"@validatem/allow-extra-properties": "^0.1.0",
"@validatem/array-of": "^0.1.2",
"@validatem/core": "^0.3.13",
"@validatem/default-to": "^0.1.0",
"@validatem/is-string": "^1.0.0",
"@validatem/required": "^0.1.1",
"bluebird": "^3.7.2",
"debug": "^4.3.1",
"p-event": "^4.2.0",
"saxes": "^5.0.1"
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save