(obj: MapArrayType<any>)
| 1045 | * see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter |
| 1046 | */ |
| 1047 | const lookupEnvironmentVariables = (obj: MapArrayType<any>) => { |
| 1048 | const replaceEnvs = (obj: MapArrayType<any>) => { |
| 1049 | for (let [key, value] of Object.entries(obj)) { |
| 1050 | /* |
| 1051 | * the first invocation of replacer() is with an empty key. Just go on, or |
| 1052 | * we would zap the entire object. |
| 1053 | */ |
| 1054 | if (key === '') { |
| 1055 | obj[key] = value; |
| 1056 | continue |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * If we received from the configuration file a number, a boolean or |
| 1061 | * something that is not a string, we can be sure that it was a literal |
| 1062 | * value. No need to perform any variable substitution. |
| 1063 | * |
| 1064 | * The environment variable expansion syntax "${ENV_VAR}" is just a string |
| 1065 | * of specific form, after all. |
| 1066 | */ |
| 1067 | |
| 1068 | if(key === 'undefined' || value === undefined) { |
| 1069 | delete obj[key] |
| 1070 | continue |
| 1071 | } |
| 1072 | |
| 1073 | if ((typeof value !== 'string' && typeof value !== 'object') || value === null) { |
| 1074 | obj[key] = value; |
| 1075 | continue |
| 1076 | } |
| 1077 | |
| 1078 | if (typeof obj[key] === "object") { |
| 1079 | replaceEnvs(obj[key]); |
| 1080 | continue |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | /* |
| 1085 | * Let's check if the string value looks like a variable expansion (e.g.: |
| 1086 | * "${ENV_VAR}" or "${ENV_VAR:default_value}") |
| 1087 | */ |
| 1088 | // MUXATOR 2019-03-21: we could use named capture groups here once we migrate to nodejs v10 |
| 1089 | const match = value.match(/^\$\{([^:]*)(:((.|\n)*))?\}$/); |
| 1090 | |
| 1091 | if (match == null) { |
| 1092 | // no match: use the value literally, without any substitution |
| 1093 | obj[key] = value; |
| 1094 | continue |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * We found the name of an environment variable. Let's read its actual value |
| 1099 | * and its default value, if given |
| 1100 | */ |
| 1101 | const envVarName = match[1]; |
| 1102 | const envVarValue = process.env[envVarName]; |
| 1103 | const defaultValue = match[3]; |
| 1104 |
no test coverage detected