2011-11-20 21:21:09 +00:00
|
|
|
/*
|
|
|
|
* env.js: Simple memory-based store for environment variables
|
|
|
|
*
|
2014-11-26 06:31:48 +00:00
|
|
|
* (C) 2011, Charlie Robbins and the Contributors.
|
2011-11-20 21:21:09 +00:00
|
|
|
*
|
|
|
|
*/
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
var util = require('util'),
|
2012-06-21 06:46:10 +00:00
|
|
|
common = require('../common'),
|
2011-11-20 21:21:09 +00:00
|
|
|
Memory = require('./memory').Memory;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
//
|
|
|
|
// ### function Env (options)
|
|
|
|
// #### @options {Object} Options for this instance.
|
|
|
|
// Constructor function for the Env nconf store, a simple abstraction
|
|
|
|
// around the Memory store that can read process environment variables.
|
|
|
|
//
|
|
|
|
var Env = exports.Env = function (options) {
|
|
|
|
Memory.call(this, options);
|
|
|
|
|
2012-06-21 06:46:10 +00:00
|
|
|
options = options || {};
|
|
|
|
this.type = 'env';
|
2017-11-13 04:11:01 +00:00
|
|
|
this.readOnly = options.readOnly !== undefined? options.readOnly : true;
|
2012-06-21 08:05:52 +00:00
|
|
|
this.whitelist = options.whitelist || [];
|
2012-06-21 06:46:10 +00:00
|
|
|
this.separator = options.separator || '';
|
2015-08-04 10:56:43 +00:00
|
|
|
this.lowerCase = options.lowerCase || false;
|
2017-10-22 01:57:31 +00:00
|
|
|
this.parseValues = options.parseValues || false;
|
2017-10-26 02:57:58 +00:00
|
|
|
this.transform = options.transform || false;
|
2014-11-26 06:28:31 +00:00
|
|
|
|
2015-08-04 16:54:59 +00:00
|
|
|
if (({}).toString.call(options.match) === '[object RegExp]'
|
2014-11-26 06:28:31 +00:00
|
|
|
&& typeof options !== 'string') {
|
|
|
|
this.match = options.match;
|
|
|
|
}
|
|
|
|
|
2012-06-21 06:46:10 +00:00
|
|
|
if (options instanceof Array) {
|
2012-06-21 08:04:37 +00:00
|
|
|
this.whitelist = options;
|
2012-06-21 06:46:10 +00:00
|
|
|
}
|
2017-11-05 01:30:56 +00:00
|
|
|
if (typeof(options) === 'string' || options instanceof RegExp) {
|
2012-06-21 07:18:22 +00:00
|
|
|
this.separator = options;
|
|
|
|
}
|
2011-11-20 21:21:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Inherit from the Memory store
|
|
|
|
util.inherits(Env, Memory);
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function loadSync ()
|
|
|
|
// Loads the data passed in from `process.env` into this instance.
|
|
|
|
//
|
|
|
|
Env.prototype.loadSync = function () {
|
|
|
|
this.loadEnv();
|
|
|
|
return this.store;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function loadEnv ()
|
|
|
|
// Loads the data passed in from `process.env` into this instance.
|
|
|
|
//
|
|
|
|
Env.prototype.loadEnv = function () {
|
|
|
|
var self = this;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2015-08-04 10:56:43 +00:00
|
|
|
var env = process.env;
|
|
|
|
|
|
|
|
if (this.lowerCase) {
|
2016-03-01 17:08:35 +00:00
|
|
|
env = {};
|
|
|
|
Object.keys(process.env).forEach(function (key) {
|
|
|
|
env[key.toLowerCase()] = process.env[key];
|
2015-08-04 10:56:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-26 02:57:58 +00:00
|
|
|
if (this.transform) {
|
|
|
|
env = common.transform(env, this.transform);
|
|
|
|
}
|
|
|
|
|
2017-11-13 04:11:01 +00:00
|
|
|
var tempWrite = false;
|
|
|
|
|
|
|
|
if(this.readOnly) {
|
|
|
|
this.readOnly = false;
|
|
|
|
tempWrite = true;
|
|
|
|
}
|
|
|
|
|
2015-08-04 10:56:43 +00:00
|
|
|
Object.keys(env).filter(function (key) {
|
2014-11-26 06:06:44 +00:00
|
|
|
if (self.match && self.whitelist.length) {
|
2014-01-10 00:26:07 +00:00
|
|
|
return key.match(self.match) || self.whitelist.indexOf(key) !== -1
|
2014-11-26 06:06:44 +00:00
|
|
|
}
|
|
|
|
else if (self.match) {
|
2014-11-26 06:28:31 +00:00
|
|
|
return key.match(self.match);
|
2014-11-26 06:06:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-01-10 00:17:52 +00:00
|
|
|
return !self.whitelist.length || self.whitelist.indexOf(key) !== -1
|
|
|
|
}
|
2011-11-20 21:21:09 +00:00
|
|
|
}).forEach(function (key) {
|
2017-11-13 04:11:01 +00:00
|
|
|
|
2017-09-27 20:32:26 +00:00
|
|
|
var val = env[key];
|
|
|
|
|
2017-10-22 01:57:31 +00:00
|
|
|
if (self.parseValues) {
|
2017-10-21 19:39:16 +00:00
|
|
|
val = common.parseValues(val);
|
2017-09-27 20:32:26 +00:00
|
|
|
}
|
|
|
|
|
2012-06-21 06:46:10 +00:00
|
|
|
if (self.separator) {
|
2017-09-27 20:32:26 +00:00
|
|
|
self.set(common.key.apply(common, key.split(self.separator)), val);
|
2014-11-26 06:28:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-09-27 20:32:26 +00:00
|
|
|
self.set(key, val);
|
2012-06-21 06:46:10 +00:00
|
|
|
}
|
2011-11-20 21:21:09 +00:00
|
|
|
});
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2017-11-13 04:11:01 +00:00
|
|
|
if (tempWrite) {
|
|
|
|
this.readOnly = true;
|
|
|
|
}
|
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
return this.store;
|
|
|
|
};
|