|
| 1 | +/** |
| 2 | + * Simple INI file parser. |
| 3 | + * |
| 4 | + * Supports the subset of INI syntax used by `.sentryclirc` config files: |
| 5 | + * `[section]` headers, `key = value` pairs, and `#`/`;` line comments. |
| 6 | + * |
| 7 | + * Design decisions: |
| 8 | + * - Section and key names are lowercased for case-insensitive lookup |
| 9 | + * - Inline comments are NOT supported (tokens/URLs may contain `#` and `;`) |
| 10 | + * - Duplicate keys: last value wins within a section |
| 11 | + * - Duplicate sections: merged (keys accumulate) |
| 12 | + * - Malformed lines are silently skipped |
| 13 | + * - Quoted values (matching `"` or `'`) are stripped |
| 14 | + */ |
| 15 | + |
| 16 | +import { logger } from "./logger.js"; |
| 17 | + |
| 18 | +const log = logger.withTag("ini"); |
| 19 | + |
| 20 | +/** Parsed INI data: section name → key → value. Keys before any section go into `""`. */ |
| 21 | +export type IniData = Record<string, Record<string, string>>; |
| 22 | + |
| 23 | +/** UTF-8 BOM character */ |
| 24 | +const BOM = "\uFEFF"; |
| 25 | + |
| 26 | +/** Match `[section]` headers, allowing whitespace inside brackets */ |
| 27 | +const SECTION_RE = /^\[([^\]]+)\]$/; |
| 28 | + |
| 29 | +/** Matches trailing `\r` for Windows line ending normalization */ |
| 30 | +const CR_RE = /\r$/; |
| 31 | + |
| 32 | +/** |
| 33 | + * Strip matching outer quotes from a value string. |
| 34 | + * |
| 35 | + * Only strips when the first and last characters are the same quote type |
| 36 | + * (`"` or `'`) and the string is at least 2 characters long. |
| 37 | + */ |
| 38 | +function stripQuotes(value: string): string { |
| 39 | + if (value.length < 2) { |
| 40 | + return value; |
| 41 | + } |
| 42 | + const first = value[0]; |
| 43 | + const last = value.at(-1); |
| 44 | + if ((first === '"' || first === "'") && first === last) { |
| 45 | + return value.slice(1, -1); |
| 46 | + } |
| 47 | + return value; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Parse INI-formatted text into a section→key→value map. |
| 52 | + * |
| 53 | + * @param content - Raw INI file content |
| 54 | + * @returns Parsed data keyed by lowercase section name, then lowercase key |
| 55 | + */ |
| 56 | +export function parseIni(content: string): IniData { |
| 57 | + const data: IniData = {}; |
| 58 | + let currentSection = ""; |
| 59 | + |
| 60 | + // Ensure the global section always exists |
| 61 | + data[currentSection] = {}; |
| 62 | + |
| 63 | + // Strip UTF-8 BOM if present |
| 64 | + const text = content.startsWith(BOM) ? content.slice(1) : content; |
| 65 | + |
| 66 | + for (const rawLine of text.split("\n")) { |
| 67 | + const line = rawLine.replace(CR_RE, "").trim(); |
| 68 | + |
| 69 | + // Skip empty lines and comments (lines starting with # or ;) |
| 70 | + if (line === "" || line[0] === "#" || line[0] === ";") { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + // Check for section header |
| 75 | + const sectionMatch = SECTION_RE.exec(line); |
| 76 | + if (sectionMatch?.[1]) { |
| 77 | + currentSection = sectionMatch[1].trim().toLowerCase(); |
| 78 | + if (!(currentSection in data)) { |
| 79 | + data[currentSection] = {}; |
| 80 | + } |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + // Check for key = value pair |
| 85 | + const eqIndex = line.indexOf("="); |
| 86 | + if (eqIndex === -1) { |
| 87 | + log.debug(`Skipping malformed INI line: ${line}`); |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + const key = line.slice(0, eqIndex).trim().toLowerCase(); |
| 92 | + if (key === "") { |
| 93 | + log.debug(`Skipping INI line with empty key: ${line}`); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + const rawValue = line.slice(eqIndex + 1).trim(); |
| 98 | + const value = stripQuotes(rawValue); |
| 99 | + |
| 100 | + // biome-ignore lint/style/noNonNullAssertion: section is always initialized above |
| 101 | + data[currentSection]![key] = value; |
| 102 | + } |
| 103 | + |
| 104 | + return data; |
| 105 | +} |
0 commit comments