| | | 1 | | import * as fs from 'node:fs/promises'; |
| | | 2 | | import * as dotenv from 'dotenv'; |
| | | 3 | | import { inject, injectable } from 'inversify'; |
| | | 4 | | import { |
| | | 5 | | DependencyMissingError, |
| | | 6 | | EnvironmentFileError, |
| | | 7 | | } from '../../domain/errors/DomainErrors.js'; |
| | | 8 | | import type { |
| | | 9 | | MapFileConfig, |
| | | 10 | | ParsedMapFile, |
| | | 11 | | } from '../../domain/MapFileConfig.js'; |
| | | 12 | | import type { ILogger } from '../../domain/ports/ILogger.js'; |
| | | 13 | | import type { IVariableStore } from '../../domain/ports/IVariableStore.js'; |
| | | 14 | | import { TYPES } from '../../types.js'; |
| | | 15 | | |
| | | 16 | | @injectable() |
| | 8 | 17 | | export class FileVariableStore implements IVariableStore { |
| | | 18 | | private logger: ILogger; |
| | | 19 | | |
| | | 20 | | constructor(@inject(TYPES.ILogger) logger: ILogger) { |
| | 44 | 21 | | if (!logger) { |
| | 1 | 22 | | throw new DependencyMissingError('Logger must be specified'); |
| | | 23 | | } |
| | 43 | 24 | | this.logger = logger; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | async getMapping(source: string): Promise<Record<string, string>> { |
| | 9 | 28 | | const { mappings } = await this.getParsedMapping(source); |
| | 6 | 29 | | return mappings; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | async getParsedMapping(source: string): Promise<ParsedMapFile> { |
| | 12 | 33 | | const raw = await this.readJsonFile(source); |
| | 9 | 34 | | const { $config, ...rest } = raw; |
| | | 35 | | const config: MapFileConfig = |
| | 9 | 36 | | $config && typeof $config === 'object' ? $config : {}; |
| | 12 | 37 | | return { config, mappings: rest as Record<string, string> }; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | private async readJsonFile(source: string): Promise<Record<string, unknown>> { |
| | 12 | 41 | | try { |
| | 12 | 42 | | const content = await fs.readFile(source, 'utf-8'); |
| | 10 | 43 | | try { |
| | 10 | 44 | | return JSON.parse(content); |
| | | 45 | | } catch (_err: unknown) { |
| | 1 | 46 | | this.logger.error(`Error parsing JSON from ${source}`); |
| | 1 | 47 | | throw new EnvironmentFileError( |
| | | 48 | | `Invalid JSON in parameter map file: ${source}`, |
| | | 49 | | ); |
| | | 50 | | } |
| | | 51 | | } catch (error) { |
| | 3 | 52 | | if (error instanceof EnvironmentFileError) { |
| | 1 | 53 | | throw error; |
| | | 54 | | } |
| | 2 | 55 | | throw new EnvironmentFileError(`Failed to read map file: ${source}`); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | async getEnvironment(source: string): Promise<Record<string, string>> { |
| | 8 | 60 | | const envVariables: Record<string, string> = {}; |
| | 8 | 61 | | try { |
| | 8 | 62 | | await fs.access(source); |
| | | 63 | | } catch { |
| | 4 | 64 | | return envVariables; |
| | | 65 | | } |
| | 4 | 66 | | const existingEnvContent = await fs.readFile(source, 'utf-8'); |
| | 2 | 67 | | const parsedEnv = dotenv.parse(existingEnvContent) || {}; |
| | 8 | 68 | | Object.assign(envVariables, parsedEnv); |
| | | 69 | | |
| | 8 | 70 | | return envVariables; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | async saveEnvironment( |
| | | 74 | | destination: string, |
| | | 75 | | envVariables: Record<string, string>, |
| | | 76 | | ): Promise<void> { |
| | 10 | 77 | | const envContent = Object.entries(envVariables) |
| | 10 | 78 | | .map(([key, value]) => `${key}=${this.escapeEnvValue(value)}`) |
| | | 79 | | .join('\n'); |
| | | 80 | | |
| | 10 | 81 | | try { |
| | 10 | 82 | | await fs.writeFile(destination, envContent); |
| | | 83 | | } catch (error) { |
| | | 84 | | const errorMessage = |
| | 2 | 85 | | error instanceof Error ? error.message : String(error); |
| | 2 | 86 | | this.logger.error(`Failed to write environment file: ${errorMessage}`); |
| | 2 | 87 | | throw new EnvironmentFileError( |
| | | 88 | | `Failed to write environment file: ${errorMessage}`, |
| | | 89 | | ); |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private escapeEnvValue(value: string): string { |
| | | 94 | | // codeql[js/incomplete-sanitization] |
| | | 95 | | // CodeQL flags this as incomplete sanitization because we don't escape backslashes |
| | | 96 | | // before newlines. However, this is intentional: the dotenv library does NOT |
| | | 97 | | // interpret escape sequences (it treats \n literally as backslash+n, not as a newline). |
| | | 98 | | // Therefore, escaping backslashes would actually break the functionality by |
| | | 99 | | // doubling them when read back by dotenv. This is not a security issue in this context. |
| | 10 | 100 | | return value.replace(/(\r\n|\n|\r)/g, '\\n'); |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | export async function readMapFileConfig( |
| | | 105 | | mapPath: string, |
| | | 106 | | ): Promise<MapFileConfig> { |
| | 4 | 107 | | try { |
| | 4 | 108 | | const content = await fs.readFile(mapPath, 'utf-8'); |
| | 3 | 109 | | try { |
| | 3 | 110 | | const raw = JSON.parse(content); |
| | 3 | 111 | | const config = raw.$config; |
| | 3 | 112 | | return config && typeof config === 'object' ? config : {}; |
| | | 113 | | } catch { |
| | 1 | 114 | | throw new EnvironmentFileError( |
| | | 115 | | `Invalid JSON in parameter map file: ${mapPath}`, |
| | | 116 | | ); |
| | | 117 | | } |
| | | 118 | | } catch (error) { |
| | 2 | 119 | | if (error instanceof EnvironmentFileError) { |
| | 1 | 120 | | throw error; |
| | | 121 | | } |
| | 1 | 122 | | throw new EnvironmentFileError(`Failed to read map file: ${mapPath}`); |
| | | 123 | | } |
| | | 124 | | } |