| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 |
1x
1x
1x
1x
1x
1x
1x
1x
77x
77x
1x
77x
1x
51x
155x
155x
155x
86x
86x
1x
120x
120x
54x
66x
20x
46x
9x
37x
103x
85x
9x
68x
5x
5x
9x
9x
15x
15x
54x
1x
53x
1x
52x
1x
51x
46x
51x
20x
51x
51x
79x
79x
20x
59x
27x
32x
98x
98x
98x
54x
44x
154x
19x
19x
2x
17x
49x
49x
44x
44x
77x
58x
19x
10x
9x
2x
7x
10x
73x
206x
9x
9x
2x
58x
| /*global process, module */
'use strict';
const _ = require('lodash');
const types = require('./types');
const EventEmitter = require('events').EventEmitter;
const errors = require('./errors');
const {
CommonEnvInvalidConfiguration,
CommonEnvRootConfigurationObjectException,
CommonEnvGetOrDieAliasesException,
CommonEnvGetOrDieException
} = errors;
const EVENT_FOUND = 'env:found';
const EVENT_FALLBACK = 'env:fallback';
module.exports = function envFactory() {
var em = new EventEmitter();
var getOrDie = getOrDieFactory(function(fullKeyName) {
throw new CommonEnvGetOrDieException(fullKeyName);
});
var getOrDieWithAliases = getOrDieFactory(function(fullKeyName, $typeConverter, aliases) {
throw new CommonEnvGetOrDieAliasesException(aliases);
});
function createContext() {
return {
fullKeyName: '',
};
}
function addSuffix(context, suffix) {
const newContext = _.clone(context);
newContext.fullKeyName += suffix;
return newContext;
}
function getOrElseAll(object, topContext) {
topContext = topContext || createContext();
if (isConfigurationObject(object)) {
throw new CommonEnvRootConfigurationObjectException(object, topContext.fullKeyName);
}
function resolver(config, value, key) {
const innerContext = addSuffix(topContext, key.toUpperCase());
if (isConfigurationObject(value)) {
config[key] = _getOrElseConfigurationObject(value, innerContext);
} else if (_.isPlainObject(value)) {
config[key] = getOrElseAll(value, addSuffix(innerContext, '_'));
} else if (_.isArray(value) && !isArrayOfAtom(value)) {
config[key] = _getOrElseArray(value, innerContext);
} else {
config[key] = getOrElse(innerContext.fullKeyName, value, null, null, innerContext);
}
return config;
}
return _.reduce(object, resolver, {});
}
/**
* [_getOrElseArray description]
* @param {Array} descriptionValue configuration array description
* @param {Object} context
* @return {Array}
*/
function _getOrElseArray(arrayValues, context) {
// use the first descriptionValue[0] as a template for other elements
/**
* find the largest defined INDEX that starts with ${fullKeyName}__ OR that uses one of the aliases
* @param {String} envKeyNamePrefix env key name prefix
* @param {[type]} env [description]
* @return {[type]} [description]
*/
function getMaxIndex(envKeyNamePrefix, env) {
var envMaxIndex = _.chain(env)
.toPairs()
// only keep keys that are in the format {envKeyNamePrefix}__{NUMBER}[....] (because it can either be an array of array or an array of other objects)
.filter(([envKey, envVal], k) => {
// console.log(envKeyNamePrefix, envKey.substring(envKeyNamePrefix.length), (/^__[0-9]+/).test(envKey.substring(envKeyNamePrefix.length)));
return envKey.startsWith(envKeyNamePrefix) && (/^__[0-9]+/).test(envKey.substring(envKeyNamePrefix.length));
})
// then extract the number from the env key name
.map(([envKey, envVal], k) => {
const matches = envKey.substring(envKeyNamePrefix.length).match(/^__([0-9]+)/);
return parseInt(matches[1], 10);
})
.max()
.value() || 0;
// we want to at least map all config defined elements
// so we choose the greater index between config defined index and the env var defined one
return Math.max(envMaxIndex, arrayValues.length - 1);
}
return _.range(0, getMaxIndex(context.fullKeyName, process.env) + 1).map(function(___, index) {
const object = arrayValues[index] || Â arrayValues[0];
// we always use the first element of the array as a full type descriptor of every other element of this array
return getOrElseAll(object, addSuffix(context, '__' + index + '_'), index);
});
}
/**
* [_getOrElseConfigurationObject description]
* @param {object} config configuration object e.g. {$default: '', $types, ...}
* @param {object} context context
* @return {mixed} a configuration primitive value
*/
function _getOrElseConfigurationObject(config, context) {
if (!_.isUndefined(config.$secure) && typeof(config.$secure) !== 'boolean') {
throw new Error('$secure must be a boolean');
}
if (!_.isUndefined(config.$aliases) && !Array.isArray(config.$aliases)) {
throw new Error('$aliases must be an array');
}
if (!_.isUndefined(config.$aliases) && (_.isUndefined(config.$type) && _.isUndefined(config.$default))) {
throw new CommonEnvInvalidConfiguration(context.fullKeyName);
}
if (_.isUndefined(config.$secure)) {
config.$secure = false;
}
if (_.isUndefined(config.$aliases)) {
config.$aliases = [];
}
// if `$type` is specified it will be used as a type checker and converter, otherwise infer the type from `$default`
var $typeConverter = config.$type || getTypeConverter(config.$default);
return config.$aliases.concat([context.fullKeyName]).reduce(function(memo, varEnvName, i, aliases) {
var isLast = i === aliases.length - 1;
if (memo !== null) {
return memo;
}
// only try to get an env var if memo is undefined
if (isLast) {
return _.isUndefined(config.$default) ? getOrDieWithAliases(varEnvName, $typeConverter, aliases, config.$secure) : getOrElse(varEnvName, config.$default, $typeConverter, config.$secure);
}
return getOrElse(varEnvName, null, $typeConverter, config.$secure);
}, null);
}
/**
* [getOrElse description]
* @param {String} fullKeyName env. var. name
* @param {B} $default default fallback value
* @param {Function} $typeConverter f(A) -> B
* @deprecated it will soon be merged into `context`
* @param {B} $secure hide output log value
* @deprecated it will soon be merged into `context`
* @param {Object} contenxt
* @return {B}
*/
function getOrElse(fullKeyName, $default, $typeConverter, $secure, context) {
$secure = typeof($secure) === 'boolean' ? $secure : false;
$typeConverter = $typeConverter || getTypeConverter($default);
if (_.has(process.env, fullKeyName)) {
return emitFound(fullKeyName, $typeConverter(process.env[fullKeyName]), $secure);
}
return emitFallback(fullKeyName, $default, $secure);
}
function getOrDieFactory(f) {
return function(fullKeyName, $typeConverter, aliases, $secure) {
var value = getOrElse(fullKeyName, null, $typeConverter, $secure);
if (value === null) {
f.apply(null, arguments);
}
return value;
};
}
function emitFound(key, value, secure) {
em.emit(EVENT_FOUND, key, value, secure);
return value;
}
function emitFallback(key, value, secure) {
em.emit(EVENT_FALLBACK, key, value, secure);
return value;
}
return _.extend(em, errors, {
getOrElseAll: getOrElseAll,
getOrElse: getOrElse,
getOrDie: getOrDie,
EVENT_FOUND: EVENT_FOUND,
EVENT_FALLBACK: EVENT_FALLBACK,
types: types.convert
});
};
// Helpers
function getTypeConverter($default) {
return isArrayOfAtom($default) ? arrayTypeConverter($default) : function(value) {
if (_.isNumber($default)) { // @todo it's a bug, it should be types.seems.Integer, changing this will be a breaking change
return toInt(value);
}
if (types.seems.Boolean(value)) {
return String(value).toLowerCase() === 'true';
}
return value;
};
}
function toInt(str) {
return parseInt(str, 10);
}
function isArrayOfAtom(array) {
return _.isArray(array) && array.every(isAtom);
}
function isConfigurationObject(value) {
return _.isObject(value) && (_.has(value, '$default') || _.has(value, '$aliases') || _.has(value, '$type') || _.has(value, '$secure'));
}
/**
* [arrayTypeConverter description]
* @param {string} value environment value
* @return {[type]} [description]
*/
function arrayTypeConverter($default) {
var typeConverter = getTypeConverter($default[0]);
return function(value) {
return value.split(',').map(typeConverter);
};
}
/**
* @param {mixed} value
* @return {Boolean} true if the specified value is either a string, a number or a boolean
*/
function isAtom(value) {
return _.isString(value) || _.isNumber(value) || types.seems.Boolean(value);
}
|