From 30ba5c8082b4b09fd35342f37188a59de547221e Mon Sep 17 00:00:00 2001 From: itzco Date: Fri, 14 Aug 2020 15:54:43 +0700 Subject: [PATCH 1/2] Add option to pass non matching XmlNodes --- README.md | 31 +++++++++++++++++++++++++++++-- lib/index.js | 16 +++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 016909f..436447a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - # Xtreamer [![npm version][npm-image]][npm-url] @@ -115,7 +114,7 @@ xtreamerTransform.on("data", (data) => { }); ### max_xml_size ( default - 10000000 ) -`max_xml_size` is maximum the number of characters allowed to hold in memory. +`max_xml_size` is maximum the number of characters allowed to hold in memory. `xtreamer` raises an `error` event in case in memory xml string exceed specified limit. This ensures that the node process doesn't get terminated because of excess in memory data collection. @@ -145,6 +144,34 @@ Note that the converted JSON is internally stringified before sending it back to In case transformer function encounters an error, `xtreamer` emits `error` event and stops the xml conversion process. +## pass_all_nodes (default - false) + +`pass_all_nodes` defines whether to pass only matching nodes or all of them. + +This can be helpful when modifying an XML document on the fly without loosing non matching nodes. + +```javascript +const xtreamer = require("xtreamer"); +// Override `pass_all_nodes` +const options = { pass_all_nodes: true }; +const xtreamer = Xtreamer("XmlNode", options) +``` + +The receiving function must verify before processing, i.e. +```javascript +// A piped transformer +async _transform(chunk, enc, done) { + if (Buffer.isBuffer(chunk)) chunk = chunk.toString(); + if (!chunk.startsWith(" 0) { + this.push(otherNode); + nodes.push(otherNode); + } + } this._options && this._options.transformer && typeof this._options.transformer === "function" ? this.push(JSON.stringify(await this._options.transformer(xmlNode))) : this.push(xmlNode); @@ -52,4 +66,4 @@ module.exports = (node, options = { max_xml_size: Constants.MAX_XML_LENGTH }) => options = options || {}; options.max_xml_size = options.max_xml_size || Constants.MAX_XML_LENGTH; return new Xtreamer(node, options); -}; \ No newline at end of file +}; From 58e2edab39446059d11f98a3e7c9b4a62bdf0368 Mon Sep 17 00:00:00 2001 From: itzco <4988324+mritzco@users.noreply.github.com> Date: Mon, 30 Jun 2025 18:44:30 +0700 Subject: [PATCH 2/2] Fix: Cutting extra nodes 1 char too short Fix pass_all_nodes method --- lib/index.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1fd44e2..58c4d34 100644 --- a/lib/index.js +++ b/lib/index.js @@ -42,7 +42,7 @@ class Xtreamer extends Transform { const xmlNode = this._xmlString.slice(nodeObj.start, endIndex); if (this._options.pass_all_nodes) { - const otherNode = this._xmlString.slice(fromIndex, nodeObj.start - 1).trim(); + const otherNode = this._xmlString.slice(fromIndex, nodeObj.start).trim(); fromIndex = endIndex; if (otherNode.length > 0) { this.push(otherNode); diff --git a/package.json b/package.json index 03553e2..d982302 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xtreamer", - "version": "1.1.2", + "version": "1.1.3", "description": "A NodeJS package to read XML files using NodeJS streams.", "main": "lib/index.js", "scripts": {