-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathConfig.js
More file actions
313 lines (273 loc) · 7.2 KB
/
Config.js
File metadata and controls
313 lines (273 loc) · 7.2 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
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
309
310
311
312
313
/**
* Copyright 2013-2022 the PM2 project authors. All rights reserved.
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var util = require('util');
/**
* Validator of configured file / commander options.
*/
var Config = module.exports = {
_errMsgs: {
'require': '"%s" is required',
'type' : 'Expect "%s" to be a typeof %s, but now is %s',
'regex' : 'Verify "%s" with regex failed, %s',
'max' : 'The maximum of "%s" is %s, but now is %s',
'min' : 'The minimum of "%s" is %s, but now is %s'
},
/**
* Schema definition.
* @returns {exports|*}
*/
get schema(){
// Cache.
if (this._schema) {
return this._schema;
}
// Render aliases.
this._schema = require('../API/schema');
for (var k in this._schema) {
if (k.indexOf('\\') > 0) {
continue;
}
var aliases = [
k.split('_').map(function(n, i){
if (i != 0 && n && n.length > 1) {
return n[0].toUpperCase() + n.slice(1);
}
return n;
}).join('')
];
if (this._schema[k].alias && Array.isArray(this._schema[k].alias)) {
// If multiple aliases, merge
this._schema[k].alias.forEach(function(alias) {
aliases.splice(0, 0, alias);
});
}
else if (this._schema[k].alias)
aliases.splice(0, 0, this._schema[k].alias);
this._schema[k].alias = aliases;
}
return this._schema;
}
};
function tokenizePm2ConfigArrayString(input) {
var tokens = [];
var token = '';
var quote = null;
var escape = false;
var stripQuoteDelimiters = false;
function flush() {
if (token && token.trim()) {
tokens.push(token);
}
token = '';
}
function isWhitespace(ch) {
return ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r' || ch === '\f' || ch === '\v';
}
for (var i = 0; i < input.length; i++) {
var ch = input[i];
if (escape) {
token += ch;
escape = false;
continue;
}
if (ch === '\\') {
token += ch;
escape = true;
continue;
}
if (quote) {
if (ch === quote) {
if (!stripQuoteDelimiters) {
token += ch;
}
quote = null;
stripQuoteDelimiters = false;
continue;
}
token += ch;
continue;
}
if (ch === '"' || ch === "'") {
// Keep legacy behavior:
// - standalone quoted token: "a b" -> a b
// - quoted value inside existing token: --k="a b" -> --k="a b"
stripQuoteDelimiters = token.length === 0;
quote = ch;
if (!stripQuoteDelimiters) {
token += ch;
}
continue;
}
if (isWhitespace(ch)) {
flush();
continue;
}
token += ch;
}
flush();
return tokens;
}
/**
* Filter / Alias options
*/
Config.filterOptions = function(cmd) {
var conf = {};
var schema = this.schema;
for (var key in schema) {
var aliases = schema[key].alias;
aliases && aliases.forEach(function(alias){
if (typeof(cmd[alias]) !== 'undefined') {
conf[key] || (conf[key] = cmd[alias]);
}
});
}
return conf;
};
/**
* Verify JSON configurations.
* @param {Object} json
* @returns {{errors: Array, config: {}}}
*/
Config.validateJSON = function(json){
// clone config
var conf = Object.assign({}, json),
res = {};
this._errors = [];
var regexKeys = {}, defines = this.schema;
for (var sk in defines) {
// Pick up RegExp keys.
if (sk.indexOf('\\') >= 0) {
regexKeys[sk] = false;
continue;
}
var aliases = defines[sk].alias;
aliases && aliases.forEach(function(alias){
conf[sk] || (conf[sk] = json[alias]);
})
var val = conf[sk];
delete conf[sk];
// Validate key-value pairs.
if (val === undefined ||
val === null ||
((val = this._valid(sk, val)) === null)) {
// If value is not defined
// Set default value (via schema.json)
if (typeof(defines[sk].default) !== 'undefined')
res[sk] = defines[sk].default;
continue;
}
//console.log(sk, val, val === null, val === undefined);
res[sk] = val;
}
// Validate RegExp values.
var hasRegexKey = false;
for (var k in regexKeys) {
hasRegexKey = true;
regexKeys[k] = new RegExp(k);
}
if (hasRegexKey) {
for (var k in conf) {
for (var rk in regexKeys) {
if (regexKeys[rk].test(k))
if (this._valid(k, conf[k], defines[rk])) {
res[k] = conf[k];
delete conf[k];
}
}
}
}
return {errors: this._errors, config: res};
};
/**
* Validate key-value pairs by specific schema
* @param {String} key
* @param {Mixed} value
* @param {Object} sch
* @returns {*}
* @private
*/
Config._valid = function(key, value, sch){
var sch = sch || this.schema[key],
scht = typeof sch.type == 'string' ? [sch.type] : sch.type;
// Required value.
var undef = typeof value == 'undefined';
if(this._error(sch.require && undef, 'require', key)){
return null;
}
// If undefined, make a break.
if (undef) {
return null;
}
// Wrap schema types.
scht = scht.map(function(t){
return '[object ' + t[0].toUpperCase() + t.slice(1) + ']'
});
// Typeof value.
var type = Object.prototype.toString.call(value), nt = '[object Number]';
// Auto parse Number
if (type != '[object Boolean]' && scht.indexOf(nt) >= 0 && !isNaN(value)) {
value = parseFloat(value);
type = nt;
}
// Verify types.
if (this._error(!~scht.indexOf(type), 'type', key, scht.join(' / '), type)) {
return null;
}
// Verify RegExp if exists.
if (this._error(type == '[object String]' && sch.regex && !(new RegExp(sch.regex)).test(value),
'regex', key, sch.desc || ('should match ' + sch.regex))) {
return null;
}
// Verify maximum / minimum of Number value.
if (type == '[object Number]') {
if (this._error(typeof sch.max != 'undefined' && value > sch.max, 'max', key, sch.max, value)) {
return null;
}
if (this._error(typeof sch.min != 'undefined' && value < sch.min, 'min', key, sch.min, value)) {
return null;
}
}
// If first type is Array, but current is String, try to split them.
if(scht.length > 1 && type != scht[0] && type == '[object String]'){
if(scht[0] == '[object Array]') {
value = tokenizePm2ConfigArrayString(value);
}
}
// Custom types: sbyte && stime.
if(sch.ext_type && type == '[object String]' && value.length >= 2) {
var seed = {
'sbyte': {
'G': 1024 * 1024 * 1024,
'M': 1024 * 1024,
'K': 1024
},
'stime': {
'h': 60 * 60 * 1000,
'm': 60 * 1000,
's': 1000
}
}[sch.ext_type];
if(seed){
value = parseFloat(value.slice(0, -1)) * (seed[value.slice(-1)]);
}
}
return value;
};
/**
* Wrap errors.
* @param {Boolean} possible A value indicates whether it is an error or not.
* @param {String} type
* @returns {*}
* @private
*/
Config._error = function(possible, type){
if (possible) {
var args = Array.prototype.slice.call(arguments);
args.splice(0, 2, this._errMsgs[type]);
this._errors && this._errors.push(util.format.apply(null, args));
}
return possible;
}