@dawans/promptshield
Version:
Secure your LLM stack with enterprise-grade RulePacks for AI safety scanning
154 lines (153 loc) • 5.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
const ScanConfig_1 = require("../../shared/types/ScanConfig");
const Result_1 = require("../../shared/types/Result");
/**
* Manages application configuration
*/
class ConfigManager {
constructor() {
this.config = { ...ScanConfig_1.defaultScanConfig };
}
/**
* Gets the singleton instance
*/
static getInstance() {
if (!ConfigManager.instance) {
ConfigManager.instance = new ConfigManager();
}
return ConfigManager.instance;
}
/**
* Loads configuration from environment variables
*/
loadFromEnvironment() {
try {
const envConfig = {};
// Load environment variables
if (process.env.PS_OUTPUT_FORMAT) {
const format = process.env.PS_OUTPUT_FORMAT.toLowerCase();
if (['json', 'markdown', 'csv', 'table', 'html', 'ndjson'].includes(format)) {
envConfig.outputFormat = format;
}
}
if (process.env.PS_RULEPACK) {
envConfig.rulepack = process.env.PS_RULEPACK;
}
if (process.env.PS_MEMORY_THRESHOLD) {
const threshold = parseFloat(process.env.PS_MEMORY_THRESHOLD);
if (!isNaN(threshold) && threshold >= 0 && threshold <= 1) {
envConfig.memoryWarningThreshold = threshold;
}
}
if (process.env.PS_TIMEOUT) {
const timeout = parseInt(process.env.PS_TIMEOUT, 10);
if (!isNaN(timeout) && timeout > 0) {
envConfig.timeout = timeout;
}
}
if (process.env.PS_BATCH_SIZE) {
const batchSize = parseInt(process.env.PS_BATCH_SIZE, 10);
if (!isNaN(batchSize) && batchSize > 0) {
envConfig.batchSize = batchSize;
}
}
if (process.env.PS_STREAMING_THRESHOLD) {
const threshold = parseInt(process.env.PS_STREAMING_THRESHOLD, 10);
if (!isNaN(threshold) && threshold > 0) {
envConfig.streamingThreshold = threshold;
}
}
if (process.env.PS_DEBUG === 'true') {
envConfig.debug = true;
}
if (process.env.PS_QUIET === 'true') {
envConfig.quiet = true;
}
if (process.env.PS_VERBOSE === 'true') {
envConfig.verbose = true;
}
if (process.env.PS_STRICT === 'true') {
envConfig.strict = true;
}
// Merge with default config
this.config = { ...this.config, ...envConfig };
return (0, Result_1.ok)(undefined);
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to load environment configuration: ${error}`));
}
}
/**
* Gets the current configuration
*/
getConfig() {
return { ...this.config };
}
/**
* Updates configuration
*/
updateConfig(updates) {
try {
this.config = { ...this.config, ...updates };
return (0, Result_1.ok)(undefined);
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to update configuration: ${error}`));
}
}
/**
* Validates configuration
*/
validateConfig(config) {
const errors = [];
// Validate output format
if (!['json', 'markdown', 'csv', 'table', 'html', 'ndjson'].includes(config.outputFormat)) {
errors.push(`Invalid output format: ${config.outputFormat}`);
}
// Validate severity levels
const validSeverities = ['low', 'medium', 'high', 'critical'];
config.severity.forEach((severity) => {
if (!validSeverities.includes(severity)) {
errors.push(`Invalid severity: ${severity}`);
}
});
// Validate numeric values
if (config.maxDepth && config.maxDepth < 1) {
errors.push('Max depth must be at least 1');
}
if (config.timeout && config.timeout < 1) {
errors.push('Timeout must be at least 1 second');
}
if (config.batchSize && config.batchSize < 1) {
errors.push('Batch size must be at least 1');
}
if (config.streamingThreshold && config.streamingThreshold < 1) {
errors.push('Streaming threshold must be at least 1');
}
if (config.memoryWarningThreshold &&
(config.memoryWarningThreshold < 0 || config.memoryWarningThreshold > 1)) {
errors.push('Memory warning threshold must be between 0 and 1');
}
if (config.compressionLevel &&
(config.compressionLevel < 0 || config.compressionLevel > 9)) {
errors.push('Compression level must be between 0 and 9');
}
// Validate fields
if (config.fields.length === 0) {
errors.push('At least one field must be specified');
}
if (errors.length > 0) {
return (0, Result_1.err)(new Error(`Configuration validation failed: ${errors.join(', ')}`));
}
return (0, Result_1.ok)(undefined);
}
/**
* Resets configuration to defaults
*/
resetToDefaults() {
this.config = { ...ScanConfig_1.defaultScanConfig };
}
}
exports.ConfigManager = ConfigManager;