-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
132 lines (113 loc) · 3.85 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
'use strict';
const CommonImport = require('./lib/util/CommonImport');
module.exports = (uri, opts) => {
/*
* Try to set log level.
*/
let logLevel = 5;
if (uri && uri.logLevel !== undefined && uri.logLevel !== null && +uri.logLevel >= 0) {
logLevel = +uri.logLevel;
} else if (opts && opts.logLevel !== undefined && opts.logLevel !== null && +opts.logLevel >= 0) {
logLevel = +opts.logLevel;
}
CommonImport.logger.configure({
level: logLevel
});
if (!uri) {
CommonImport.logger.error('Invalid cayley configurations.');
throw new Error('Invalid cayley configurations.');
}
switch (typeof uri) {
case 'string':
uri = uri.replace(/.*\/{2,}|\/+.*$/g, '');
break;
case 'object':
CommonImport.logger.warn("'uri' is provided with JSON object, 2nd parameter 'opts' will be ignored.");
opts = uri;
uri = undefined;
break;
default:
CommonImport.logger.error('Invalid cayley configurations.');
throw new Error('Invalid cayley configurations.');
}
/*
* Default values for opts.
* Options defined for specific cayley instance will override outside options.
*/
const _opts = {
promisify: false,
secure: false,
servers: []
};
if (opts) {
if (typeof opts === 'object') {
_opts.promisify = !!opts.promisify;
if (!!opts.secure) {
if (!opts.certFile || !opts.keyFile || !opts.caFile) {
CommonImport.logger.error("'secure' is set to 'true', 'certFile', 'keyFile' or 'caFile' is missing.");
throw new Error("'secure' is set to 'true', 'certFile', 'keyFile' or 'caFile' is missing.");
}
Object.assign(_opts, {
secure: true,
certFile: opts.certFile,
keyFile: opts.keyFile,
caFile: opts.caFile
});
} else {
_opts.secure = false;
}
if (!!uri) {
CommonImport.logger.warn("Both 'uri' and 'opts' provided, 'uri' will be merged into 'opts.servers' with top level options.");
_opts.servers.push({
secure: _opts.secure,
baseUrl: (_opts.secure ? 'https://' : 'http://') + uri
});
}
if (opts.servers) {
if (Array.isArray(opts.servers)) {
opts.servers.forEach((item) => {
if (item.host && item.port) {
const secure = (item.secure === undefined || item.secure === null || typeof item.secure !== 'boolean') ? _opts.secure : item.secure;
const baseUrl = (secure ? 'https://' : 'http://') + item.host + ':' + item.port;
if (_opts.servers.every((server) => {
return server.baseUrl !== baseUrl;
})) {
const tmp = {
secure: secure,
baseUrl: baseUrl
};
if (secure && item.certFile && item.keyFile && item.caFile) {
Object.assign(tmp, {
certFile: item.certFile,
keyFile: item.keyFile,
caFile: item.caFile
});
}
_opts.servers.push(tmp);
}
}
});
} else {
CommonImport.logger.error("'opts.servers' should be provided with an array.");
throw new Error("'opts.servers' should be provided with an array.");
}
}
} else {
CommonImport.logger.error('Options should be provided with a JSON object.');
throw new Error('Options should be provided with a JSON object.');
}
} else {
_opts.servers = [
{
baseUrl: 'http://' + uri,
secure: false
}
];
}
const cayleyInstancePool = require('./lib/client')(_opts);
if (cayleyInstancePool.length === 1) {
return cayleyInstancePool[0];
}
cayleyInstancePool.pickRandomly = require('./lib/util/Util').pickRandomly;
return cayleyInstancePool;
};