First upload version 0.0.1
This commit is contained in:
13
node_modules/env-var/lib/accessors/array.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/array.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asString = require('./string')
|
||||
|
||||
module.exports = function asArray (value, delimiter) {
|
||||
delimiter = delimiter || ','
|
||||
|
||||
if (!value.length) {
|
||||
return []
|
||||
} else {
|
||||
return asString(value).split(delimiter).filter(Boolean)
|
||||
}
|
||||
}
|
||||
11
node_modules/env-var/lib/accessors/bool-strict.js
generated
vendored
Normal file
11
node_modules/env-var/lib/accessors/bool-strict.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function asBoolStrict (value) {
|
||||
const val = value.toLowerCase()
|
||||
|
||||
if ((val !== 'false') && (val !== 'true')) {
|
||||
throw new Error('should be either "true", "false", "TRUE", or "FALSE"')
|
||||
}
|
||||
|
||||
return val !== 'false'
|
||||
}
|
||||
18
node_modules/env-var/lib/accessors/bool.js
generated
vendored
Normal file
18
node_modules/env-var/lib/accessors/bool.js
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function asBool (value) {
|
||||
const val = value.toLowerCase()
|
||||
|
||||
const allowedValues = [
|
||||
'false',
|
||||
'0',
|
||||
'true',
|
||||
'1'
|
||||
]
|
||||
|
||||
if (allowedValues.indexOf(val) === -1) {
|
||||
throw new Error('should be either "true", "false", "TRUE", "FALSE", 1, or 0')
|
||||
}
|
||||
|
||||
return !(((val === '0') || (val === 'false')))
|
||||
}
|
||||
16
node_modules/env-var/lib/accessors/email-string.js
generated
vendored
Normal file
16
node_modules/env-var/lib/accessors/email-string.js
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
'use strict'
|
||||
|
||||
const asString = require('./string')
|
||||
|
||||
// eslint-disable-next-line no-control-regex
|
||||
const EMAIL_REGEX = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\u0001-\u0008\u000b\u000c\u000e-\u001f\u0021\u0023-\u005b\u005d-\u007f]|\\[\u0001-\u0009\u000b\u000c\u000e-\u007f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\u0001-\u0008\u000b\u000c\u000e-\u001f\u0021-\u005a\u0053-\u007f]|\\[\u0001-\u0009\u000b\u000c\u000e-\u007f])+)\])$/
|
||||
|
||||
module.exports = function asEmailString (value) {
|
||||
const strValue = asString(value)
|
||||
|
||||
if (!EMAIL_REGEX.test(strValue)) {
|
||||
throw new Error('should be a valid email address')
|
||||
}
|
||||
|
||||
return strValue
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/enum.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/enum.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asString = require('./string')
|
||||
|
||||
module.exports = function asEnum (value, validValues) {
|
||||
const valueString = asString(value)
|
||||
|
||||
if (validValues.indexOf(valueString) < 0) {
|
||||
throw new Error(`should be one of [${validValues.join(', ')}]`)
|
||||
}
|
||||
|
||||
return valueString
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/float-negative.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/float-negative.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asFloat = require('./float')
|
||||
|
||||
module.exports = function asFloatNegative (value) {
|
||||
const ret = asFloat(value)
|
||||
|
||||
if (ret > 0) {
|
||||
throw new Error('should be a negative float')
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/float-positive.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/float-positive.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asFloat = require('./float')
|
||||
|
||||
module.exports = function asFloatPositive (value) {
|
||||
const ret = asFloat(value)
|
||||
|
||||
if (ret < 0) {
|
||||
throw new Error('should be a positive float')
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/float.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/float.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function asFloat (value) {
|
||||
const n = parseFloat(value)
|
||||
|
||||
// Some values are parsed as valid floats despite being obviously invalid, e.g. "1.o" or "192.168.1.1".
|
||||
// In these cases we would want to throw an error.
|
||||
if (isNaN(n) || isNaN(value)) {
|
||||
throw new Error('should be a valid float')
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
31
node_modules/env-var/lib/accessors/index.js
generated
vendored
Normal file
31
node_modules/env-var/lib/accessors/index.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
module.exports = {
|
||||
asArray: require('./array'),
|
||||
asSet: require('./set'),
|
||||
|
||||
asBoolStrict: require('./bool-strict'),
|
||||
asBool: require('./bool'),
|
||||
|
||||
asPortNumber: require('./port'),
|
||||
asEnum: require('./enum'),
|
||||
|
||||
asFloatNegative: require('./float-negative'),
|
||||
asFloatPositive: require('./float-positive'),
|
||||
asFloat: require('./float'),
|
||||
|
||||
asIntNegative: require('./int-negative'),
|
||||
asIntPositive: require('./int-positive'),
|
||||
asInt: require('./int'),
|
||||
|
||||
asJsonArray: require('./json-array'),
|
||||
asJsonObject: require('./json-object'),
|
||||
asJson: require('./json'),
|
||||
|
||||
asRegExp: require('./regexp'),
|
||||
|
||||
asString: require('./string'),
|
||||
|
||||
asUrlObject: require('./url-object'),
|
||||
asUrlString: require('./url-string'),
|
||||
|
||||
asEmailString: require('./email-string')
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/int-negative.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/int-negative.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asInt = require('./int')
|
||||
|
||||
module.exports = function asIntNegative (value) {
|
||||
const ret = asInt(value)
|
||||
|
||||
if (ret > 0) {
|
||||
throw new Error('should be a negative integer')
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/int-positive.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/int-positive.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asInt = require('./int')
|
||||
|
||||
module.exports = function asIntPositive (value) {
|
||||
const ret = asInt(value)
|
||||
|
||||
if (ret < 0) {
|
||||
throw new Error('should be a positive integer')
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
11
node_modules/env-var/lib/accessors/int.js
generated
vendored
Normal file
11
node_modules/env-var/lib/accessors/int.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function asInt (value) {
|
||||
const n = parseInt(value, 10)
|
||||
|
||||
if (isNaN(n) || n.toString(10) !== value) {
|
||||
throw new Error('should be a valid integer')
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/json-array.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/json-array.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asJson = require('./json')
|
||||
|
||||
module.exports = function asJsonArray (value) {
|
||||
var ret = asJson(value)
|
||||
|
||||
if (!Array.isArray(ret)) {
|
||||
throw new Error('should be a parseable JSON Array')
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/json-object.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/json-object.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asJson = require('./json')
|
||||
|
||||
module.exports = function asJsonObject (value) {
|
||||
var ret = asJson(value)
|
||||
|
||||
if (Array.isArray(ret)) {
|
||||
throw new Error('should be a parseable JSON Object')
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
9
node_modules/env-var/lib/accessors/json.js
generated
vendored
Normal file
9
node_modules/env-var/lib/accessors/json.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function asJson (value) {
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch (e) {
|
||||
throw new Error('should be valid (parseable) JSON')
|
||||
}
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/port.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/port.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asIntPositive = require('./int-positive')
|
||||
|
||||
module.exports = function asPortNumber (value) {
|
||||
var ret = asIntPositive(value)
|
||||
|
||||
if (ret > 65535) {
|
||||
throw new Error('cannot assign a port number greater than 65535')
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
21
node_modules/env-var/lib/accessors/regexp.js
generated
vendored
Normal file
21
node_modules/env-var/lib/accessors/regexp.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function asRegExp (value, flags) {
|
||||
// We have to test the value and flags indivudally if we want to write our
|
||||
// own error messages,as there is no way to differentiate between the two
|
||||
// errors except by using string comparisons.
|
||||
|
||||
// Test the flags
|
||||
try {
|
||||
RegExp(undefined, flags)
|
||||
} catch (err) {
|
||||
throw new Error('invalid regexp flags')
|
||||
}
|
||||
|
||||
try {
|
||||
return new RegExp(value, flags)
|
||||
} catch (err) {
|
||||
// We know that the regexp is the issue because we tested the flags earlier
|
||||
throw new Error('should be a valid regexp')
|
||||
}
|
||||
}
|
||||
11
node_modules/env-var/lib/accessors/set.js
generated
vendored
Normal file
11
node_modules/env-var/lib/accessors/set.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const asArray = require('./array')
|
||||
|
||||
module.exports = function asSet (value, delimiter) {
|
||||
if (!value.length) {
|
||||
return new Set()
|
||||
} else {
|
||||
return new Set(asArray(value, delimiter))
|
||||
}
|
||||
}
|
||||
5
node_modules/env-var/lib/accessors/string.js
generated
vendored
Normal file
5
node_modules/env-var/lib/accessors/string.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function asString (value) {
|
||||
return value
|
||||
}
|
||||
13
node_modules/env-var/lib/accessors/url-object.js
generated
vendored
Normal file
13
node_modules/env-var/lib/accessors/url-object.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const asString = require('./string')
|
||||
|
||||
module.exports = function asUrlObject (value) {
|
||||
const ret = asString(value)
|
||||
|
||||
try {
|
||||
return new URL(ret)
|
||||
} catch (e) {
|
||||
throw new Error('should be a valid URL')
|
||||
}
|
||||
}
|
||||
7
node_modules/env-var/lib/accessors/url-string.js
generated
vendored
Normal file
7
node_modules/env-var/lib/accessors/url-string.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const urlObject = require('./url-object')
|
||||
|
||||
module.exports = function asUrlString (value) {
|
||||
return urlObject(value).toString()
|
||||
}
|
||||
20
node_modules/env-var/lib/env-error.js
generated
vendored
Normal file
20
node_modules/env-var/lib/env-error.js
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Custom error class that can be used to identify errors generated
|
||||
* by the module
|
||||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error}
|
||||
*/
|
||||
class EnvVarError extends Error {
|
||||
constructor (message, ...params) {
|
||||
super(`env-var: ${message}`, ...params)
|
||||
/* istanbul ignore else */
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, EnvVarError)
|
||||
}
|
||||
|
||||
this.name = 'EnvVarError'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EnvVarError
|
||||
13
node_modules/env-var/lib/logger.js
generated
vendored
Normal file
13
node_modules/env-var/lib/logger.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Default logger included with env-var.
|
||||
* Will not log anything if NODE_ENV is set to production
|
||||
*/
|
||||
module.exports = function genLogger (out, prodFlag) {
|
||||
return function envVarLogger (varname, str) {
|
||||
if (!prodFlag || !prodFlag.match(/prod|production/)) {
|
||||
out(`env-var (${varname}): ${str}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
183
node_modules/env-var/lib/variable.js
generated
vendored
Normal file
183
node_modules/env-var/lib/variable.js
generated
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
'use strict'
|
||||
|
||||
const EnvVarError = require('./env-error')
|
||||
const base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/
|
||||
|
||||
/**
|
||||
* Returns an Object that contains functions to read and specify the format of
|
||||
* the variable you wish to have returned
|
||||
* @param {Object} container Encapsulated container (e.g., `process.env`).
|
||||
* @param {String} varName Name of the requested property from `container`.
|
||||
* @param {*} defValue Default value to return if `varName` is invalid.
|
||||
* @param {Object} extraAccessors Extra accessors to install.
|
||||
* @return {Object}
|
||||
*/
|
||||
module.exports = function getVariableAccessors (container, varName, extraAccessors, logger) {
|
||||
let isBase64 = false
|
||||
let isRequired = false
|
||||
let defValue
|
||||
let example
|
||||
|
||||
const builtInAccessors = require('./accessors/index')
|
||||
|
||||
/**
|
||||
* Logs the given string using the provided logger
|
||||
* @param {String} str
|
||||
* @param {String} str
|
||||
*/
|
||||
function log (str) {
|
||||
logger(varName, str)
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an error with a consistent type/format.
|
||||
* @param {String} value
|
||||
*/
|
||||
function raiseError (value, msg) {
|
||||
let errMsg = `"${varName}" ${msg}`
|
||||
|
||||
if (value) {
|
||||
errMsg = `${errMsg}`
|
||||
}
|
||||
|
||||
if (example) {
|
||||
errMsg = `${errMsg}. An example of a valid value would be: ${example}`
|
||||
}
|
||||
|
||||
throw new EnvVarError(errMsg)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an accessor wrapped by error handling and args passing logic
|
||||
* @param {Function} accessor
|
||||
*/
|
||||
function generateAccessor (accessor) {
|
||||
return function () {
|
||||
let value = container[varName]
|
||||
|
||||
log(`will be read from the environment using "${accessor.name}" accessor`)
|
||||
|
||||
if (typeof value === 'undefined') {
|
||||
if (typeof defValue === 'undefined' && isRequired) {
|
||||
log('was not found in the environment, but is required to be set')
|
||||
// Var is not set, nor is a default. Throw an error
|
||||
raiseError(undefined, 'is a required variable, but it was not set')
|
||||
} else if (typeof defValue !== 'undefined') {
|
||||
log(`was not found in the environment, parsing default value "${defValue}" instead`)
|
||||
value = defValue
|
||||
} else {
|
||||
log('was not found in the environment, but is not required. returning undefined')
|
||||
// return undefined since variable is not required and
|
||||
// there's no default value provided
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
if (isRequired) {
|
||||
log('verifying variable value is not an empty string')
|
||||
// Need to verify that required variables aren't just whitespace
|
||||
if (value.trim().length === 0) {
|
||||
raiseError(undefined, 'is a required variable, but its value was empty')
|
||||
}
|
||||
}
|
||||
|
||||
if (isBase64) {
|
||||
log('verifying variable is a valid base64 string')
|
||||
if (!value.match(base64Regex)) {
|
||||
raiseError(value, 'should be a valid base64 string if using convertFromBase64')
|
||||
}
|
||||
log('converting from base64 to utf8 string')
|
||||
value = Buffer.from(value, 'base64').toString()
|
||||
}
|
||||
|
||||
const args = [value].concat(Array.prototype.slice.call(arguments))
|
||||
|
||||
try {
|
||||
log(`passing value "${value}" to "${accessor.name}" accessor`)
|
||||
|
||||
const result = accessor.apply(
|
||||
accessor,
|
||||
args
|
||||
)
|
||||
|
||||
log(`parsed successfully, returning ${result}`)
|
||||
return result
|
||||
} catch (error) {
|
||||
raiseError(value, error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const accessors = {
|
||||
/**
|
||||
* Instructs env-var to first convert the value of the variable from base64
|
||||
* when reading it using a function such as asString()
|
||||
*/
|
||||
convertFromBase64: function () {
|
||||
log('marking for base64 conversion')
|
||||
isBase64 = true
|
||||
|
||||
return accessors
|
||||
},
|
||||
|
||||
/**
|
||||
* Set a default value for the variable
|
||||
* @param {String} value
|
||||
*/
|
||||
default: function (value) {
|
||||
if (typeof value === 'number') {
|
||||
defValue = value.toString()
|
||||
} else if (Array.isArray(value) || (typeof value === 'object' && value !== null)) {
|
||||
defValue = JSON.stringify(value)
|
||||
} else if (typeof value !== 'string') {
|
||||
throw new EnvVarError('values passed to default() must be of Number, String, Array, or Object type')
|
||||
} else {
|
||||
defValue = value
|
||||
}
|
||||
|
||||
log(`setting default value to "${defValue}"`)
|
||||
|
||||
return accessors
|
||||
},
|
||||
|
||||
/**
|
||||
* Ensures a variable is set in the given environment container. Throws an
|
||||
* EnvVarError if the variable is not set or a default is not provided
|
||||
* @param {Boolean} required
|
||||
*/
|
||||
required: function (required) {
|
||||
if (typeof required === 'undefined') {
|
||||
log('marked as required')
|
||||
// If no value is passed assume that developer means "true"
|
||||
// This is to retain support legacy usage (and intuitive)
|
||||
isRequired = true
|
||||
} else {
|
||||
log(`setting required flag to ${required}`)
|
||||
isRequired = required
|
||||
}
|
||||
|
||||
return accessors
|
||||
},
|
||||
|
||||
/**
|
||||
* Set an example value for this variable. If the variable value is not set
|
||||
* or is set to an invalid value this example will be show in error output.
|
||||
* @param {String} example
|
||||
*/
|
||||
example: function (ex) {
|
||||
example = ex
|
||||
|
||||
return accessors
|
||||
}
|
||||
}
|
||||
|
||||
// Attach accessors, and extra accessors if provided.
|
||||
Object.entries({
|
||||
...builtInAccessors,
|
||||
...extraAccessors
|
||||
}).forEach(([name, accessor]) => {
|
||||
accessors[name] = generateAccessor(accessor)
|
||||
})
|
||||
|
||||
return accessors
|
||||
}
|
||||
Reference in New Issue
Block a user