Comparing version
@@ -290,3 +290,28 @@ /* | ||
// | ||
// ### function required (keys) | ||
// #### @keys {array} List of keys | ||
// Throws an error if any of `keys` has no value, otherwise returns `true` | ||
Provider.prototype.required = function (keys) { | ||
if (!Array.isArray(keys)) { | ||
throw new Error('Incorrect parameter, array expected'); | ||
} | ||
var missing = []; | ||
keys.forEach(function(key) { | ||
if (typeof this.get(key) === 'undefined') { | ||
missing.push(key); | ||
} | ||
}, this); | ||
if (missing.length) { | ||
throw new Error('Missing required keys: ' + missing.join(', ')); | ||
} else { | ||
return true; | ||
} | ||
}; | ||
// | ||
// ### function reset (callback) | ||
@@ -293,0 +318,0 @@ // #### @callback {function} **Optional** Continuation to respond to when complete. |
@@ -12,2 +12,3 @@ /* | ||
util = require('util'), | ||
Secure = require('secure-keys'), | ||
formats = require('../formats'), | ||
@@ -53,2 +54,8 @@ Memory = require('./memory').Memory, | ||
} | ||
this.keys = new Secure({ | ||
secret: this.secure.secret, | ||
alg: this.secure.alg, | ||
format: this.format | ||
}); | ||
} | ||
@@ -171,15 +178,3 @@ | ||
if (this.secure) { | ||
data = Object.keys(data).reduce(function (acc, key) { | ||
var value = self.format.stringify(data[key]); | ||
acc[key] = { | ||
alg: self.secure.alg, | ||
value: cipherConvert(value, { | ||
alg: self.secure.alg, | ||
secret: self.secure.secret, | ||
encs: { input: 'utf8', output: 'hex' } | ||
}) | ||
} | ||
return acc; | ||
}, {}); | ||
data = this.keys.encrypt(data); | ||
} | ||
@@ -203,14 +198,7 @@ | ||
return Object.keys(parsed).reduce(function (acc, key) { | ||
var decrypted = cipherConvert(parsed[key].value, { | ||
alg: parsed[key].alg || self.secure.alg, | ||
secret: self.secure.secret, | ||
encs: { input: 'hex', output: 'utf8' } | ||
}); | ||
return this.keys.decrypt(parsed); | ||
acc[key] = self.format.parse(decrypted); | ||
return acc; | ||
}, {}); | ||
}; | ||
// | ||
@@ -306,13 +294,1 @@ // ### function search (base) | ||
}; | ||
// | ||
// ### function cipherConvert (contents, opts) | ||
// Returns the result of the cipher operation | ||
// on the contents contents. | ||
// | ||
function cipherConvert(contents, opts) { | ||
var encs = opts.encs; | ||
var cipher = crypto.createCipher(opts.alg, opts.secret); | ||
return cipher.update(contents, encs.input, encs.output) | ||
+ cipher.final(encs.output); | ||
} |
{ | ||
"name": "nconf", | ||
"description": "Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"author": "Charlie Robbins <charlie.robbins@gmail.com>", | ||
@@ -18,5 +18,8 @@ "repository": { | ||
"ini": "^1.3.0", | ||
"secure-keys": "^1.0.0", | ||
"yargs": "^3.19.0" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.4", | ||
"istanbul": "^0.4.1", | ||
"vows": "0.8.x" | ||
@@ -26,3 +29,5 @@ }, | ||
"scripts": { | ||
"test": "vows test/*-test.js test/**/*-test.js --spec" | ||
"test": "vows test/*-test.js test/**/*-test.js --spec", | ||
"cover": "istanbul cover vows -- test/*-test.js test/**/*-test.js --spec", | ||
"coveralls": "cat coverage/lcov.info | coveralls" | ||
}, | ||
@@ -29,0 +34,0 @@ "engines": { |
# nconf | ||
[](https://www.npmjs.com/package/nconf)[](https://www.npmjs.com/package/nconf)[](https://travis-ci.org/indexzero/nconf)[](https://david-dm.org/indexzero/nconf) | ||
[](https://www.npmjs.com/package/nconf)[](https://www.npmjs.com/package/nconf)[](https://travis-ci.org/indexzero/nconf)[](https://coveralls.io/github/indexzero/nconf)[](https://david-dm.org/indexzero/nconf) | ||
@@ -152,2 +152,14 @@ Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging. | ||
nconf.remove('file'); | ||
```y | ||
### nconf.required(keys) | ||
Declares a set of string keys to be mandatory, and throw an error if any are missing. | ||
```js | ||
nconf.defaults({ | ||
keya: 'a', | ||
}); | ||
nconf.required(['keya', 'keyb']); | ||
// Error: Missing required keys: keyb | ||
``` | ||
@@ -154,0 +166,0 @@ |
@@ -27,2 +27,3 @@ /* | ||
assert.isFunction(nconf.reset); | ||
assert.isFunction(nconf.required); | ||
}, | ||
@@ -45,2 +46,12 @@ "the use() method": { | ||
} | ||
}, | ||
"the required() method": { | ||
"should throw error with missing keys": function() { | ||
nconf.set('foo:bar:bazz', 'buzz'); | ||
assert.throws(nconf.required.bind(nconf, ['missing', 'foo:bar:bazz']), Error); | ||
}, | ||
"should return true if all required keys exist": function() { | ||
nconf.set('foo:bar:bazz', 'buzz'); | ||
assert.isTrue(nconf.required(['foo:bar:bazz'])); | ||
} | ||
} | ||
@@ -47,0 +58,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
126425
0.7%2730
0.26%311
4.01%4
33.33%3
200%+ Added
+ Added