Comparing version 0.2.1 to 0.2.2
40
index.js
@@ -34,3 +34,7 @@ function habitat(prefix, defaults) { | ||
var value = process.env[envkey] || someDefault; | ||
return habitat.parse(value); | ||
if (typeof value !== 'undefined') | ||
return habitat.parse(value); | ||
value = this.getAsObject(key); | ||
if (Object.keys(value).length) | ||
return value; | ||
}; | ||
@@ -135,9 +139,5 @@ | ||
var prefix = this.prefix; | ||
var keys = Object.keys(process.env); | ||
if (!prefix) return process.env; | ||
return keys.reduce(function (accum, key) { | ||
if (key.indexOf(prefix) === 0) | ||
accum.push(key) | ||
return accum; | ||
}, []).reduce(function (obj, key) { | ||
var keys = this.rawKeys(); | ||
return keys.reduce(function (obj, key) { | ||
var lowerKey = key.replace(prefix + '_', '').toLowerCase(); | ||
@@ -150,3 +150,29 @@ obj[lowerKey] = habitat.parse(process.env[key]); | ||
habitat.prototype.rawKeys = function rawKeys() { | ||
var prefix = this.prefix; | ||
var keys = Object.keys(process.env); | ||
if (!prefix) return keys; | ||
return keys.reduce(function (accum, key) { | ||
if (key.indexOf(prefix) === 0) | ||
accum.push(key); | ||
return accum; | ||
}, []); | ||
}; | ||
/** | ||
* Get an object by key | ||
* | ||
* @param {String} keyPrefix | ||
* @return {Object} | ||
*/ | ||
habitat.prototype.getAsObject = function getAsObject(keyPrefix) { | ||
var envkey = this.envkey(keyPrefix); | ||
var env = new habitat(envkey); | ||
return env.all(); | ||
}; | ||
/** | ||
* Get a key from the environment without a prefix. | ||
@@ -153,0 +179,0 @@ * |
{ | ||
"name": "habitat", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Small library for managing environment variables", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -67,3 +67,3 @@ # habitat [](http://travis-ci.org/brianloveswords/habitat) | ||
```bash | ||
exports APP_ADMINS='["me@example.com", "you@example.com"]'; | ||
exports APP_ADMINS='["me@example.com", "you@example.com"]' | ||
``` | ||
@@ -79,2 +79,18 @@ ```js | ||
You can also do stuff like this: | ||
```bash | ||
exports APP_DB="redis" | ||
exports APP_REDIS_HOST="127.0.0.1" | ||
exports APP_REDIS_PORT=6379 | ||
``` | ||
```js | ||
var env = new habitat('app'); | ||
var db = env.get('db'); | ||
var options = env.get('redis'); | ||
console.log(db); // "redis" | ||
console.log(options.host); // "127.0.0.1" | ||
console.log(options.port); // 6379 | ||
``` | ||
## habitat#set(key, value) | ||
@@ -81,0 +97,0 @@ |
@@ -129,1 +129,33 @@ var fs = require('fs'); | ||
}); | ||
test('habitat#rawKeys', function (t) { | ||
process.env['OTHERAPP_HOST'] = 'not-local-host'; | ||
process.env['RAWKEYS_HOST'] = 'localhost'; | ||
process.env['RAWKEYS_PORT'] = 3000; | ||
var env = new habitat('rawkeys'); | ||
t.same(env.rawKeys(), ['RAWKEYS_HOST', 'RAWKEYS_PORT']) | ||
t.end(); | ||
}); | ||
test('habitat#getAsObject', function (t) { | ||
process.env['APP_REDIS_HOST'] = 'localhost'; | ||
process.env['APP_REDIS_PORT'] = 3000; | ||
var env = new habitat('app'); | ||
var obj = env.getAsObject('redis'); | ||
t.same(obj.host, 'localhost'); | ||
t.same(obj.port, 3000); | ||
t.end(); | ||
}); | ||
test('habitat#get: should try `getAsObject` if no value is found', function (t) { | ||
process.env['APP_REDIS_HOST'] = 'localhost'; | ||
process.env['APP_REDIS_PORT'] = 3000; | ||
var env = new habitat('app'); | ||
var obj = env.get('redis'); | ||
t.same(obj.host, 'localhost'); | ||
t.same(obj.port, 3000); | ||
var value = env.get('nonexistent'); | ||
t.notOk(value, 'should not have a value'); | ||
t.end(); | ||
}); |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 5 instances in 1 package
13860
293
160
21