UNPKG

@syntropysoft/praetorian

Version:

Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.

107 lines 3.65 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.parseXmlContent = exports.XmlFileAdapter = void 0; const xml2js = __importStar(require("xml2js")); const AbstractFileAdapter_1 = require("../base/AbstractFileAdapter"); /** * XML File Adapter - Functional Programming * * Single Responsibility: Parse XML configuration files only * Uses xml2js library for robust XML parsing */ class XmlFileAdapter extends AbstractFileAdapter_1.AbstractFileAdapter { canHandle(filePath) { // Guard clause: no file path if (!filePath || typeof filePath !== 'string') { return false; } return isXmlFile(filePath); } async read(filePath) { // Guard clause: no file path if (!filePath || typeof filePath !== 'string') { throw new Error('File path is required'); } this.validateFileExists(filePath); try { const content = await this.readFileContent(filePath); return await (0, exports.parseXmlContent)(content); } catch (error) { throw new Error(`Failed to parse XML file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`); } } getFormat() { return 'xml'; } getSupportedExtensions() { return ['.xml']; } } exports.XmlFileAdapter = XmlFileAdapter; /** * Pure function to check if file is XML */ const isXmlFile = (filePath) => { // Guard clause: no file path if (!filePath) { return false; } return filePath.endsWith('.xml'); }; /** * Pure function to parse XML content using xml2js */ const parseXmlContent = async (content) => { // Guard clause: no content if (!content || typeof content !== 'string') { return {}; } try { const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true, explicitRoot: false }); const result = await parser.parseStringPromise(content); return result || {}; } catch (error) { throw new Error(`XML parsing failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; exports.parseXmlContent = parseXmlContent; //# sourceMappingURL=XmlFileAdapter.js.map