< Summary - Envilder Node.js SDK

Information
Class: src/sdks/nodejs/src/application/map-file-parser.ts
Assembly: Default
File(s): src/sdks/nodejs/src/application/map-file-parser.ts
Tag: 299_25910610327
Line coverage
92%
Covered lines: 25
Uncovered lines: 2
Coverable lines: 27
Total lines: 77
Line coverage: 92.5%
Branch coverage
88%
Covered branches: 24
Total branches: 27
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/sdks/nodejs/src/application/map-file-parser.ts

#LineLine coverage
 1import type { MapFileConfig } from '../domain/map-file-config.js';
 2import type { ParsedMapFile } from '../domain/parsed-map-file.js';
 3import { SecretProviderType } from '../domain/secret-provider-type.js';
 4
 35const CONFIG_KEY = '$config';
 6
 37const 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 */
 15export 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 {
 1723    const raw = this.parseRawJson(json);
 1724    const mappings = new Map<string, string>();
 1725    let config: MapFileConfig = {};
 26
 1727    for (const [key, value] of Object.entries(raw)) {
 3228      if (key.startsWith('$')) {
 829        if (key === CONFIG_KEY) {
 630          config = this.parseConfig(value);
 31        }
 732        continue;
 33      }
 34
 2435      if (typeof value === 'string') {
 2236        mappings.set(key, value);
 37      }
 38    }
 39
 1540    return { config, mappings };
 41  }
 42
 43  private parseRawJson(json: string): Record<string, unknown> {
 44    let raw: unknown;
 1745    try {
 1746      raw = JSON.parse(json);
 47    } catch {
 048      throw new Error('Invalid map file: content is not valid JSON');
 49    }
 1750    if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
 151      throw new Error('Invalid map file: root must be a JSON object');
 52    }
 1653    return raw as Record<string, unknown>;
 54  }
 55
 56  private parseConfig(value: unknown): MapFileConfig {
 657    if (typeof value !== 'object' || value === null) {
 058      return {};
 59    }
 660    const obj = value as Record<string, unknown>;
 61    const providerStr =
 662      typeof obj.provider === 'string' ? obj.provider.toLowerCase() : undefined;
 663    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
 669    if (providerStr && !config.provider) {
 170      throw new Error(
 71        `Unknown provider: '${obj.provider}'. Supported: aws, azure`,
 72      );
 73    }
 74
 575    return config;
 76  }
 77}