config 4.4.1 → 5.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/config.js +2 -717
- package/lib/config.mjs +720 -0
- package/lib/defer.js +2 -3
- package/lib/util.js +15 -14
- package/package.json +3 -20
- package/parser.js +34 -40
- package/types/lib/config.d.mts +365 -0
- package/types/lib/config.d.ts +2 -364
- package/types/lib/util.d.ts +1 -1
- package/types/parser.d.ts +131 -128
- package/async.js +0 -91
- package/config-4.4.1.tgz +0 -0
- package/defer.js +0 -19
- package/raw.js +0 -16
package/lib/config.js
CHANGED
|
@@ -1,719 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
// May be freely distributed under the MIT license.
|
|
3
|
-
// For further details and documentation:
|
|
4
|
-
// http://lorenwest.github.com/node-config
|
|
5
|
-
|
|
6
|
-
// Dependencies
|
|
7
|
-
/** @typedef {import('./util').Util} Util */
|
|
8
|
-
/** @typedef {import('./util').Load} Load */
|
|
9
|
-
/** @typedef {import('./util').LoadOptions} LoadOptions */
|
|
10
|
-
/** @typedef {typeof import('./../parser')} Parser */
|
|
11
|
-
const { Util, Load, RawConfig } = require('./util.js');
|
|
12
|
-
const Path = require('path');
|
|
13
|
-
|
|
14
|
-
const LOAD_SYMBOL = Symbol('load');
|
|
15
|
-
const DEFAULT_CLONE_DEPTH = 20;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* <p>Application Configurations</p>
|
|
19
|
-
* @class
|
|
20
|
-
*/
|
|
21
|
-
class ConfigClass {
|
|
22
|
-
/**
|
|
23
|
-
* Non-enumerable field for util functions
|
|
24
|
-
*
|
|
25
|
-
* @type {ConfigUtils}
|
|
26
|
-
*/
|
|
27
|
-
util;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* <p>Get the configuration object.</p>
|
|
31
|
-
*
|
|
32
|
-
* <p>
|
|
33
|
-
* The configuration object is a shared singleton object within the application,
|
|
34
|
-
* attained by calling require('config').
|
|
35
|
-
* </p>
|
|
36
|
-
*
|
|
37
|
-
* <p>
|
|
38
|
-
* Usually you'll specify a CONFIG variable at the top of your .js file
|
|
39
|
-
* for file/module scope. If you want the root of the object, you can do this:
|
|
40
|
-
* </p>
|
|
41
|
-
* <pre>
|
|
42
|
-
* const CONFIG = require('config');
|
|
43
|
-
* </pre>
|
|
44
|
-
*
|
|
45
|
-
* <p>
|
|
46
|
-
* Sometimes you only care about a specific sub-object within the CONFIG
|
|
47
|
-
* object. In that case you could do this at the top of your file:
|
|
48
|
-
* </p>
|
|
49
|
-
* <pre>
|
|
50
|
-
* const CONFIG = require('config').customer;
|
|
51
|
-
* or
|
|
52
|
-
* const CUSTOMER_CONFIG = require('config').customer;
|
|
53
|
-
* </pre>
|
|
54
|
-
*
|
|
55
|
-
* <script type="text/javascript">
|
|
56
|
-
* document.getElementById("showProtected").style.display = "block";
|
|
57
|
-
* </script>
|
|
58
|
-
*
|
|
59
|
-
* @method constructor
|
|
60
|
-
*/
|
|
61
|
-
constructor(load) {
|
|
62
|
-
this[LOAD_SYMBOL] = load;
|
|
63
|
-
|
|
64
|
-
Util.extendDeep(this, load.config);
|
|
65
|
-
Util.makeHidden(this, 'util', new ConfigUtils(this));
|
|
66
|
-
this.util.attachProtoDeep(this);
|
|
67
|
-
// Perform strictness checks and possibly throw an exception.
|
|
68
|
-
this.util.runStrictnessChecks(this);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* <p>Get a configuration value</p>
|
|
73
|
-
*
|
|
74
|
-
* <p>
|
|
75
|
-
* This will return the specified property value, throwing an exception if the
|
|
76
|
-
* configuration isn't defined. It is used to assure configurations are defined
|
|
77
|
-
* before being used, and to prevent typos.
|
|
78
|
-
* </p>
|
|
79
|
-
*
|
|
80
|
-
* @template T
|
|
81
|
-
* @param {string} property - The configuration property to get. Can include '.' sub-properties.
|
|
82
|
-
* @returns {T} The property value
|
|
83
|
-
*/
|
|
84
|
-
get(property) {
|
|
85
|
-
if(property === null || typeof property === "undefined"){
|
|
86
|
-
throw new Error("Calling config.get with null or undefined argument");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Make configurations immutable after first get (unless disabled)
|
|
90
|
-
const load = this[LOAD_SYMBOL];
|
|
91
|
-
if (load.updated) {
|
|
92
|
-
load.updated = false;
|
|
93
|
-
|
|
94
|
-
if (!load.initParam('ALLOW_CONFIG_MUTATIONS', false)) {
|
|
95
|
-
if (Object.isExtensible(this)) {
|
|
96
|
-
this.util.makeImmutable();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const value = Util.getPath(this, property);
|
|
102
|
-
|
|
103
|
-
// Produce an exception if the property doesn't exist
|
|
104
|
-
if (typeof value === "undefined") {
|
|
105
|
-
throw new Error('Configuration property "' + property + '" is not defined');
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Return the value
|
|
109
|
-
return value;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Test that a configuration parameter exists
|
|
114
|
-
*
|
|
115
|
-
* <pre>
|
|
116
|
-
* const config = require('config');
|
|
117
|
-
* if (config.has('customer.dbName')) {
|
|
118
|
-
* console.log('Customer database name: ' + config.customer.dbName);
|
|
119
|
-
* }
|
|
120
|
-
* </pre>
|
|
121
|
-
*
|
|
122
|
-
* @method has
|
|
123
|
-
* @param {string} property - The configuration property to test. Can include '.' sub-properties.
|
|
124
|
-
* @return {boolean} - True if the property is defined, false if not defined.
|
|
125
|
-
*/
|
|
126
|
-
has(property) {
|
|
127
|
-
// While get() throws an exception for undefined input, has() is designed to test validity, so false is appropriate
|
|
128
|
-
if (property === null || typeof property === "undefined"){
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return typeof Util.getPath(this, property) !== "undefined";
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* The exported configuration instance type.
|
|
138
|
-
* This is here because the Config class is not a named export and this is how we get
|
|
139
|
-
* the exported configuration instance type.
|
|
140
|
-
* @typedef {ConfigClass} Config
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Source of config.util utility functions.
|
|
145
|
-
*
|
|
146
|
-
* In general, lib/util.js is what you are looking for.
|
|
147
|
-
*
|
|
148
|
-
* @class ConfigUtils
|
|
149
|
-
*/
|
|
150
|
-
class ConfigUtils {
|
|
151
|
-
/** @type {Config} */
|
|
152
|
-
#config = undefined;
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* @param {Config} config
|
|
156
|
-
*/
|
|
157
|
-
constructor(config) {
|
|
158
|
-
this.#config = config;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* <p>
|
|
163
|
-
* Set default configurations for a node.js module.
|
|
164
|
-
* </p>
|
|
165
|
-
*
|
|
166
|
-
* <p>
|
|
167
|
-
* This allows module developers to attach their configurations onto the
|
|
168
|
-
* default configuration object so they can be configured by the consumers
|
|
169
|
-
* of the module.
|
|
170
|
-
* </p>
|
|
171
|
-
*
|
|
172
|
-
* <p>Using the function within your module:</p>
|
|
173
|
-
* <pre>
|
|
174
|
-
* const CONFIG = require("config");
|
|
175
|
-
* CONFIG.util.setModuleDefaults("MyModule", {
|
|
176
|
-
* templateName: "t-50",
|
|
177
|
-
* colorScheme: "green"
|
|
178
|
-
* });
|
|
179
|
-
* <br>
|
|
180
|
-
* // Template name may be overridden by application config files
|
|
181
|
-
* console.log("Template: " + CONFIG.MyModule.templateName);
|
|
182
|
-
* </pre>
|
|
183
|
-
*
|
|
184
|
-
* <p>
|
|
185
|
-
* The above example results in a "MyModule" element of the configuration
|
|
186
|
-
* object, containing an object with the specified default values.
|
|
187
|
-
* </p>
|
|
188
|
-
*
|
|
189
|
-
* @method setModuleDefaults
|
|
190
|
-
* @param {string} moduleName - Name of your module.
|
|
191
|
-
* @param {any} defaultProperties - The default module configuration.
|
|
192
|
-
* @return {any} moduleConfig - The module level configuration object.
|
|
193
|
-
* @see Load.scan() for loading more robust defaults
|
|
194
|
-
*/
|
|
195
|
-
setModuleDefaults(moduleName, defaultProperties) {
|
|
196
|
-
// Copy the properties into a new object
|
|
197
|
-
const path = moduleName.split('.');
|
|
198
|
-
const load = this.#config[LOAD_SYMBOL];
|
|
199
|
-
const moduleConfig = load.setModuleDefaults(moduleName, defaultProperties);
|
|
200
|
-
let existing = Util.getPath(this.#config, path);
|
|
201
|
-
|
|
202
|
-
if (existing === undefined) {
|
|
203
|
-
Util.setPath(this.#config, path, Util.cloneDeep(moduleConfig));
|
|
204
|
-
} else {
|
|
205
|
-
Util.extendDeep(existing, moduleConfig);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Attach handlers & watchers onto the module config object
|
|
209
|
-
return this.attachProtoDeep(Util.getPath(this.#config, path));
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Set default configurations for a node.js module.
|
|
214
|
-
*
|
|
215
|
-
* This variant is provided to support handling loading of multiple versions
|
|
216
|
-
* of a library. This is meant for module developers to create a config snapshot
|
|
217
|
-
* for an old version of the code, particularly during a staged upgrade.
|
|
218
|
-
* Instead of adding the values to the configuration, it creates a new Config
|
|
219
|
-
* instance that contains the provided defaults.
|
|
220
|
-
*
|
|
221
|
-
* Note: This feature is primarily useful for adding, removing, or renaming
|
|
222
|
-
* properties. It will struggle to deal with changing the semantics of
|
|
223
|
-
* existing fields if you need to override those fields in your top level
|
|
224
|
-
* config/ directory. It is always best when versioning an API to change the
|
|
225
|
-
* names of fields when you change the meaning of the field.
|
|
226
|
-
*
|
|
227
|
-
* <p>Using the function within your module:</p>
|
|
228
|
-
* <pre>
|
|
229
|
-
* const configModule = require("config");
|
|
230
|
-
* const config = configModule.withModuleDefaults("MyModule", {
|
|
231
|
-
* templateName: "t-50",
|
|
232
|
-
* colorScheme: "green"
|
|
233
|
-
* });
|
|
234
|
-
* <br>
|
|
235
|
-
* // Template name may be overridden by application config files
|
|
236
|
-
* console.log("Template: " + config.MyModule.templateName);
|
|
237
|
-
* </pre>
|
|
238
|
-
*
|
|
239
|
-
* <p>
|
|
240
|
-
* The above example results in a "MyModule" element of the configuration
|
|
241
|
-
* object, containing an object with the specified default values.
|
|
242
|
-
* </p>
|
|
243
|
-
*
|
|
244
|
-
* @method withModuleDefaults
|
|
245
|
-
* @param moduleName {string} - Name of your module.
|
|
246
|
-
* @param defaultProperties {Object} - The default module configuration.
|
|
247
|
-
* @returns {Config}
|
|
248
|
-
* @see Load.scan() for loading more robust defaults
|
|
249
|
-
*/
|
|
250
|
-
withModuleDefaults(moduleName, defaultProperties) {
|
|
251
|
-
const load = this.#config[LOAD_SYMBOL];
|
|
252
|
-
const copy = new ConfigClass(load.clone());
|
|
253
|
-
|
|
254
|
-
copy.util.setModuleDefaults(moduleName, defaultProperties);
|
|
255
|
-
|
|
256
|
-
return copy;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* <p>Make a javascript object property immutable (assuring it cannot be changed
|
|
261
|
-
* from the current value).</p>
|
|
262
|
-
* <p>
|
|
263
|
-
* If the specified property is an object, all attributes of that object are
|
|
264
|
-
* made immutable, including properties of contained objects, recursively.
|
|
265
|
-
* If a property name isn't supplied, the object and all of its properties
|
|
266
|
-
* are made immutable.
|
|
267
|
-
* </p>
|
|
268
|
-
* <p>
|
|
269
|
-
* This operation cannot be undone.
|
|
270
|
-
* </p>
|
|
271
|
-
*
|
|
272
|
-
* <p>Example:</p>
|
|
273
|
-
* <pre>
|
|
274
|
-
* const config = require('config');
|
|
275
|
-
* const myObject = {hello:'world'};
|
|
276
|
-
* config.util.makeImmutable(myObject);
|
|
277
|
-
* </pre>
|
|
278
|
-
*
|
|
279
|
-
* @method makeImmutable
|
|
280
|
-
* @deprecated see Util.makeImmutable()
|
|
281
|
-
* @param {any} object - The object to specify immutable properties for
|
|
282
|
-
* @param {string | string[]=} property - The name of the property (or array of names) to make immutable.
|
|
283
|
-
* If not provided, the entire object is marked immutable.
|
|
284
|
-
* @param {any=} value - Property value (or array of values) to set
|
|
285
|
-
* the property to before making immutable. Only used when setting a single
|
|
286
|
-
* property. Retained for backward compatibility.
|
|
287
|
-
* @return {any} - The original object is returned - for chaining.
|
|
288
|
-
*/
|
|
289
|
-
makeImmutable(object, property, value) {
|
|
290
|
-
if (object === undefined) {
|
|
291
|
-
return Util.makeImmutable(this.#config);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
Util.errorOnce("MAKE_IMMUTABLE", "`config.util.makeImmutable(obj)` is deprecated, and will not be supported in 5.0");
|
|
295
|
-
|
|
296
|
-
if (property !== undefined) {
|
|
297
|
-
return this.makeImmutablePartial(object, property, value);
|
|
298
|
-
} else {
|
|
299
|
-
return Util.makeImmutable(object);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* <p>Make a javascript object property immutable (assuring it cannot be changed
|
|
305
|
-
* from the current value).</p>
|
|
306
|
-
* <p>
|
|
307
|
-
* If the specified property is an object, all attributes of that object are
|
|
308
|
-
* made immutable, including properties of contained objects, recursively.
|
|
309
|
-
* If a property name isn't supplied, the object and all of its properties
|
|
310
|
-
* are made immutable.
|
|
311
|
-
* </p>
|
|
312
|
-
* <p>
|
|
313
|
-
* This operation cannot be undone.
|
|
314
|
-
* </p>
|
|
315
|
-
*
|
|
316
|
-
* <p>Example:</p>
|
|
317
|
-
* <pre>
|
|
318
|
-
* const config = require('config');
|
|
319
|
-
* const myObject = {hello:'world'};
|
|
320
|
-
* config.util.makeImmutable(myObject);
|
|
321
|
-
* </pre>
|
|
322
|
-
*
|
|
323
|
-
* @method makeImmutable
|
|
324
|
-
* @protected
|
|
325
|
-
* @deprecated - partial immutability will no longer be supported by this project
|
|
326
|
-
* @param object {Object} - The object to specify immutable properties for
|
|
327
|
-
* @param property {string | [string]} - The name of the property (or array of names) to make immutable.
|
|
328
|
-
* If not provided, the entire object is marked immutable.
|
|
329
|
-
* @param [value] {* | [*]} - Property value (or array of values) to set
|
|
330
|
-
* the property to before making immutable. Only used when setting a single
|
|
331
|
-
* property. Retained for backward compatibility.
|
|
332
|
-
* @return object {Object} - The original object is returned - for chaining.
|
|
333
|
-
*/
|
|
334
|
-
makeImmutablePartial(object, property, value) {
|
|
335
|
-
Util.errorOnce("PARTIAL_IMMUTABLE", "`makeImmutable(object, propName, value)` is deprecated and will not be supported in 5.0");
|
|
336
|
-
// Backwards compatibility mode where property/value can be specified
|
|
337
|
-
if (typeof property === 'string') {
|
|
338
|
-
return Object.defineProperty(object, property, {
|
|
339
|
-
value: (typeof value === 'undefined') ? object[property] : value,
|
|
340
|
-
writable: false,
|
|
341
|
-
configurable: false
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
let properties = property;
|
|
346
|
-
|
|
347
|
-
// Process each property
|
|
348
|
-
for (let i = 0; i < properties.length; i++) {
|
|
349
|
-
const propertyName = properties[i];
|
|
350
|
-
let value = object[propertyName];
|
|
351
|
-
|
|
352
|
-
if (value instanceof RawConfig) {
|
|
353
|
-
Object.defineProperty(object, propertyName, {
|
|
354
|
-
value: value.resolve(),
|
|
355
|
-
writable: false,
|
|
356
|
-
configurable: false
|
|
357
|
-
});
|
|
358
|
-
} else if (Array.isArray(value)) {
|
|
359
|
-
// Ensure object items of this array are also immutable.
|
|
360
|
-
value.forEach((item, index) => {
|
|
361
|
-
if (Util.isObject(item) || Array.isArray(item)) {
|
|
362
|
-
Util.makeImmutable(item);
|
|
363
|
-
}
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
Object.defineProperty(object, propertyName, {
|
|
367
|
-
value: Object.freeze(value)
|
|
368
|
-
});
|
|
369
|
-
} else {
|
|
370
|
-
// Call recursively if an object.
|
|
371
|
-
if (Util.isObject(value)) {
|
|
372
|
-
// Create a proxy, to capture user updates of configuration options, and throw an exception for awareness, as per:
|
|
373
|
-
// https://github.com/lorenwest/node-config/issues/514
|
|
374
|
-
value = new Proxy(Util.makeImmutable(value), {
|
|
375
|
-
get(target, property, receiver) {
|
|
376
|
-
// Config's own defined prototype properties and methods (e.g., `get`, `has`, etc.)
|
|
377
|
-
const ownProps = [
|
|
378
|
-
...Object.getOwnPropertyNames(ConfigClass.prototype), //TODO: This keeps us from moving this function to util.js where it belongs
|
|
379
|
-
...Object.getOwnPropertyNames(target),
|
|
380
|
-
]
|
|
381
|
-
|
|
382
|
-
// Bypass proxy receiver for properties directly on the target (e.g., RegExp.prototype.source)
|
|
383
|
-
// or properties that are not functions to prevent errors related to internal object methods.
|
|
384
|
-
if (ownProps.includes(property) || (property in target && typeof target[property] !== 'function')) {
|
|
385
|
-
return Reflect.get(target, property);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// Otherwise, use the proxy receiver to handle the property access
|
|
389
|
-
const ref = Reflect.get(target, property, receiver);
|
|
390
|
-
|
|
391
|
-
// Binds the method's `this` context to the target object (e.g., Date.prototype.toISOString)
|
|
392
|
-
// to ensure it behaves correctly when called on the proxy.
|
|
393
|
-
if (typeof ref === 'function') {
|
|
394
|
-
return ref.bind(target);
|
|
395
|
-
}
|
|
396
|
-
return ref;
|
|
397
|
-
},
|
|
398
|
-
set(target, name) {
|
|
399
|
-
const message = (Reflect.has(target, name) ? 'update' : 'add');
|
|
400
|
-
// Notify the user.
|
|
401
|
-
throw Error(`Can not ${message} runtime configuration property: "${name}". Configuration objects are immutable unless ALLOW_CONFIG_MUTATIONS is set.`)
|
|
402
|
-
}
|
|
403
|
-
})
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// Check if property already has writable: false and configurable: false
|
|
407
|
-
const currentDescriptor = Object.getOwnPropertyDescriptor(object, propertyName);
|
|
408
|
-
if (!currentDescriptor || currentDescriptor.writable !== false || currentDescriptor.configurable !== false) {
|
|
409
|
-
Object.defineProperty(object, propertyName, {
|
|
410
|
-
value: value,
|
|
411
|
-
writable: false,
|
|
412
|
-
configurable: false
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// https://github.com/lorenwest/node-config/issues/505
|
|
419
|
-
// https://github.com/lorenwest/node-config/issues/865
|
|
420
|
-
Object.preventExtensions(object)
|
|
421
|
-
|
|
422
|
-
return object;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Return the sources for the configurations
|
|
427
|
-
*
|
|
428
|
-
* <p>
|
|
429
|
-
* All sources for configurations are stored in an array of objects containing
|
|
430
|
-
* the source name (usually the filename), the original source (as a string),
|
|
431
|
-
* and the parsed source as an object.
|
|
432
|
-
* </p>
|
|
433
|
-
*
|
|
434
|
-
* @method getConfigSources
|
|
435
|
-
* @return {import('./util').ConfigSource[]} configSources - An array of objects containing
|
|
436
|
-
* name, original, and parsed elements
|
|
437
|
-
*/
|
|
438
|
-
getConfigSources() {
|
|
439
|
-
return this.#config[LOAD_SYMBOL].getSources();
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* Load the individual file configurations.
|
|
444
|
-
*
|
|
445
|
-
* <p>
|
|
446
|
-
* This method builds a map of filename to the configuration object defined
|
|
447
|
-
* by the file. The search order is:
|
|
448
|
-
* </p>
|
|
449
|
-
*
|
|
450
|
-
* <pre>
|
|
451
|
-
* default.EXT
|
|
452
|
-
* (deployment).EXT
|
|
453
|
-
* (hostname).EXT
|
|
454
|
-
* (hostname)-(deployment).EXT
|
|
455
|
-
* local.EXT
|
|
456
|
-
* local-(deployment).EXT
|
|
457
|
-
* </pre>
|
|
458
|
-
*
|
|
459
|
-
* <p>
|
|
460
|
-
* EXT can be yml, yaml, coffee, iced, json, jsonc, cson or js signifying the file type.
|
|
461
|
-
* yaml (and yml) is in YAML format, coffee is a coffee-script, iced is iced-coffee-script,
|
|
462
|
-
* json is in JSON format, jsonc is in JSONC format, cson is in CSON format, properties is
|
|
463
|
-
* in .properties format (http://en.wikipedia.org/wiki/.properties), and js is a javascript
|
|
464
|
-
* executable file that is require()'d with module.exports being the config object.
|
|
465
|
-
* </p>
|
|
466
|
-
*
|
|
467
|
-
* <p>
|
|
468
|
-
* hostname is the $HOST environment variable (or --HOST command line parameter)
|
|
469
|
-
* if set, otherwise the $HOSTNAME environment variable (or --HOSTNAME command
|
|
470
|
-
* line parameter) if set, otherwise the hostname found from
|
|
471
|
-
* require('os').hostname().
|
|
472
|
-
* </p>
|
|
473
|
-
*
|
|
474
|
-
* <p>
|
|
475
|
-
* Once a hostname is found, everything from the first period ('.') onwards
|
|
476
|
-
* is removed. For example, abc.example.com becomes abc
|
|
477
|
-
* </p>
|
|
478
|
-
*
|
|
479
|
-
* <p>
|
|
480
|
-
* (deployment) is the deployment type, found in the $NODE_ENV environment
|
|
481
|
-
* variable (which can be overridden by using $NODE_CONFIG_ENV
|
|
482
|
-
* environment variable). Defaults to 'development'.
|
|
483
|
-
* </p>
|
|
484
|
-
*
|
|
485
|
-
* <p>
|
|
486
|
-
* If the $NODE_APP_INSTANCE environment variable (or --NODE_APP_INSTANCE
|
|
487
|
-
* command line parameter) is set, then files with this appendage will be loaded.
|
|
488
|
-
* See the Multiple Application Instances section of the main documentation page
|
|
489
|
-
* for more information.
|
|
490
|
-
* </p>
|
|
491
|
-
*
|
|
492
|
-
* @see Util.loadFileConfigs for discrete execution of most of this functionality
|
|
493
|
-
* @method loadFileConfigs
|
|
494
|
-
* @param {string=} configDir the path to the directory containing the configurations to load
|
|
495
|
-
* @param {LoadOptions=} options parsing options
|
|
496
|
-
* @return {Record<string, any>} The configuration object
|
|
497
|
-
*/
|
|
498
|
-
loadFileConfigs(configDir, options) {
|
|
499
|
-
let load = this.#config[LOAD_SYMBOL];
|
|
500
|
-
let newLoad;
|
|
501
|
-
|
|
502
|
-
if (configDir) {
|
|
503
|
-
let opts = {...load.options, configDir, ...options};
|
|
504
|
-
newLoad = new Load(opts);
|
|
505
|
-
newLoad.scan();
|
|
506
|
-
} else {
|
|
507
|
-
newLoad = new Load({...load.options, ...options});
|
|
508
|
-
_init(newLoad);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
return newLoad.config;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Attach the Config class prototype to all config objects recursively.
|
|
516
|
-
*
|
|
517
|
-
* <p>
|
|
518
|
-
* This allows you to do anything with CONFIG sub-objects as you can do with
|
|
519
|
-
* the top-level CONFIG object. It's so you can do this:
|
|
520
|
-
* </p>
|
|
521
|
-
*
|
|
522
|
-
* <pre>
|
|
523
|
-
* const CUST_CONFIG = require('config').Customer;
|
|
524
|
-
* CUST_CONFIG.get(...)
|
|
525
|
-
* </pre>
|
|
526
|
-
*
|
|
527
|
-
* @method attachProtoDeep
|
|
528
|
-
* @param {object} toObject
|
|
529
|
-
* @param {number} [depth=20]
|
|
530
|
-
* @return {object}
|
|
531
|
-
*/
|
|
532
|
-
attachProtoDeep(toObject, depth = DEFAULT_CLONE_DEPTH) {
|
|
533
|
-
if (toObject instanceof RawConfig) {
|
|
534
|
-
return toObject;
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
// Recursion detection
|
|
538
|
-
if (depth < 0) {
|
|
539
|
-
return toObject;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
// Adding Config.prototype methods directly to toObject as hidden properties
|
|
543
|
-
// because adding to toObject.__proto__ exposes the function in toObject
|
|
544
|
-
for (const fnName of ['get', 'has', 'util', LOAD_SYMBOL]) {
|
|
545
|
-
if (!toObject[fnName]) {
|
|
546
|
-
Util.makeHidden(toObject, fnName, this.#config[fnName]);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
// Add prototypes to sub-objects
|
|
551
|
-
for (const prop in toObject) {
|
|
552
|
-
if (Util.isObject(toObject[prop])) {
|
|
553
|
-
this.attachProtoDeep(toObject[prop], depth - 1);
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
// Return the original object
|
|
558
|
-
return toObject;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* <p>Get a Config Environment Variable Value</p>
|
|
563
|
-
*
|
|
564
|
-
* <p>
|
|
565
|
-
* This method returns the value of the specified config environment variable,
|
|
566
|
-
* including any defaults or overrides.
|
|
567
|
-
* </p>
|
|
568
|
-
*
|
|
569
|
-
* @method getEnv
|
|
570
|
-
* @param varName {String} The environment variable name
|
|
571
|
-
* @return {String} The value of the environment variable
|
|
572
|
-
*/
|
|
573
|
-
getEnv(varName) {
|
|
574
|
-
let load = this.#config[LOAD_SYMBOL];
|
|
575
|
-
|
|
576
|
-
return load.getEnv(varName);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
/**
|
|
580
|
-
* Returns a new deep copy of the current config object, or any part of the config if provided.
|
|
581
|
-
*
|
|
582
|
-
* @param {object} [config] The part of the config to copy and serialize. Omit this argument to return the entire config.
|
|
583
|
-
* @returns {object} The cloned config or part of the config
|
|
584
|
-
*/
|
|
585
|
-
toObject(config = this.#config) {
|
|
586
|
-
return Util.toObject(config);
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* Run strictness checks on NODE_ENV and NODE_APP_INSTANCE and throw an error if there's a problem.
|
|
591
|
-
* @param {Config} [config]
|
|
592
|
-
*/
|
|
593
|
-
runStrictnessChecks(config = this.#config) {
|
|
594
|
-
const load = config[LOAD_SYMBOL];
|
|
595
|
-
|
|
596
|
-
if (load.initParam('SUPPRESS_STRICTNESS_CHECK')) {
|
|
597
|
-
return;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
const sources = config.util.getConfigSources();
|
|
601
|
-
const sourceFilenames = sources.map(function (src) {
|
|
602
|
-
return Path.basename(src.name);
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
load.options.nodeEnv.forEach(function (env) {
|
|
606
|
-
// Throw an exception if there's no explicit config file for NODE_ENV
|
|
607
|
-
const anyFilesMatchEnv = sourceFilenames.some(function (filename) {
|
|
608
|
-
return filename.match(env);
|
|
609
|
-
});
|
|
610
|
-
// development is special-cased because it's the default value
|
|
611
|
-
if (env && (env !== 'development') && !anyFilesMatchEnv) {
|
|
612
|
-
_warnOrThrow(`${load.getEnv("nodeEnv")} value of '${env}' did not match any deployment config file names.`);
|
|
613
|
-
}
|
|
614
|
-
// Throw if NODE_ENV matches' default' or 'local'
|
|
615
|
-
if ((env === 'default') || (env === 'local')) {
|
|
616
|
-
_warnOrThrow(`${load.getEnv("nodeEnv")} value of '${env}' is ambiguous.`);
|
|
617
|
-
}
|
|
618
|
-
});
|
|
619
|
-
|
|
620
|
-
let appInstance = load.options.appInstance;
|
|
621
|
-
|
|
622
|
-
if (appInstance) {
|
|
623
|
-
// Throw an exception if there's no explicit config file for NODE_APP_INSTANCE
|
|
624
|
-
const anyFilesMatchInstance = sourceFilenames.some(function (filename) {
|
|
625
|
-
return filename.match(appInstance);
|
|
626
|
-
});
|
|
627
|
-
|
|
628
|
-
if (!anyFilesMatchInstance) {
|
|
629
|
-
_warnOrThrow(`NODE_APP_INSTANCE value of '${appInstance}' did not match any instance config file names.`);
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
function _warnOrThrow(msg) {
|
|
634
|
-
const beStrict = process.env.NODE_CONFIG_STRICT_MODE;
|
|
635
|
-
const prefix = beStrict ? 'FATAL: ' : 'WARNING: ';
|
|
636
|
-
const seeURL = 'See https://github.com/node-config/node-config/wiki/Strict-Mode';
|
|
637
|
-
|
|
638
|
-
console.error(prefix + msg);
|
|
639
|
-
console.error(prefix + seeURL);
|
|
640
|
-
|
|
641
|
-
// Accept 1 and true as truthy values. When set via process.env, Node.js casts them to strings.
|
|
642
|
-
if (["true", "1"].indexOf(beStrict) >= 0) {
|
|
643
|
-
throw new Error(prefix + msg + ' ' + seeURL);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
/**
|
|
649
|
-
* @deprecated please use Parser.stripYamlComments
|
|
650
|
-
* @param {string} fileStr The string to strip comments from
|
|
651
|
-
*/
|
|
652
|
-
stripYamlComments(fileStr) {
|
|
653
|
-
return this.#config[LOAD_SYMBOL].parser.stripYamlComments(fileStr);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
/**
|
|
658
|
-
* Scan with the default config dir (usually only at startup.
|
|
659
|
-
* This adds a bit more data from NODE_CONFIG that _load() skips
|
|
660
|
-
*
|
|
661
|
-
* @param load {Load}
|
|
662
|
-
* @private
|
|
663
|
-
*/
|
|
664
|
-
function _init(load) {
|
|
665
|
-
let options = load.options;
|
|
666
|
-
let additional = [];
|
|
667
|
-
|
|
668
|
-
// Override configurations from the $NODE_CONFIG environment variable
|
|
669
|
-
let envConfig = {};
|
|
670
|
-
|
|
671
|
-
load.setEnv("CONFIG_DIR", options.configDir);
|
|
672
|
-
|
|
673
|
-
if (process.env.NODE_CONFIG) {
|
|
674
|
-
try {
|
|
675
|
-
envConfig = JSON.parse(process.env.NODE_CONFIG);
|
|
676
|
-
} catch(e) {
|
|
677
|
-
console.error('The $NODE_CONFIG environment variable is malformed JSON');
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
additional.push({ name: "$NODE_CONFIG", config: envConfig });
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
// Override configurations from the --NODE_CONFIG command line
|
|
684
|
-
let cmdLineConfig = load.getCmdLineArg('NODE_CONFIG');
|
|
685
|
-
if (cmdLineConfig) {
|
|
686
|
-
try {
|
|
687
|
-
cmdLineConfig = JSON.parse(cmdLineConfig);
|
|
688
|
-
} catch(e) {
|
|
689
|
-
console.error('The --NODE_CONFIG={json} command line argument is malformed JSON');
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
additional.push({ name: "--NODE_CONFIG argument", config: cmdLineConfig });
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
// Place the mixed NODE_CONFIG into the environment
|
|
696
|
-
load.setEnv('NODE_CONFIG', JSON.stringify(Util.extendDeep(envConfig, cmdLineConfig, {})));
|
|
697
|
-
|
|
698
|
-
load.scan(additional);
|
|
699
|
-
}
|
|
1
|
+
/** @typedef {import('./config.mjs').Config} Config */
|
|
700
2
|
|
|
701
3
|
/** @type {Config} */
|
|
702
|
-
module.exports = (
|
|
703
|
-
const load = Load.fromEnvironment();
|
|
704
|
-
|
|
705
|
-
_init(load);
|
|
706
|
-
|
|
707
|
-
// Instantiate and export the configuration
|
|
708
|
-
const config = new ConfigClass(load);
|
|
709
|
-
|
|
710
|
-
// Produce warnings if the configuration is empty
|
|
711
|
-
const showWarnings = !(load.initParam('SUPPRESS_NO_CONFIG_WARNING'));
|
|
712
|
-
|
|
713
|
-
if (showWarnings && Object.keys(config).length === 0) {
|
|
714
|
-
console.error('WARNING: No configurations found in configuration directory:' + load.options.configDir);
|
|
715
|
-
console.error('WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.');
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
return config;
|
|
719
|
-
})();
|
|
4
|
+
module.exports = require('./config.mjs').default;
|