-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
308 lines (254 loc) · 9.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict'
const urlLib = require('url')
const deepFreeze = require('./deepFreeze')
module.exports = SpConf
SpConf.prototype.makeClonableAndDeepFreeze = deepFreeze.makeClonableAndDeepFreeze
SpConf.makeClonableAndDeepFreeze = deepFreeze.makeClonableAndDeepFreeze
function SpConf (defaultOptions) {
if (!(this instanceof SpConf)) return new SpConf(defaultOptions)
this.defaultOptions = _cleanOptions(defaultOptions)
this.missingEnvVars = false
}
SpConf.missingEnvVars = false
SpConf.prototype.readString = function (name, options) {
return readString(name, options, this)
}
SpConf.readString = function (name, options) {
return readString(name, options, module.exports)
}
function readString (name, options, owner) {
if (Array.isArray(name)) return _tryEach(readString, 'string', name, options, owner)
options = _cleanOptions(options, owner && owner.defaultOptions)
const val = options.source[name]
if (val !== undefined) {
if (_failedvalidator(options.validator, val)) {
options.error(`Expected env var "${name}" to be match pattern "${options.validator}" but was "${val}" and did not.`)
owner.missingEnvVars = true
} else {
options.log(`Using env var ${name} ${val}`)
return val
}
} else if (options.defaultValue !== undefined) {
options.log(`Using default ${name} ${options.defaultValue}`)
return options.defaultValue
} else {
options.error(`Required string env var "${name}" was not supplied.`)
owner.missingEnvVars = true
}
}
SpConf.prototype.readNumber = function (name, options) {
return readNumber(name, options, this)
}
SpConf.readNumber = function (name, options) {
return readNumber(name, options, module.exports)
}
function readNumber (name, options, owner) {
if (Array.isArray(name)) return _tryEach(readNumber, 'number', name, options, owner)
options = _cleanOptions(options, owner && owner.defaultOptions)
const val = options.source[name]
if (val !== undefined) {
const number = parseInt(val, 10)
if (('' + number) !== val) {
options.error(`Expected env var "${name}" to be numeric but was "${val}".`)
owner.missingEnvVars = true
} else {
options.log(`Using env var ${name} ${number}`)
return number
}
} else if (options.defaultValue !== undefined) {
options.log(`Using default env var ${name} ${options.defaultValue}`)
return options.defaultValue
} else {
options.error(`Required numeric env var "${name}" was not supplied.`)
owner.missingEnvVars = true
}
}
SpConf.prototype.readBool = function (name, options) {
return readBool(name, options, this)
}
SpConf.readBool = function (name, options) {
return readBool(name, options, module.exports)
}
function readBool (name, options, owner) {
if (Array.isArray(name)) return _tryEach(readBool, 'bool', name, options, owner)
options = _cleanOptions(options, owner && owner.defaultOptions)
const val = options.source[name]
if (val !== undefined) {
const valLower = val.toLowerCase()
if (valLower === 't' || valLower === 'true' || valLower === 'on' || valLower === '1') {
options.log(`Using env var ${name} true`)
return true
} else if (valLower === 'f' || valLower === 'false' || valLower === 'off' || valLower === '0') {
options.log(`Using env var ${name} false`)
return false
} else {
options.error(`Expected env var "${name}" to be a bool but was "${val}".`)
owner.missingEnvVars = true
}
} else if (options.defaultValue !== undefined) {
options.log(`Using default env var ${name} ${options.defaultValue}`)
return options.defaultValue
} else {
options.error(`Required numeric env var "${name}" was not supplied.`)
owner.missingEnvVars = true
}
}
SpConf.prototype.readPassword = function (name, options) {
return readPassword(name, options, this)
}
SpConf.readPassword = function (name, options) {
return readPassword(name, options, module.exports)
}
function readPassword (name, options, owner) {
if (Array.isArray(name)) return _tryEach(readPassword, 'password', name, options, owner)
options = _cleanOptions(options, owner && owner.defaultOptions)
const val = options.source[name]
if (val !== undefined) {
options.log(`Using env var ${name} ${_obfuscate(val)}`)
return val
} else if (options.defaultValue !== undefined) {
options.log(`Using default ${name} ${_obfuscate(options.defaultValue)}`)
return options.defaultValue
} else {
options.error(`Required password env var "${name}" was not supplied.`)
owner.missingEnvVars = true
}
}
SpConf.prototype.readCertificate = function (name, options) {
return readCertificate(name, options, this)
}
SpConf.readCertificate = function (name, options) {
return readCertificate(name, options, module.exports)
}
function readCertificate (name, options, owner) {
if (Array.isArray(name)) return _tryEach(readCertificate, 'certificate', name, options, owner)
options = _cleanOptions(options, owner && owner.defaultOptions)
const val = options.source[name]
if (val !== undefined) {
options.log(`Using env var ${name} ${_obfuscateCertificate(val)}`)
return val
} else if (options.defaultValue !== undefined) {
options.log(`Using default ${name} ${_obfuscateCertificate(options.defaultValue)}`)
return options.defaultValue
} else {
options.error(`Required certificate env var "${name}" was not supplied.`)
owner.missingEnvVars = true
}
}
SpConf.prototype.readUrl = function (name, options) {
return readUrl(name, options, this)
}
SpConf.readUrl = function (name, options) {
return readUrl(name, options, module.exports)
}
function readUrl (name, options, owner) {
if (Array.isArray(name)) return _tryEach(readUrl, 'url', name, options, owner)
options = _cleanOptions(options, owner && owner.defaultOptions)
const val = options.source[name]
if (val !== undefined) {
if (_failedvalidator(options.validator, val)) {
options.error(`Expected env var "${name}" to be match pattern "${options.validator}" but was "${_obfuscateAuth(val)}" and did not.`)
owner.missingEnvVars = true
} else {
options.log(`Using env var ${name} ${_obfuscateAuth(val)}`)
return val
}
} else if (options.defaultValue !== undefined) {
options.log(`Using default ${name} ${_obfuscateAuth(options.defaultValue)}`)
return options.defaultValue
} else {
options.error(`Required url env var "${name}" was not supplied.`)
owner.missingEnvVars = true
}
}
SpConf.obfuscate = _obfuscate
function _obfuscate (str) {
if (!str) return str
let showBits = Math.floor(str.length / 6)
if (showBits > 3) showBits = 3
const start = str.substr(0, showBits)
const end = str.substr(-showBits, showBits)
const middle = new Array(str.length - ((showBits * 2) - 1)).join('*')
return start + middle + end
}
SpConf.obfuscateAuth = _obfuscateAuth
function _obfuscateAuth (url) {
url = urlLib.parse(url)
if (url.auth && url.auth.split) {
const bits = url.auth.split(':')
if (bits[1]) {
bits[1] = _obfuscate(bits[1])
url.auth = bits.join(':')
}
}
return url.format()
}
const certificateHeader = '-----BEGIN CERTIFICATE-----'
const certificateFooter = '-----END CERTIFICATE-----'
SpConf.obfuscateCertificate = _obfuscateCertificate
function _obfuscateCertificate (certificate) {
if (!certificate) return certificate
const headerPos = certificate.indexOf(certificateHeader)
if (headerPos !== -1) {
certificate = certificate.substr(headerPos + certificateHeader.length)
}
const footerPos = certificate.indexOf(certificateFooter)
if (footerPos !== -1) {
certificate = certificate.substr(0, footerPos)
}
certificate = certificate.split(/\s+/).join('')
let showBits = Math.floor(certificate.length / 6)
if (showBits > 3) showBits = 3
const start = certificate.substr(0, showBits)
const end = certificate.substr(-showBits, showBits)
let middle = new Array(certificate.length - ((showBits * 2) - 1)).join('*')
if (middle.length > 26) {
middle = '**********...**********'
}
return start + middle + end
}
function _tryEach (func, type, names, options, owner) {
var found = null
const cleanOptions = _cleanOptions(options, owner && owner.defaultOptions)
names.find(name => {
const innerOptions = Object.assign({}, options, {error: _ => null})
delete innerOptions.defaultValue
const innerOwner = Object.assign({}, owner)
found = func(name, innerOptions, innerOwner)
if (found) return found
cleanOptions.log(`Could not use ${type} "${name}".`)
})
if (found) return found
if (cleanOptions.defaultValue !== undefined) {
cleanOptions.log(`Using default for ${names} ${cleanOptions.defaultValue}`)
return cleanOptions.defaultValue
}
owner.missingEnvVars = true
cleanOptions.error(`At least one of required ${type}s "${names.join('" or "')}" was not supplied.`)
}
function _failedvalidator (validator, val) {
if (!validator) return false
if (typeof validator === 'string') validator = new RegExp(validator)
return !validator.test(val)
}
function _cleanOptions (options, defaultOptions) {
if (typeof options === 'string') {
options = {
'defaultValue': options
}
}
if (options === undefined) options = {}
if (defaultOptions) options = Object.assign({}, defaultOptions, options)
if (options.source === undefined) options.source = process.env
if (options.log === undefined) {
options.log = function () {
console.log.apply(console, arguments)
}
}
if (options.error === undefined) {
options.error = function () {
console.error.apply(console, arguments)
}
}
return options
}