| | | 1 | | import type { MapFileConfig } from '../domain/map-file-config.js'; |
| | | 2 | | import type { ParsedMapFile } from '../domain/parsed-map-file.js'; |
| | | 3 | | import { SecretProviderType } from '../domain/secret-provider-type.js'; |
| | | 4 | | |
| | 3 | 5 | | const CONFIG_KEY = '$config'; |
| | | 6 | | |
| | 3 | 7 | | const PROVIDER_MAP: Record<string, SecretProviderType> = { |
| | | 8 | | aws: SecretProviderType.Aws, |
| | | 9 | | azure: SecretProviderType.Azure, |
| | | 10 | | }; |
| | | 11 | | |
| | | 12 | | /** |
| | | 13 | | * Parses a JSON map-file string into a {@link ParsedMapFile}. |
| | | 14 | | */ |
| | | 15 | | export class MapFileParser { |
| | | 16 | | /** |
| | | 17 | | * Parse a JSON map-file string, extracting `$config` and variable mappings. |
| | | 18 | | * |
| | | 19 | | * @param json - Raw JSON string of the map file. |
| | | 20 | | * @returns Parsed config and variable mappings. |
| | | 21 | | */ |
| | | 22 | | parse(json: string): ParsedMapFile { |
| | 17 | 23 | | const raw = this.parseRawJson(json); |
| | 17 | 24 | | const mappings = new Map<string, string>(); |
| | 17 | 25 | | let config: MapFileConfig = {}; |
| | | 26 | | |
| | 17 | 27 | | for (const [key, value] of Object.entries(raw)) { |
| | 32 | 28 | | if (key.startsWith('$')) { |
| | 8 | 29 | | if (key === CONFIG_KEY) { |
| | 6 | 30 | | config = this.parseConfig(value); |
| | | 31 | | } |
| | 7 | 32 | | continue; |
| | | 33 | | } |
| | | 34 | | |
| | 24 | 35 | | if (typeof value === 'string') { |
| | 22 | 36 | | mappings.set(key, value); |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | 15 | 40 | | return { config, mappings }; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private parseRawJson(json: string): Record<string, unknown> { |
| | | 44 | | let raw: unknown; |
| | 17 | 45 | | try { |
| | 17 | 46 | | raw = JSON.parse(json); |
| | | 47 | | } catch { |
| | 0 | 48 | | throw new Error('Invalid map file: content is not valid JSON'); |
| | | 49 | | } |
| | 17 | 50 | | if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) { |
| | 1 | 51 | | throw new Error('Invalid map file: root must be a JSON object'); |
| | | 52 | | } |
| | 16 | 53 | | return raw as Record<string, unknown>; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private parseConfig(value: unknown): MapFileConfig { |
| | 6 | 57 | | if (typeof value !== 'object' || value === null) { |
| | 0 | 58 | | return {}; |
| | | 59 | | } |
| | 6 | 60 | | const obj = value as Record<string, unknown>; |
| | | 61 | | const providerStr = |
| | 6 | 62 | | typeof obj.provider === 'string' ? obj.provider.toLowerCase() : undefined; |
| | 6 | 63 | | const config: MapFileConfig = { |
| | | 64 | | provider: providerStr ? PROVIDER_MAP[providerStr] : undefined, |
| | | 65 | | vaultUrl: typeof obj.vaultUrl === 'string' ? obj.vaultUrl : undefined, |
| | | 66 | | profile: typeof obj.profile === 'string' ? obj.profile : undefined, |
| | | 67 | | }; |
| | | 68 | | |
| | 6 | 69 | | if (providerStr && !config.provider) { |
| | 1 | 70 | | throw new Error( |
| | | 71 | | `Unknown provider: '${obj.provider}'. Supported: aws, azure`, |
| | | 72 | | ); |
| | | 73 | | } |
| | | 74 | | |
| | 5 | 75 | | return config; |
| | | 76 | | } |
| | | 77 | | } |