UNPKG

middleout.js

Version:

A spoof compression library that pretends to revolutionize data compression using made-up algorithms — inspired by the legendary middle-out compression from Silicon Valley

78 lines (75 loc) 2.55 kB
#!/usr/bin/env node // src/config/index.ts import fs from "fs"; import path from "path"; var defaultConfig = { algorithm: "middle-out", wisemanOptimized: true, aggressionLevel: 7, preserveWhitespace: false, targetWeissman: 5 }; function validateConfig(config) { if (typeof config !== "object" || config === null) { throw new Error("Config must be an object"); } if (!config.algorithm) { throw new Error("algorithm is required"); } const validAlgorithms = ["rle", "stk", "tnt", "zph", "middle-out"]; if (!validAlgorithms.includes(config.algorithm)) { throw new Error(`algorithm must be one of: ${validAlgorithms.join(", ")}`); } if (config.wisemanOptimized !== void 0 && typeof config.wisemanOptimized !== "boolean") { throw new Error("wisemanOptimized must be a boolean"); } if (config.aggressionLevel === void 0 || config.aggressionLevel === null) { throw new Error("aggressionLevel is required"); } if (typeof config.aggressionLevel !== "number" || config.aggressionLevel < 0 || config.aggressionLevel > 10) { throw new Error("aggressionLevel must be a number between 0 and 10"); } if (config.preserveWhitespace === void 0 || config.preserveWhitespace === null) { throw new Error("preserveWhitespace is required"); } if (typeof config.preserveWhitespace !== "boolean") { throw new Error("preserveWhitespace must be a boolean"); } if (config.targetWeissman === void 0 || config.targetWeissman === null) { throw new Error("targetWeissman is required"); } if (typeof config.targetWeissman !== "number" || config.targetWeissman < 0) { throw new Error("targetWeissman must be a positive number"); } return true; } function loadConfig(pathStr = "") { const configPath = pathStr ? path.resolve(pathStr) : path.resolve(process.cwd(), ".middleoutrc"); if (!fs.existsSync(configPath)) { fs.writeFileSync( configPath, JSON.stringify(defaultConfig, null, 2), "utf-8" ); return defaultConfig; } try { const raw = fs.readFileSync(configPath, "utf-8"); const parsed = JSON.parse(raw); validateConfig(parsed); const config = { ...defaultConfig, ...parsed }; return config; } catch (error) { if (error instanceof SyntaxError) { throw new Error( `Invalid JSON in config file ${configPath}: ${error.message}` ); } else if (error instanceof Error) { throw new Error(`Invalid config in ${configPath}: ${error.message}`); } throw error; } } export { loadConfig };