// static/js/utils/yamlUtils.js
/**
* Extracts the YAML section for a specific domain from the filterChains array.
*/
export function extractFilterChainByDomain(yamlData, domainName) {
if (typeof jsyaml === 'undefined') {
console.error("Error: YAML parser (js-yaml) is required but not found.");
return null;
}
let fullConfig;
try {
const allDocs = jsyaml.loadAll(yamlData);
fullConfig = allDocs.find(doc => doc && typeof doc === 'object');
} catch (e) {
console.error("Error parsing YAML data:", e);
return null;
}
if (!fullConfig || !Array.isArray(fullConfig.filterChains)) {
console.warn("Input YAML does not contain a 'filterChains' array, or the main document was not found.");
return null;
}
let matchingChain;
if (domainName === "*" && fullConfig.filterChains.length > 0) {
matchingChain = fullConfig.filterChains.find(chain => {
return chain.filterChainMatch == null;
});
} else {
matchingChain = fullConfig.filterChains.find(chain => {
const serverNames = chain.filterChainMatch?.serverNames;
return serverNames && serverNames.includes(domainName);
});
}
if (!matchingChain) {
console.log(`No filterChain found for domain: ${domainName}`);
return null;
}
try {
const outputYaml = jsyaml.dump(matchingChain, {
indent: 2,
lineWidth: -1,
flowLevel: -1
});
return outputYaml.trim();
} catch (e) {
console.error("Error dumping YAML data:", e);
return null;
}
}
export function dumpYaml(data) {
if (typeof jsyaml === 'undefined') {
throw new Error("YAML parser (js-yaml) is required but not found.");
}
return jsyaml.dump(data, { indent: 2, lineWidth: -1, flowLevel: -1 }).trim();
}