haraka-config
Advanced tools
Comparing version 1.0.1 to 1.0.2
'use strict'; | ||
var path = require('path'); | ||
var path = require('path'); | ||
var cfreader = require('./configfile'); | ||
var cfreader = require('./configfile'); | ||
@@ -11,2 +11,3 @@ module.exports = new Config(); | ||
this.root_path = root_path || cfreader.config_path; | ||
// console.log('root_path: ' + this.root_path); | ||
this.module_config = function (defaults_path, overrides_path) { | ||
@@ -21,3 +22,3 @@ var cfg = new Config(path.join(defaults_path, 'config')); | ||
Config.prototype.get = function(name, type, cb, options) { | ||
Config.prototype.get = function (name, type, cb, options) { | ||
var a = this.arrange_args([name, type, cb, options]); | ||
@@ -24,0 +25,0 @@ if (!a[1]) a[1] = 'value'; |
@@ -23,6 +23,35 @@ 'use strict'; | ||
cfreader.config_path = process.env.HARAKA ? | ||
path.join(process.env.HARAKA, 'config') | ||
: path.join(__dirname, './config'); | ||
var config_dir_candidates = [ | ||
path.join(__dirname, 'config'), // Haraka ./config dir | ||
__dirname, // npm packaged plugins | ||
]; | ||
function get_path_to_config_dir () { | ||
if (process.env.HARAKA) { | ||
// console.log('process.env.HARAKA: ' + process.env.HARAKA); | ||
cfreader.config_path = path.join(process.env.HARAKA, 'config'); | ||
return; | ||
} | ||
if (process.env.NODE_ENV === 'test') { | ||
cfreader.config_path = path.join(__dirname, 'test', 'config'); | ||
return; | ||
} | ||
for (var i=0; i < config_dir_candidates.length; i++) { | ||
try { | ||
var stat = fs.statSync(config_dir_candidates[i]); | ||
if (stat && stat.isDirectory()) { | ||
cfreader.config_path = config_dir_candidates[i]; | ||
return; | ||
} | ||
} | ||
catch (ignore) { | ||
console.error(ignore.message); | ||
} | ||
} | ||
} | ||
get_path_to_config_dir(); | ||
// console.log('cfreader.config_path: ' + cfreader.config_path); | ||
cfreader.watch_files = true; | ||
@@ -161,10 +190,6 @@ cfreader._config_cache = {}; | ||
var cache_key = cfreader.get_cache_key(name, options); | ||
// console.log('\tcache_key: ' + cache_key); | ||
if (cfreader._config_cache[cache_key]) { | ||
var cached = cfreader._config_cache[cache_key]; | ||
// Make sure that any .ini file booleans are applied | ||
if (type === 'ini' && (options && options.booleans && | ||
Array.isArray(options.booleans))) | ||
{ | ||
cfreader.init_booleans(options, cached); | ||
} | ||
// console.log('\t' + name + ' is cached'); | ||
return cached; | ||
@@ -249,2 +274,3 @@ } | ||
var cache_key = cfreader.get_cache_key(name, options); | ||
try { | ||
@@ -258,3 +284,3 @@ switch (type) { | ||
result = cfrType.load(name); | ||
cfreader.process_file_overrides(name, result); | ||
cfreader.process_file_overrides(name, options, result); | ||
break; | ||
@@ -265,7 +291,8 @@ // case 'binary': | ||
} | ||
cfreader._config_cache[cache_key] = result; | ||
} | ||
catch (err) { | ||
if (err.code !== 'EBADF') throw err; | ||
if (cfreader._config_cache[name]) { | ||
result = cfreader._config_cache[name]; | ||
if (cfreader._config_cache[cache_key]) { | ||
result = cfreader._config_cache[cache_key]; | ||
} | ||
@@ -276,3 +303,3 @@ } | ||
cfreader.process_file_overrides = function (name, result) { | ||
cfreader.process_file_overrides = function (name, options, result) { | ||
// We might be re-loading this file: | ||
@@ -282,4 +309,5 @@ // * build a list of cached overrides | ||
var cp = cfreader.config_path; | ||
if (cfreader._config_cache[name]) { | ||
var ck_keys = Object.keys(cfreader._config_cache[name]); | ||
var cache_key = cfreader.get_cache_key(name, options); | ||
if (cfreader._config_cache[cache_key]) { | ||
var ck_keys = Object.keys(cfreader._config_cache[cache_key]); | ||
for (var i=0; i<ck_keys.length; i++) { | ||
@@ -286,0 +314,0 @@ if (ck_keys[i].substr(0,1) !== '!') continue; |
@@ -6,3 +6,3 @@ { | ||
"description": "Haraka's config file loader", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"homepage": "http://haraka.github.io", | ||
@@ -9,0 +9,0 @@ "repository": { |
'use strict'; | ||
var path = require('path'); | ||
process.env.NODE_ENV = 'test' | ||
var config = require('../config'); | ||
var path = require('path'); | ||
@@ -10,12 +10,62 @@ var cb = function () { return false; }; | ||
exports.module_config = { | ||
function clearRequireCache () { | ||
// node_unit runs all the tests in the same process, so the process.env | ||
// changes affect other tests. Icky. Work around by invalidating | ||
// the require cache, so config and configfile re-initialize | ||
delete require.cache[ | ||
path.resolve(__dirname, '../config') + '.js' | ||
]; | ||
delete require.cache[ | ||
path.resolve(__dirname, '../configfile') + '.js' | ||
]; | ||
} | ||
function setUp (done) { | ||
process.env.HARAKA = ''; | ||
clearRequireCache(); | ||
this.config = require('../config'); | ||
done(); | ||
}; | ||
exports.config = { | ||
'setUp' : setUp, | ||
'new' : function (test) { | ||
test.expect(1); | ||
var c = config.module_config('foo', 'bar'); | ||
test.ok(c.root_path); | ||
test.ok(/haraka\-config\/test\/config$/.test(this.config.root_path)); | ||
test.done(); | ||
} | ||
}, | ||
'module_config' : function (test) { | ||
test.expect(2); | ||
var c = this.config.module_config('foo', 'bar'); | ||
test.equal(c.root_path, 'foo/config'); | ||
test.equal(c.overrides_path, 'bar/config'); | ||
test.done(); | ||
}, | ||
}; | ||
exports.config_path = { | ||
'config_path process.env.HARAKA': function (test) { | ||
test.expect(1); | ||
process.env.HARAKA = '/tmp'; | ||
clearRequireCache(); | ||
var config = require('../config'); | ||
// console.log(config); | ||
test.equal(config.root_path, '/tmp/config'); | ||
test.done(); | ||
}, | ||
'config_path process.env.NODE_ENV': function (test) { | ||
test.expect(1); | ||
process.env.HARAKA = ''; | ||
process.env.NODE_ENV = 'not-test'; | ||
clearRequireCache(); | ||
var config = require('../config'); | ||
// ./config doesn't exist so path will be resolved ./ | ||
test.ok(/haraka\-config$/.test(config.root_path)); | ||
process.env.NODE_ENV = 'test'; | ||
test.done(); | ||
}, | ||
}; | ||
exports.arrange_args = { | ||
'setUp' : setUp, | ||
// config.get('name'); | ||
@@ -25,3 +75,3 @@ 'name' : function (test) { | ||
test.deepEqual( | ||
config.arrange_args(['test.ini']), | ||
this.config.arrange_args(['test.ini']), | ||
['test.ini', 'ini', undefined, undefined]); | ||
@@ -34,3 +84,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','ini']), | ||
this.config.arrange_args(['test.ini','ini']), | ||
['test.ini', 'ini', undefined, undefined]); | ||
@@ -43,3 +93,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini',cb]), | ||
this.config.arrange_args(['test.ini',cb]), | ||
['test.ini', 'ini', cb, undefined]); | ||
@@ -52,3 +102,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini',cb,opts]), | ||
this.config.arrange_args(['test.ini',cb,opts]), | ||
['test.ini', 'ini', cb, opts]); | ||
@@ -61,3 +111,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini',opts]), | ||
this.config.arrange_args(['test.ini',opts]), | ||
['test.ini', 'ini', undefined, opts]); | ||
@@ -70,3 +120,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','ini',cb]), | ||
this.config.arrange_args(['test.ini','ini',cb]), | ||
['test.ini', 'ini', cb, undefined]); | ||
@@ -79,3 +129,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','ini',opts]), | ||
this.config.arrange_args(['test.ini','ini',opts]), | ||
['test.ini', 'ini', undefined, opts]); | ||
@@ -88,3 +138,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','ini',cb, opts]), | ||
this.config.arrange_args(['test.ini','ini',cb, opts]), | ||
['test.ini', 'ini', cb, opts]); | ||
@@ -97,3 +147,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','list',cb, opts]), | ||
this.config.arrange_args(['test.ini','list',cb, opts]), | ||
['test.ini', 'list', cb, opts]); | ||
@@ -106,3 +156,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','binary',cb, opts]), | ||
this.config.arrange_args(['test.ini','binary',cb, opts]), | ||
['test.ini', 'binary', cb, opts]); | ||
@@ -115,3 +165,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','value',cb, opts]), | ||
this.config.arrange_args(['test.ini','value',cb, opts]), | ||
['test.ini', 'value', cb, opts]); | ||
@@ -124,3 +174,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','json',cb, opts]), | ||
this.config.arrange_args(['test.ini','json',cb, opts]), | ||
['test.ini', 'json', cb, opts]); | ||
@@ -133,3 +183,3 @@ test.done(); | ||
test.deepEqual( | ||
config.arrange_args(['test.ini','data',cb, opts]), | ||
this.config.arrange_args(['test.ini','data',cb, opts]), | ||
['test.ini', 'data', cb, opts]); | ||
@@ -172,3 +222,5 @@ test.done(); | ||
test.expect(1); | ||
test.deepEqual(config.get(name, type, callback, options), expected); | ||
var config = require('../config'); | ||
var cfg = config.get(name, type, callback, options); | ||
test.deepEqual(cfg, expected); | ||
test.done(); | ||
@@ -178,13 +230,17 @@ } | ||
exports.get = { | ||
'setUp' : setUp, | ||
// config.get('name'); | ||
'name=test (non-existing)' : function (test) { | ||
'test (non-existing)' : function (test) { | ||
_test_get(test, 'test', null, null, null, null); | ||
}, | ||
'test (non-existing, cached)' : function (test) { | ||
test.expect(1); | ||
var cfg = this.config.get('test', null, null); | ||
test.deepEqual(cfg, null); | ||
test.done(); | ||
}, | ||
// config.get('name.ini'); | ||
'name.ini' : function (test) { | ||
_test_get(test, 'test.ini', null, null, null, { "main": {} }); | ||
}, | ||
// config.get('test.ini'); | ||
'test.ini, no opts' : function (test) { | ||
_test_get(test, '../test/config/test.ini', null, null, null, { | ||
_test_get(test, 'test.ini', null, null, null, { | ||
main: { bool_true: 'true', bool_false: 'false', str_true: 'true', str_false: 'false' }, | ||
@@ -210,3 +266,3 @@ sect1: { bool_true: 'true', bool_false: 'false', str_true: 'true', str_false: 'false' }, | ||
'test.flat, type=' : function (test) { | ||
_test_get(test, '../test/config/test.flat', null, null, null, 'line1'); | ||
_test_get(test, 'test.flat', null, null, null, 'line1'); | ||
}, | ||
@@ -219,7 +275,7 @@ | ||
'test.flat, type=value' : function (test) { | ||
_test_get(test, '../test/config/test.value', 'value', null, null, 'line1'); | ||
_test_get(test, 'test.value', 'value', null, null, 'line1'); | ||
}, | ||
// config.get('test.flat', 'list'); | ||
'test.flat, type=list' : function (test) { | ||
_test_get(test, '../test/config/test.list', 'list', null, null, | ||
_test_get(test, 'test.list', 'list', null, null, | ||
['line1', 'line2','line3', 'line5'] ); | ||
@@ -229,3 +285,3 @@ }, | ||
'test.flat, type=data' : function (test) { | ||
_test_get(test, '../test/config/test.data', 'data', null, null, | ||
_test_get(test, 'test.data', 'data', null, null, | ||
['line1', 'line2','line3', '', 'line5'] ); | ||
@@ -236,7 +292,7 @@ }, | ||
'test.json, type=' : function (test) { | ||
_test_get(test, '../test/config/test.json', null, null, null, jsonRes); | ||
_test_get(test, 'test.json', null, null, null, jsonRes); | ||
}, | ||
// config.get('test.json', 'json'); | ||
'test.json, type=json' : function (test) { | ||
_test_get(test, '../test/config/test.json', 'json', null, null, jsonRes); | ||
_test_get(test, 'test.json', 'json', null, null, jsonRes); | ||
}, | ||
@@ -246,11 +302,11 @@ | ||
'test.yaml, type=' : function (test) { | ||
_test_get(test, '../test/config/test.yaml', null, null, null, yamlRes); | ||
_test_get(test, 'test.yaml', null, null, null, yamlRes); | ||
}, | ||
// config.get('test.yaml', 'yaml'); | ||
'test.yaml, type=yaml' : function (test) { | ||
_test_get(test, '../test/config/test.yaml', 'yaml', null, null, yamlRes); | ||
_test_get(test, 'test.yaml', 'yaml', null, null, yamlRes); | ||
}, | ||
// config.get('missing.json'); | ||
'missing.yaml, asked for json' : function (test) { | ||
_test_get(test, '../test/config/missing.json', 'json', null, null, {"matt": "waz here"}); | ||
_test_get(test, 'missing.json', 'json', null, null, {"matt": "waz here"}); | ||
}, | ||
@@ -261,3 +317,3 @@ | ||
test.expect(2); | ||
var res = config.get('../test/config/test.binary', 'binary'); | ||
var res = this.config.get('test.binary', 'binary'); | ||
test.equal(res.length, 120); | ||
@@ -270,8 +326,9 @@ test.ok(Buffer.isBuffer(res)); | ||
exports.merged = { | ||
'setUp' : setUp, | ||
'before_merge' : function (test) { | ||
test.expect(1); | ||
this.config = require('../config').module_config( | ||
var lc = this.config.module_config( | ||
path.join('test','default') | ||
); | ||
test.deepEqual(this.config.get('test.ini'), | ||
test.deepEqual(lc.get('test.ini'), | ||
{ main: {}, defaults: { one: 'one', two: 'two' } } | ||
@@ -283,7 +340,7 @@ ); | ||
test.expect(1); | ||
this.config = require('../config').module_config( | ||
var lc = this.config.module_config( | ||
path.join('test','default'), | ||
path.join('test','override') | ||
); | ||
test.deepEqual(this.config.get('test.ini'), | ||
test.deepEqual(lc.get('test.ini'), | ||
{ main: {}, defaults: { one: 'three', two: 'four' } } | ||
@@ -295,10 +352,9 @@ ); | ||
test.expect(1); | ||
this.config = require('../config').module_config( | ||
var lc = this.config.module_config( | ||
path.join('test','default'), | ||
path.join('test','override') | ||
); | ||
var cfg = this.config.get('test.flat'); | ||
test.equal(cfg, 'flatoverrode'); | ||
test.equal(lc.get('test.flat'), 'flatoverrode'); | ||
test.done(); | ||
}, | ||
} |
'use strict'; | ||
process.env.NODE_ENV === 'test'; | ||
var _set_up = function (done) { | ||
@@ -4,0 +6,0 @@ this.cfreader = require('../configfile'); |
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 1 instance 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
65474
1514
19