Spaces:
Running
Running
| /** | |
| * Load a bulk CONFIG_ENV secret into process.env. | |
| * | |
| * This module intentionally does not write CONFIG_ENV to disk. Explicit | |
| * environment variables always win over values from CONFIG_ENV. | |
| */ | |
| function unquote(value) { | |
| if (value.length >= 2) { | |
| const first = value[0]; | |
| const last = value[value.length - 1]; | |
| if ((first === '"' && last === '"') || (first === "'" && last === "'")) { | |
| return value.slice(1, -1); | |
| } | |
| } | |
| return value; | |
| } | |
| export function parseConfigEnvBlock(block) { | |
| const parsed = {}; | |
| if (!block || typeof block !== 'string') return parsed; | |
| for (const rawLine of block.split(/\r?\n/)) { | |
| let line = rawLine.trim(); | |
| if (!line || line.startsWith('#')) continue; | |
| if (line.startsWith('export ')) line = line.slice(7).trim(); | |
| const separatorIndex = line.indexOf('='); | |
| if (separatorIndex <= 0) continue; | |
| const key = line.slice(0, separatorIndex).trim(); | |
| const value = unquote(line.slice(separatorIndex + 1).trim()); | |
| if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue; | |
| parsed[key] = value; | |
| } | |
| return parsed; | |
| } | |
| export function applyConfigEnv(block = process.env.CONFIG_ENV, target = process.env) { | |
| const parsed = parseConfigEnvBlock(block); | |
| let applied = 0; | |
| for (const [key, value] of Object.entries(parsed)) { | |
| if (target[key] === undefined || target[key] === '') { | |
| target[key] = value; | |
| applied++; | |
| } | |
| } | |
| return { parsed: Object.keys(parsed).length, applied }; | |
| } | |
| const result = applyConfigEnv(); | |
| if (result.applied > 0) { | |
| console.info(`[Bootstrap] Loaded ${result.applied} variable(s) from CONFIG_ENV`); | |
| } | |