fable-settings
Advanced tools
Comparing version 2.0.1 to 2.0.3
{ | ||
"name": "fable-settings", | ||
"version": "2.0.1", | ||
"version": "2.0.3", | ||
"description": "A simple, tolerant configuration chain.", | ||
@@ -8,3 +8,3 @@ "main": "source/Fable-Settings.js", | ||
"start": "node source/Fable-Settings.js", | ||
"coverage": "./node_modules/nyc/bin/nyc.js --reporter=lcov --reporter=text-lcov ./node_modules/mocha/bin/_mocha -- -u tdd -R spec", | ||
"coverage": "nyc npm run test --reporter=lcov", | ||
"test": "./node_modules/mocha/bin/_mocha -u tdd -R spec" | ||
@@ -41,3 +41,5 @@ }, | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"match-all": "^1.2.6" | ||
} | ||
} |
@@ -10,2 +10,5 @@ /** | ||
// needed since String.matchAll wasn't added to node until v12 | ||
const libMatchAll = require('match-all'); | ||
/** | ||
@@ -27,2 +30,5 @@ * Fable Solution Settings | ||
// default environment variable templating to on | ||
this._PerformEnvTemplating = !tmpSettings || tmpSettings.NoEnvReplacement !== true; | ||
// The base settings object (what they were on initialization, before other actors have altered them) | ||
@@ -69,5 +75,39 @@ this.base = JSON.parse(JSON.stringify(tmpSettings)); | ||
{ | ||
return JSON.parse(JSON.stringify(require('./Fable-Settings-Default'))) | ||
return JSON.parse(JSON.stringify(require('./Fable-Settings-Default'))); | ||
} | ||
// Resolve (recursive) any environment variables found in settings object. | ||
_resolveEnv(pSettings) | ||
{ | ||
for (const tmpKey in pSettings) | ||
{ | ||
const tmpValue = pSettings[tmpKey]; | ||
if (typeof(tmpValue) === 'object') // && !Array.isArray(tmpValue)) | ||
{ | ||
this._resolveEnv(tmpValue); | ||
} | ||
else if (typeof(tmpValue) === 'string') | ||
{ | ||
if (tmpValue.indexOf('${') >= 0) | ||
{ | ||
//pick out and resolve env constiables from the settings value. | ||
const tmpMatches = libMatchAll(tmpValue, /\$\{(.*?)\}/g).toArray(); | ||
tmpMatches.forEach((tmpMatch) => | ||
{ | ||
//format: VAR_NAME|DEFAULT_VALUE | ||
const tmpParts = tmpMatch.split('|'); | ||
let tmpResolvedValue = process.env[tmpParts[0]] || ''; | ||
if (!tmpResolvedValue && tmpParts.length > 1) | ||
{ | ||
tmpResolvedValue = tmpParts[1]; | ||
} | ||
pSettings[tmpKey] = pSettings[tmpKey].replace('${' + tmpMatch + '}', tmpResolvedValue); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
// Merge some new object into the existing settings. | ||
@@ -81,4 +121,11 @@ merge(pSettingsFrom, pSettingsTo) | ||
tmpSettingsTo = Object.assign(tmpSettingsTo, tmpSettingsFrom); | ||
// do not mutate the From object property values | ||
let tmpSettingsFromCopy = JSON.parse(JSON.stringify(tmpSettingsFrom)); | ||
tmpSettingsTo = Object.assign(tmpSettingsTo, tmpSettingsFromCopy); | ||
if (this._PerformEnvTemplating) | ||
{ | ||
this._resolveEnv(tmpSettingsTo); | ||
} | ||
return tmpSettingsTo; | ||
@@ -85,0 +132,0 @@ } |
@@ -18,2 +18,6 @@ { | ||
"Environment": "${NOT_DEFAULT|default}-${USE_DEFAULT|default}", | ||
"EnvArray": [ "${NOT_DEFAULT|default}", "${USE_DEFAULT|default}" ], | ||
"MySQL": | ||
@@ -30,2 +34,2 @@ { | ||
"LogStreams": [{"streamtype":"process.stdout", "level":"trace"}] | ||
} | ||
} |
@@ -171,5 +171,54 @@ /** | ||
); | ||
test | ||
( | ||
'resolve environment variables', | ||
function() | ||
{ | ||
process.env['NOT_DEFAULT'] = 'found_value'; | ||
const tmpFableSettings = require('../source/Fable-Settings.js').new( | ||
{ | ||
DefaultConfigFile: `${__dirname}/DefaultExampleSettings.json`, | ||
ConfigFile: `${__dirname}/ExampleSettings.json` | ||
}); | ||
Expect(tmpFableSettings).to.have.a.property('settings') | ||
.that.is.a('object'); | ||
Expect(tmpFableSettings.settings).to.have.a.property('Environment') | ||
.that.is.a('string'); | ||
Expect(tmpFableSettings.settings.Environment) | ||
.to.equal('found_value-default'); | ||
Expect(tmpFableSettings.settings).to.have.a.property('EnvArray') | ||
.that.is.an('array'); | ||
Expect(tmpFableSettings.settings.EnvArray) | ||
.to.deep.equal(['found_value', 'default']); | ||
} | ||
); | ||
test | ||
( | ||
'ignores environment variables if disabled', | ||
function() | ||
{ | ||
process.env['NOT_DEFAULT'] = 'found_value'; | ||
const tmpFableSettings = require('../source/Fable-Settings.js').new( | ||
{ | ||
NoEnvReplacement: true, | ||
DefaultConfigFile: `${__dirname}/DefaultExampleSettings.json`, | ||
ConfigFile: `${__dirname}/ExampleSettings.json` | ||
}); | ||
Expect(tmpFableSettings).to.have.a.property('settings') | ||
.that.is.a('object'); | ||
Expect(tmpFableSettings.settings).to.have.a.property('Environment') | ||
.that.is.a('string'); | ||
Expect(tmpFableSettings.settings.Environment) | ||
.to.equal('${NOT_DEFAULT|default}-${USE_DEFAULT|default}'); | ||
Expect(tmpFableSettings.settings).to.have.a.property('EnvArray') | ||
.that.is.an('array'); | ||
Expect(tmpFableSettings.settings.EnvArray) | ||
.to.deep.equal(['${NOT_DEFAULT|default}', '${USE_DEFAULT|default}']); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
); | ||
); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37033
570
1
11
+ Addedmatch-all@^1.2.6
+ Addedmatch-all@1.2.6(transitive)