UNPKG

@syntropysoft/praetorian

Version:

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

149 lines 5.08 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.parseYamlContent = exports.YamlFileAdapter = void 0; const yaml = __importStar(require("js-yaml")); const AbstractFileAdapter_1 = require("../base/AbstractFileAdapter"); /** * YAML File Adapter - Functional Programming * * Single Responsibility: Parse YAML configuration files only * Pure functions, no state, no side effects */ class YamlFileAdapter extends AbstractFileAdapter_1.AbstractFileAdapter { canHandle(filePath) { // Guard clause: no file path if (!filePath || typeof filePath !== 'string') { return false; } return isYamlFile(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 (0, exports.parseYamlContent)(content, filePath); } catch (error) { throw new Error(`Failed to parse YAML file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`); } } getFormat() { return 'yaml'; } getSupportedExtensions() { return ['.yaml', '.yml']; } } exports.YamlFileAdapter = YamlFileAdapter; /** * Pure function to check if file is YAML */ const isYamlFile = (filePath) => { // Guard clause: no file path if (!filePath) { return false; } return filePath.endsWith('.yaml') || filePath.endsWith('.yml'); }; /** * Pure function to parse YAML content */ const parseYamlContent = (content, filePath) => { // Guard clause: no content if (!content || typeof content !== 'string') { return {}; } try { const parsedContent = yaml.load(content); return validateYamlContent(parsedContent, filePath); } catch (error) { const errorMessage = getYamlErrorMessage(error, filePath); throw new Error(errorMessage); } }; exports.parseYamlContent = parseYamlContent; /** * Pure function to validate YAML content structure */ const validateYamlContent = (parsedContent, filePath) => { // Guard clause: no parsed content if (parsedContent === null || parsedContent === undefined) { return {}; } // Guard clause: not an object if (typeof parsedContent !== 'object') { const fileName = filePath ? ` in ${filePath}` : ''; throw new Error(`Invalid YAML content${fileName}: expected object, got ${typeof parsedContent}`); } // Guard clause: array instead of object if (Array.isArray(parsedContent)) { const fileName = filePath ? ` in ${filePath}` : ''; throw new Error(`Invalid YAML content${fileName}: expected object, got array`); } return parsedContent; }; /** * Pure function to get YAML error message */ const getYamlErrorMessage = (error, filePath) => { // Guard clause: no error if (!error) { return 'Unknown YAML parsing error'; } const fileName = filePath ? ` in ${filePath}` : ''; // Check if it's a YAML syntax error if (isYamlSyntaxError(error)) { return `Invalid YAML syntax${fileName}: ${error.message}`; } // Generic error return `YAML parsing error${fileName}: ${error.message || 'Unknown error'}`; }; /** * Pure function to check if error is YAML syntax error */ const isYamlSyntaxError = (error) => { // Guard clause: no error if (!error) { return false; } return error instanceof yaml.YAMLException; }; //# sourceMappingURL=YamlFileAdapter.js.map