-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConfiguration.ts
executable file
·254 lines (238 loc) · 7.73 KB
/
Configuration.ts
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
import { merge, isArray, defaults, union } from 'lodash'
import { resolve, dirname } from 'path'
import { IllegalAccessError, ConfigValueError } from './errors'
import { requireMainFilename } from './utils'
import { Core } from './Core'
// Proxy Handler for get requests to the configuration
const ConfigurationProxyHandler: ProxyHandler<Configuration> = {
get (target: any, key: string) {
if (target.has && target.has(key)) {
const value = target.immutable === true ? Object.freeze(target.get(key)) : target.get(key)
return new Proxy(value, ConfigurationProxyHandler)
}
else {
return target.immutable === true ? Object.freeze(target[key]) : target[key]
}
}
}
/**
* Extend map class for getter/setter tuple config
*/
export class Configuration extends Map<any, any> {
public immutable: boolean
public env: {}
/**
* Flattens configuration tree
* Recursive
*/
static flattenTree (tree = { }) {
const toReturn: { [key: string]: any } = {}
// Try to flatten and fail if unable to resolve circular object
try {
Object.entries(tree).forEach(([k, v]) => {
// if (typeof v === 'object' && v !== null) {
if (
v !== null
&& v instanceof Object
&& typeof v !== 'function'
) {
// If value is an array, flatten by index and don't try to flatten further
// Configs with Array will throw a warning in v2.0 and an error in v3.0
if (Array.isArray(v)) {
v.forEach((val, i) => {
toReturn[`${k}.${i}`] = val
})
}
else if (!Core.isNotCircular(v)) {
toReturn[k] = v
}
// If the value is a normal object, keep flattening
else {
const flatObject = Configuration.flattenTree(v)
Object.keys(flatObject).forEach(flatKey => {
toReturn[`${k}.${flatKey}`] = flatObject[flatKey]
})
}
}
// Other wise, the value is a function, string, or number etc and should stop flattening
toReturn[k] = v
})
// Return the consturcted return object
return toReturn
}
catch (err) {
if (err !== Core.BreakException) {
throw new RangeError('Tree is circular and can not be resolved, check that there are no circular references in the config')
}
return toReturn
}
}
/**
* Defines the initial api resources
*/
static initialResources (tree, resources = []) {
if (tree.hasOwnProperty('main') && tree.main.hasOwnProperty('resources')) {
// Configs with Array will throw a warning in v2.0 and an error in v3.0
if (!isArray(tree.main['resources'])) {
throw new ConfigValueError('if set, main.resources must be an array')
}
return tree.main['resources']
}
else {
return resources
}
}
/**
* Copy and merge the provided configuration into a new object, decorated with
* necessary default and environment-specific values.
*/
static buildConfig (initialConfig: {env?: {[key: string]: any}} = { }, nodeEnv?: string) {
const root = resolve(dirname(requireMainFilename()))
const temp = resolve(root, '.tmp')
const envConfig = initialConfig.env && initialConfig.env[nodeEnv] || { }
const configTemplate = {
main: {
resources: Configuration.initialResources(initialConfig),
lockResources: false,
maxListeners: 128,
spools: [ ],
paths: {
root: root,
temp: temp,
sockets: resolve(temp, 'sockets'),
logs: resolve(temp, 'log')
},
freezeConfig: true,
createPaths: true
}
}
return merge(configTemplate, initialConfig, envConfig, { env: nodeEnv })
}
constructor (
configTree: {[key: string]: any} = { },
processEnv: {
[key: string]: any,
NODE_ENV?: string
} = { }
) {
// Constants for configuration
const config = Configuration.buildConfig(configTree, processEnv['NODE_ENV'])
const configEntries = Object.entries(Configuration.flattenTree(config))
// Add to the map constructor
super(configEntries)
// Initial values
this.immutable = false
this.env = processEnv
// Bind methods
this.get = this.get.bind(this)
this.set = this.set.bind(this)
this.entries = this.entries.bind(this)
this.has = this.has.bind(this)
// Return Proxy
return new Proxy(this, ConfigurationProxyHandler)
}
/**
* Recursively sets the tree values on the config map
*/
private _reverseFlattenSet(key, value) {
if (/\.[0-9a-z]+$/.test(key)) {
const decedent = (key).match(/\.([0-9a-z]+)$/)[1]
const parent = key.replace(/\.[0-9a-z]+$/, '')
const proto = Array.isArray(value) ? [] : {}
const newParentValue = Core.defaultsDeep({[decedent]: value}, this.get(parent) || proto)
super.set(key, value)
// Recursively reverse flatten the set back up the tree
return this._reverseFlattenSet(parent, newParentValue)
}
else {
// This is as high as it goes
return super.set(key, value)
}
}
/**
* Flattens what is being called to .set
*/
private _flattenSet(key, value) {
if (
value !== null
&& value instanceof Object
&& typeof value !== 'function'
&& !Array.isArray(value)
) {
// Flatten the new value
const configEntries = Object.entries(Configuration.flattenTree({[key]: value}))
// Set the flat values
configEntries.forEach(([_key, _value]) => {
return super.set(_key, _value)
})
}
// Reverse flatten up the tree
return this._reverseFlattenSet(key, value)
}
/**
* Throws IllegalAccessError if the configuration has already been set to immutable
* and an attempt to set value occurs.
*/
set (key: string, value: any) {
if (this.immutable === true) {
throw new IllegalAccessError('Cannot set properties directly on config. Use .set(key, value) (immutable)')
}
return this._flattenSet(key, value)
}
/**
* Merge tree into this configuration if allowed. Return overwritten keys
*/
merge (configTree: {[key: string]: any}, configAction = 'hold'): { hasKey: boolean, key: any }[] {
const configEntries = Object.entries(Configuration.flattenTree(configTree))
return configEntries.map(([ key, value ]) => {
const hasKey = this.has(key)
// If the key has never been set, it is added to the config
// If configAction is set to hold, then it will replace the initial config
if (!hasKey || configAction === 'hold') {
this.set(key, value)
}
// If configAction is set to merge, it will default values over the initial config
else if (hasKey && configAction === 'merge') {
if (value === null) {
// Do Nothing
}
else if (typeof value === 'undefined') {
// Do Nothing
}
else if (Array.isArray(value)) {
// Do Nothing
}
else if (typeof value === 'number') {
// Do Nothing
}
else if (typeof value === 'string') {
// Do Nothing
}
else if (typeof value === 'function') {
// Do Nothing
}
else {
this.set(key, Core.defaultsDeep(this.get(key), value))
}
}
// If configAction is replaceable, and the key already exists, it's ignored completely
// This is because it was set by a higher level app config
else if (hasKey && configAction === 'replaceable') {
// Do Nothing
}
return { hasKey, key }
})
}
/**
* Prevent changes to the app configuration
*/
freeze (): void {
this.immutable = true
}
/**
* Allow changes to the app configuration
*/
unfreeze (): void {
this.immutable = false
}
}