"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { StreamXMLAppender: () => StreamXMLAppender, StreamXMLReader: () => StreamXMLReader }); module.exports = __toCommonJS(src_exports); // src/model/stream-xml-editor.ts var saxes = __toESM(require("saxes"), 1); var StreamXMLEditor = class { constructor(readStream) { this.readStream = readStream; this.saxesParser = new saxes.SaxesParser({ fragment: true }); this.saxesParser.on("error", (error) => { console.log("!error", error); }); } isInsideNode(path) { if (path[0] != "/") { throw new Error("Path has to be started with /"); } if (path[path.length - 1] == "/") { throw new Error("Path cannot be ended with /"); } const arrayPath = path.split("/"); arrayPath.shift(); const names = []; for (const tag of this.saxesParser["tags"]) { names.push(tag.name); } if (names.length != arrayPath.length) { return false; } if (JSON.stringify(names) != JSON.stringify(arrayPath)) { return false; } return true; } }; // src/model/stream-xml-reader.ts var StreamXMLReader = class extends StreamXMLEditor { constructor(readStream) { super(readStream); } start() { this.readStream.on("readable", () => { let chunk; while (null !== (chunk = this.readStream.read())) { this.saxesParser.write(chunk); } }); this.readStream.on("end", () => { this.saxesParser.close(); }); } addReadingRule(path, nodeType, handler) { this.saxesParser.on(nodeType, (content) => { if (this.isInsideNode(path)) { return handler(content); } }); } }; // src/model/stream-xml-appender.ts var StreamXMLAppender = class extends StreamXMLEditor { constructor(readStream, writeStream) { super(readStream); this.writeStream = writeStream; this.tasks = []; this.saxesParser.on("closetag", () => { if (this.tasks.length > 0) { if (this.tasks[0].parentNode.length > 0 && this.isInsideNode(this.tasks[0].parentNode)) { const task = this.tasks.shift(); if (task && task.value.length > 0) { let indents = ""; if (task.doIndent) { indents = "\n"; const indents_number = this.saxesParser["tags"].length; for (let it = 0; it < indents_number; it++) { indents = indents + " "; } } this.writeStream.write(indents + task.value); } } } }); } start() { this.readStream.on("readable", () => { let chunk; while (null !== (chunk = this.readStream.read(1))) { this.writeStream.write(chunk); this.saxesParser.write(chunk); } }); this.readStream.on("end", () => { this.saxesParser.close(); this.writeStream.close(); }); } addAppendingRule(pathToRead, nodeType, handler) { this.saxesParser.on(nodeType, (content) => { if (this.isInsideNode(pathToRead)) { const task = handler(content); this.tasks.push(task); } }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { StreamXMLAppender, StreamXMLReader }); //# sourceMappingURL=index.cjs.map