< 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: 558_41035824231
Line coverage
93%
Covered lines: 27
Uncovered lines: 2
Coverable lines: 29
Total lines: 87
Line coverage: 93.1%
Branch coverage
89%
Covered branches: 26
Total branches: 29
Branch coverage: 89.6%
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 {
 2  invalidEnvironmentVariableNameMessage,
 3  isValidEnvironmentVariableName,
 4} from '../domain/environment-variable-name.js';
 5import type { MapFileConfig } from '../domain/map-file-config.js';
 6import type { ParsedMapFile } from '../domain/parsed-map-file.js';
 7import { SecretProviderType } from '../domain/secret-provider-type.js';
 8
 49const CONFIG_KEY = '$config';
 10
 411const PROVIDER_MAP: Record<string, SecretProviderType> = {
 12  aws: SecretProviderType.Aws,
 13  azure: SecretProviderType.Azure,
 14};
 15
 16/**
 17 * Parses a JSON map-file string into a {@link ParsedMapFile}.
 18 */
 19export class MapFileParser {
 20  /**
 21   * Parse a JSON map-file string, extracting `$config` and variable mappings.
 22   *
 23   * @param json - Raw JSON string of the map file.
 24   * @returns Parsed config and variable mappings.
 25   */
 26  parse(json: string): ParsedMapFile {
 5427    const raw = this.parseRawJson(json);
 5428    const mappings = new Map<string, string>();
 5429    let config: MapFileConfig = {};
 30
 5431    for (const [key, value] of Object.entries(raw)) {
 7532      if (key.startsWith('$')) {
 1033        if (key === CONFIG_KEY) {
 734          config = this.parseConfig(value);
 35        }
 936        continue;
 37      }
 38
 39      // Before the non-string filter, so the name rule covers every mapping
 40      // key the file declares, matching the CLI and the published schema.
 6541      if (!isValidEnvironmentVariableName(key)) {
 3542        throw new Error(invalidEnvironmentVariableNameMessage(key));
 43      }
 44
 3045      if (typeof value === 'string') {
 2846        mappings.set(key, value);
 47      }
 48    }
 49
 1750    return { config, mappings };
 51  }
 52
 53  private parseRawJson(json: string): Record<string, unknown> {
 54    let raw: unknown;
 5455    try {
 5456      raw = JSON.parse(json);
 57    } catch {
 058      throw new Error('Invalid map file: content is not valid JSON');
 59    }
 5460    if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
 161      throw new Error('Invalid map file: root must be a JSON object');
 62    }
 5363    return raw as Record<string, unknown>;
 64  }
 65
 66  private parseConfig(value: unknown): MapFileConfig {
 767    if (typeof value !== 'object' || value === null) {
 068      return {};
 69    }
 770    const obj = value as Record<string, unknown>;
 71    const providerStr =
 772      typeof obj.provider === 'string' ? obj.provider.toLowerCase() : undefined;
 773    const config: MapFileConfig = {
 74      provider: providerStr ? PROVIDER_MAP[providerStr] : undefined,
 75      vaultUrl: typeof obj.vaultUrl === 'string' ? obj.vaultUrl : undefined,
 76      profile: typeof obj.profile === 'string' ? obj.profile : undefined,
 77    };
 78
 779    if (providerStr && !config.provider) {
 180      throw new Error(
 81        `Unknown provider: '${obj.provider}'. Supported: aws, azure`,
 82      );
 83    }
 84
 685    return config;
 86  }
 87}

Methods/Properties

parse
parseRawJson
parseConfig