-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
213 lines (180 loc) · 5.01 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
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
const YAML = require('js-yaml');
const fs = require('fs');
const fsp = require('fs/promises');
const configName = 'vbrequest.yml';
const encodingConfig = 'utf8';
// Generate file 'vbrequest.yml' if not exists
const defaultConfig = `
# VBRequest Configuration File
# This file is used to configure the VBRequest module
# With token when fetching data you can use %token% to replace it if needed
groups:
servers1:
server-choosing: 1
servers:
1:
url: 'http://localhost:3000'
token: 'token'
2:
url: 'http://localhost:3001'
token: 'token'
servers2:
server-choosing: 1
servers:
1:
url: 'http://localhost:3000'
token: 'token'
2:
url: 'http://localhost:3001'
token: 'token'
`;
if (!fs.existsSync(configName)) fs.writeFileSync(configName, defaultConfig);
const readConfig = () =>
{
return fsp.readFile(configName, encodingConfig);
}
const writeConfig = (data) =>
{
return fsp.writeFile(configName, data, encodingConfig);
}
const parseConfig = async () =>
{
return YAML.load(await readConfig());
}
const getKOConfig = async (path) =>
{
let config = await parseConfig();
let keys = path.split('.');
let lastKey = keys.pop();
let lastObj = keys.reduce((obj, key) => obj[key] = obj[key] || {}, config);
return {
config: config,
lastObj: lastObj,
lastKey: lastKey
};
}
const setNestedValue = async (path, value) =>
{
try {
let koConfig = await getKOConfig(path);
let lastKey = koConfig.lastKey;
let lastObj = koConfig.lastObj;
lastObj[lastKey] = value;
await writeConfig(YAML.dump(koConfig.config));
} catch (err) {
console.error('VBError: ' + err);
return;
}
}
const getNestedValue = async (path) =>
{
try {
let koConfig = await getKOConfig(path);
let lastKey = koConfig.lastKey;
let lastObj = koConfig.lastObj;
return lastObj[lastKey];
} catch (err) {
console.error('VBError: ' + err);
return null;
}
}
const getNextServer = async (group) =>
{
try {
let nestedServerChoosing = `groups.${group}.server-choosing`;
let servers = await getNestedValue(`groups.${group}.servers`);
let serverChoosing = await getNestedValue(nestedServerChoosing);
// If the server choosing is greater than the number of servers, reset it to 1
if (serverChoosing >= Object.keys(servers).length) {
await setNestedValue(nestedServerChoosing, 1);
} else {
// Increment the server choosing
await setNestedValue(nestedServerChoosing, serverChoosing + 1);
}
let nextServer = servers[serverChoosing];
return nextServer;
} catch (err) {
console.error('VBError: ' + err);
return null;
}
}
const replaceToken = (headers, token) =>
{
for (let key in headers) headers[key] = headers[key].toString().replace('%token%', token);
}
const doFetch = async (group, method, path, init = null) =>
{
let server = await getNextServer(group);
if (server == null) return {
error: 'Server not found'
};
let url = server.url + path;
let token = server.token;
let defaultInit = {
method: method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
};
if (init == null) init = defaultInit;
try {
return await fetch(url, replaceToken(init, token));
} catch (err) {
console.error('VBError: ' + err);
return {
error: err
};
}
}
const get = async (group, path, init = null) =>
{
return doFetch(group, 'GET', path, init);
}
const post = async (group, path, init = null) =>
{
return doFetch(group, 'POST', path, init);
}
const put = async (group, path, init = null) =>
{
return doFetch(group, 'PUT', path, init);
}
const patch = async (group, path, init = null) =>
{
return doFetch(group, 'PATCH', path, init);
}
const del = async (group, path, init = null) =>
{
return doFetch(group, 'DELETE', path, init);
}
const head = async (group, path, init = null) =>
{
return doFetch(group, 'HEAD', path, init);
}
const options = async (group, path, init = null) =>
{
return doFetch(group, 'OPTIONS', path, init);
}
const trace = async (group, path, init = null) =>
{
return doFetch(group, 'TRACE', path, init);
}
const connect = async (group, path, init = null) =>
{
return doFetch(group, 'CONNECT', path, init);
}
module.exports = {
readConfig,
writeConfig,
replaceToken,
doFetch,
get,
post,
put,
patch,
del,
head,
options,
trace,
connect
}