-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconfig.js
More file actions
185 lines (152 loc) · 5.24 KB
/
Copy pathconfig.js
File metadata and controls
185 lines (152 loc) · 5.24 KB
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
'use strict'
const path = require('node:path')
const reader = require('./lib/reader')
// Resolve a caller-supplied config name against `base`.
// Absolute paths are an explicit, documented opt-in (e.g. /etc/services).
// Relative names must stay inside `base`; a `..` escape is rejected so a
// name can't reach files outside the configured config directory.
function safe_resolve(base, name) {
if (path.isAbsolute(name)) return name
const resolved = path.resolve(base, name)
const rel = path.relative(base, resolved)
if (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
throw new Error(`config name '${name}' escapes the config directory (${base})`)
}
return resolved
}
class Config {
constructor(root_path, no_overrides) {
this.root_path = root_path || reader.config_path
if (process.env.HARAKA_TEST_DIR) {
this.root_path = path.join(process.env.HARAKA_TEST_DIR, 'config')
return
}
if (process.env.HARAKA && !no_overrides) {
this.overrides_path = root_path || reader.config_path
this.root_path = path.join(process.env.HARAKA, 'config')
}
}
get(...args) {
/* eslint prefer-const: 0 */
let [name, type, cb, options] = this.arrange_args(args)
if (!type) type = 'value'
const full_path = safe_resolve(this.root_path, name)
let results = reader.read_config(full_path, type, cb, options)
if (this.overrides_path) {
const overrides_path = safe_resolve(this.overrides_path, name)
const overrides = reader.read_config(overrides_path, type, cb, options)
results = merge_config(results, overrides, type)
}
// Pass arrays by value to prevent config being modified accidentally.
if (Array.isArray(results)) return results.slice()
return results
}
getInt(filename, default_value) {
if (!filename) return NaN
const full_path = safe_resolve(this.root_path, filename)
const r = parseInt(reader.read_config(full_path, 'value', null, null), 10)
if (!isNaN(r)) return r
return parseInt(default_value, 10)
}
getDir(name, opts, done) {
const dir = safe_resolve(this.root_path, name)
// no callback, return promise
if (arguments.length < 3) return reader.read_dir(dir, opts)
reader
.read_dir(dir, opts)
.then((files) => {
done(null, files) // keep the API consistent
})
.catch(done)
}
arrange_args(args) {
/* ways get() can be called:
config.get('thing');
config.get('thing', type);
config.get('thing', cb);
config.get('thing', cb, options);
config.get('thing', options);
config.get('thing', type, cb);
config.get('thing', type, options);
config.get('thing', type, cb, options);
*/
const fs_name = args.shift()
let fs_type = null
let cb
let options
for (const arg of args) {
if ([undefined, null].includes(arg)) continue
switch (typeof arg) {
case 'function':
cb = arg
continue
case 'object':
options = arg
continue
case 'string':
if (/^(ini|value|list|data|h?json|js|yaml|binary)$/.test(arg)) {
fs_type = arg
continue
}
console.log(`unknown string: ${arg}`)
continue
}
// console.log(`unknown arg: ${arg}, typeof: ${typeof arg}`);
}
if (!fs_type) fs_type = reader.getType(fs_name)
return [fs_name, fs_type, cb, options]
}
// Stop watching `name`. Idempotent.
stop_watching(name) {
const full_path = safe_resolve(this.root_path, name)
// close both the path itself (getDir target) and its parent (get target)
reader.stop_watching(full_path)
reader.stop_watching(path.dirname(full_path))
}
module_config(defaults_path, overrides_path) {
const cfg = new Config(path.join(defaults_path, 'config'), true)
if (overrides_path) {
cfg.overrides_path = path.join(overrides_path, 'config')
}
return cfg
}
}
module.exports = new Config()
function merge_config(defaults, overrides, type) {
switch (type) {
case 'ini':
case 'hjson':
case 'json':
case 'js':
case 'yaml':
return merge_struct(JSON.parse(JSON.stringify(defaults)), overrides)
}
// flat list/data: a non-empty override replaces the default; an empty
// override (e.g. a missing override file, which reads as []) leaves the
// default in place rather than silently wiping it
if (Array.isArray(overrides)) {
return overrides.length ? overrides : defaults
}
// flat value: only a present (non-null) override replaces the default
if (overrides != null) return overrides
return defaults
}
const isObject = (v) => typeof v === 'object' && v !== null
function merge_struct(defaults, overrides) {
for (const k in overrides) {
if (['__proto__', 'constructor'].includes(k)) continue
if (overrides[k] === null) continue
if (k in defaults) {
if (isObject(overrides[k]) && isObject(defaults[k])) {
defaults[k] = merge_struct(defaults[k], overrides[k])
} else {
defaults[k] = overrides[k]
}
} else {
defaults[k] = overrides[k]
}
}
return defaults
}
// JSON overrides needs smtp.(json|yaml) loaded early
module.exports.get('smtp.json')