-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.js
69 lines (54 loc) · 1.71 KB
/
config.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
// var utilities = require('./utilities');
var shortid = require('shortid')
, deepCopy = require('deep-copy')
;
module.exports = Config;
function Config() {
}
// we deep copy the configuration, then process various shortform leaves
// the deep copy ensures that configs passed in do not come out with new properties
Config.prototype.process = function (mesh, config, callback) {
var serializer;
this.log = mesh.log.createLogger('Config');
this.log.$$TRACE('process()');
var clonedConfig = deepCopy(config);
// process shortform endpoints
Object.keys(clonedConfig.endpoints || {}).forEach(function (name) {
var econf = clonedConfig.endpoints[name];
if (!isNaN(parseInt(econf))) {
clonedConfig.endpoints[name] = {
clonedConfig: {
port: parseInt(econf)
}
}
} else if (typeof econf == 'string') {
var pp = econf.split(':')
clonedConfig.endpoints[name] = {
clonedConfig: {
host: pp[0].trim(),
port: parseInt(pp[1].trim())
}
}
}
});
clonedConfig.endpoints = clonedConfig.endpoints || {};
if (clonedConfig.exchange && (serializer = clonedConfig.exchange.serializer)) {
if (typeof serializer == 'string') {
try {
mesh._mesh.serializer = this.validateSerializer(require(serializer));
} catch (e) {
this.log.warn('serializer load failed', e);
}
} else {
mesh._mesh.serializer = this.validateSerializer(serializer);
}
}
callback(null, clonedConfig);
};
Config.prototype.validateSerializer = function (it) {
if (typeof it.__encode !== 'function' || typeof it.__decode !== 'function') {
this.log.warn('invalid serializer ignored');
return;
}
return it;
};