godprotocol 1.2.19 → 1.2.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Objects/Oracle.js +225 -284
- package/Objects/Repository.js +33 -16
- package/package.json +1 -1
- package/utils/create_server.js +62 -45
package/Objects/Oracle.js
CHANGED
|
@@ -3,356 +3,297 @@ import Github from "godprotocol/repos/github.js";
|
|
|
3
3
|
import Handlers from "godprotocol/utils/oracle_handlers.js";
|
|
4
4
|
import { post_request } from "godprotocol/utils/services.js";
|
|
5
5
|
|
|
6
|
-
class Oracle extends Handlers{
|
|
6
|
+
class Oracle extends Handlers {
|
|
7
7
|
constructor(server, mirror) {
|
|
8
8
|
super();
|
|
9
9
|
|
|
10
|
-
this.server = server
|
|
10
|
+
this.server = server;
|
|
11
11
|
this.mirror = mirror;
|
|
12
|
-
this.repos = new Array()
|
|
13
|
-
this.remotes = new Array()
|
|
12
|
+
this.repos = new Array();
|
|
13
|
+
this.remotes = new Array();
|
|
14
|
+
|
|
15
|
+
this.account_repos = new Object();
|
|
14
16
|
}
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
let
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
fetch = async (payload, server) => {
|
|
19
|
+
let response;
|
|
20
|
+
try {
|
|
21
|
+
response = await post_request({
|
|
22
|
+
options: {
|
|
23
|
+
...server,
|
|
24
|
+
path: "/oracle",
|
|
25
|
+
},
|
|
26
|
+
data: JSON.stringify(payload),
|
|
27
|
+
});
|
|
28
|
+
} catch (e) {}
|
|
20
29
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
repo = new Fs(repo.options)
|
|
24
|
-
break
|
|
25
|
-
case 'github':
|
|
26
|
-
repo = new Github(repo.options)
|
|
27
|
-
break
|
|
28
|
-
}
|
|
29
|
-
this.repos.push({filter, repo})
|
|
30
|
+
return response;
|
|
31
|
+
};
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
for (let r=0; r< this.remotes.length; r++){
|
|
34
|
-
let remote = this.remotes[r]
|
|
33
|
+
call = async (payload) => {
|
|
34
|
+
let { method, args } = payload;
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}, remote)
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
await this.fetch({
|
|
43
|
-
method: 'add_repo',
|
|
44
|
-
arg: {...repo}
|
|
45
|
-
}, this.server)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
36
|
+
switch (method) {
|
|
37
|
+
case "update_server":
|
|
38
|
+
let { repos, account, server } = args;
|
|
48
39
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
servers.push(arg.server)
|
|
62
|
-
}else servers = [arg.server]
|
|
63
|
-
|
|
64
|
-
await this.set_to_repos(addr, JSON.stringify(servers))
|
|
65
|
-
break;
|
|
66
|
-
case 'update_accounts':
|
|
67
|
-
let a_addr = `.oracle/${arg.address}/.configs`
|
|
68
|
-
let accs = await this.get_from_repos(a_addr)
|
|
69
|
-
if (accs){
|
|
70
|
-
accs = JSON.parse(accs)
|
|
71
|
-
if (accs.includes(arg.account)){
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
accs.push(arg.account)
|
|
75
|
-
}else accs = [arg.account]
|
|
76
|
-
|
|
77
|
-
await this.set_to_repos(a_addr, JSON.stringify(accs))
|
|
78
|
-
break
|
|
79
|
-
case 'get_accounts':
|
|
80
|
-
result = this.get_accounts(arg.address, true)
|
|
81
|
-
break
|
|
82
|
-
case 'get_servers':
|
|
83
|
-
result = this.get_servers(arg.account, true)
|
|
84
|
-
break
|
|
85
|
-
case 'write':
|
|
86
|
-
await this.set_to_repos(`${arg.prefix}/${arg.path}`, arg.content)
|
|
87
|
-
break;
|
|
88
|
-
case 'read':
|
|
89
|
-
let res = await this.get_from_repos(`${arg.prefix}/${arg.path}`)
|
|
90
|
-
result = {content: res}
|
|
40
|
+
let servers = await this.get_servers(account);
|
|
41
|
+
if (servers.find((serv) => this.server_match(serv, server))) return;
|
|
42
|
+
servers.push(server);
|
|
43
|
+
|
|
44
|
+
let reps = await this.set_to_repos(
|
|
45
|
+
`.oracle/${account}/.config`,
|
|
46
|
+
JSON.stringify(servers),
|
|
47
|
+
{ exclude: repos }
|
|
48
|
+
);
|
|
49
|
+
await this.merge_repos([...repos, ...reps], account);
|
|
50
|
+
|
|
51
|
+
return reps;
|
|
91
52
|
break;
|
|
92
|
-
case 'fs':
|
|
93
|
-
let repo = await this.get_repo('fs', arg.name)
|
|
94
|
-
result = await repo.call(arg.payload)
|
|
95
|
-
break
|
|
96
|
-
case 'sync':
|
|
97
|
-
let repos = this.string_repos()
|
|
98
|
-
await this.push_remote(arg.manager_server)
|
|
99
|
-
result = {repos}
|
|
100
|
-
break
|
|
101
|
-
case 'add_repo':
|
|
102
|
-
await this.add_repo(arg)
|
|
103
|
-
break
|
|
104
53
|
}
|
|
54
|
+
};
|
|
105
55
|
|
|
106
|
-
|
|
107
|
-
|
|
56
|
+
stringify_repos = async () => {
|
|
57
|
+
let rp = [];
|
|
58
|
+
for (let r = 0; r < this.repos.length; r++) {
|
|
59
|
+
let repo = this.repos[r];
|
|
108
60
|
|
|
109
|
-
|
|
110
|
-
|
|
61
|
+
rp.push({ repo: await repo.repo.objectify(), filter: repo.filter });
|
|
62
|
+
}
|
|
63
|
+
return rp;
|
|
64
|
+
};
|
|
111
65
|
|
|
112
|
-
|
|
113
|
-
|
|
66
|
+
server_match = (s1, s2) => {
|
|
67
|
+
return s1.port === s2.port && s1.hostname === s2.hostname;
|
|
68
|
+
};
|
|
114
69
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
70
|
+
cloth_repo = async (repo) => {
|
|
71
|
+
switch (repo.type) {
|
|
72
|
+
case "fs":
|
|
73
|
+
repo = new Fs(repo.options || repo);
|
|
74
|
+
break;
|
|
75
|
+
case "github":
|
|
76
|
+
repo = new Github(repo.options || repo);
|
|
77
|
+
break;
|
|
120
78
|
}
|
|
121
|
-
return repos;
|
|
122
|
-
}
|
|
123
79
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
let {repo} = this.repos[r]
|
|
80
|
+
return repo;
|
|
81
|
+
};
|
|
127
82
|
|
|
128
|
-
|
|
129
|
-
|
|
83
|
+
repo_object = (repos, account) => {
|
|
84
|
+
repos = [...repos];
|
|
85
|
+
let acc_repos = this.account_repos[account] || [];
|
|
86
|
+
for (let r = 0; r < repos.length; r++) {
|
|
87
|
+
let repo = repos[r];
|
|
88
|
+
|
|
89
|
+
let fnd =
|
|
90
|
+
this.repos.find((r) => r.repo.get_id() === repo) ||
|
|
91
|
+
acc_repos.find((r) => r.repo._id === repo);
|
|
92
|
+
if (fnd) {
|
|
93
|
+
if (fnd.repo.get_id) {
|
|
94
|
+
fnd.repo = fnd.repo.objectify();
|
|
95
|
+
}
|
|
96
|
+
repos[r] = fnd;
|
|
97
|
+
}
|
|
130
98
|
}
|
|
131
|
-
}
|
|
132
99
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
data: JSON.stringify(payload)
|
|
142
|
-
})
|
|
143
|
-
}catch(e){
|
|
144
|
-
console.log(e)
|
|
100
|
+
return repos.filter((r) => !!r.repo);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
merge_repos = async (repos, account) => {
|
|
104
|
+
let reps = Array.isArray(account) ? account : this.account_repos[account];
|
|
105
|
+
if (!reps) {
|
|
106
|
+
reps = new Array();
|
|
107
|
+
this.account_repos[account] = reps;
|
|
145
108
|
}
|
|
146
109
|
|
|
147
|
-
|
|
148
|
-
|
|
110
|
+
for (let r = 0; r < repos.length; r++) {
|
|
111
|
+
let repo = repos[r];
|
|
112
|
+
if (reps.find((rp) => rp.repo._id === repo.repo._id)) continue;
|
|
149
113
|
|
|
150
|
-
|
|
151
|
-
let {count, args} = options, counted = count != null, res;
|
|
152
|
-
for (let r=0; r< this.repos.length; r++){
|
|
153
|
-
let repo = this.repos[r]
|
|
114
|
+
repo.repo = await this.cloth_repo(repo.repo);
|
|
154
115
|
|
|
155
|
-
|
|
156
|
-
res = await repo.repo.write_file(address, content, args)
|
|
157
|
-
if (!args.just_exists){
|
|
158
|
-
res = await repo.repo.objectify()
|
|
159
|
-
}
|
|
160
|
-
if (counted && !count) break;
|
|
161
|
-
count --
|
|
162
|
-
}
|
|
116
|
+
reps.push(repo);
|
|
163
117
|
}
|
|
118
|
+
return reps;
|
|
119
|
+
};
|
|
164
120
|
|
|
165
|
-
|
|
166
|
-
|
|
121
|
+
get_from_repos = async (path, options = {}) => {
|
|
122
|
+
let repos = await this.parse_option_repos(options),
|
|
123
|
+
content;
|
|
167
124
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if(repo_ && repo.repo.match(repo_)){
|
|
174
|
-
content = await repo.repo.read_file(address)
|
|
175
|
-
break
|
|
176
|
-
} else if (!repo.filter || address.match(repo.filter)){
|
|
177
|
-
content = await repo.repo.read_file(address)
|
|
178
|
-
if (content) break
|
|
125
|
+
for (let r = 0; r < repos.length; r++) {
|
|
126
|
+
let repo = repos[r];
|
|
127
|
+
if (!repo.filter || path.match(repo.filter)) {
|
|
128
|
+
content = await repo.repo.read_file(path);
|
|
129
|
+
if (content) break;
|
|
179
130
|
}
|
|
180
131
|
}
|
|
181
|
-
return content
|
|
182
|
-
}
|
|
132
|
+
return content;
|
|
133
|
+
};
|
|
183
134
|
|
|
184
|
-
|
|
185
|
-
let
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
for (let s=0; s< servers.length; s++){
|
|
194
|
-
let server = servers[s]
|
|
195
|
-
if (this.server_match(server, this.server)){
|
|
196
|
-
let res = await this.fetch({
|
|
197
|
-
method: 'read',
|
|
198
|
-
arg: {path, prefix}
|
|
199
|
-
}, server)
|
|
200
|
-
|
|
201
|
-
if(res && res.content){
|
|
202
|
-
return res.content
|
|
203
|
-
}else break;
|
|
135
|
+
parse_option_repos = async (options) => {
|
|
136
|
+
let repos = [];
|
|
137
|
+
if (options.repos) {
|
|
138
|
+
for (let r = 0; r < options.repos[r]; r++) {
|
|
139
|
+
let rep = options.repos[r];
|
|
140
|
+
repos.push({
|
|
141
|
+
filter: rep.filter,
|
|
142
|
+
repo: await this.cloth_repo(rep.repo),
|
|
143
|
+
});
|
|
204
144
|
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
145
|
+
} else repos = this.repos;
|
|
146
|
+
|
|
147
|
+
return repos;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
set_to_repos = async (path, content, options = {}) => {
|
|
151
|
+
let { exclude } = options;
|
|
152
|
+
let repos = await this.parse_option_repos(options),
|
|
153
|
+
content,
|
|
154
|
+
reps = [];
|
|
155
|
+
|
|
156
|
+
for (let r = 0; r < repos.length; r++) {
|
|
157
|
+
let repo = repos[r];
|
|
158
|
+
if (exclude && exclude.find((e) => e._id === repo.repo.get_id())) {
|
|
159
|
+
continue;
|
|
210
160
|
}
|
|
161
|
+
if (!repo.filter || path.match(repo.filter)) {
|
|
162
|
+
await repo.repo.write_file(path, content);
|
|
211
163
|
|
|
212
|
-
|
|
213
|
-
method: 'read',
|
|
214
|
-
arg: {path, prefix}
|
|
215
|
-
}, server)
|
|
216
|
-
if(res && res.content){
|
|
217
|
-
return res.content
|
|
164
|
+
reps.push({ filter: repo.filter, repo: repo.repo.objectify() });
|
|
218
165
|
}
|
|
219
166
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
write = async(path, content, options={})=>{
|
|
223
|
-
let {account, address, prefix} = options
|
|
224
|
-
prefix = prefix || '.storage'
|
|
167
|
+
return reps;
|
|
168
|
+
};
|
|
225
169
|
|
|
226
|
-
|
|
227
|
-
let servers = await this.
|
|
170
|
+
get_servers = async (account) => {
|
|
171
|
+
let servers = await this.get_from_repos(`.oracle/${account}/.config`);
|
|
172
|
+
if (servers) servers = JSON.parse(servers);
|
|
173
|
+
else servers = [];
|
|
228
174
|
|
|
229
|
-
|
|
230
|
-
|
|
175
|
+
return servers;
|
|
176
|
+
};
|
|
231
177
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
178
|
+
get_config = async (address, cache) => {
|
|
179
|
+
let conf;
|
|
180
|
+
if (cache) {
|
|
181
|
+
conf = this.configs[address];
|
|
182
|
+
if (conf) return conf;
|
|
236
183
|
}
|
|
237
|
-
|
|
184
|
+
conf = await this.get_from_repos(`.oracle/${address}/.config`);
|
|
185
|
+
if (!conf) return;
|
|
238
186
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
let servers = await this.get_from_repos(addr)
|
|
242
|
-
servers = servers ? JSON.parse(servers) : []
|
|
243
|
-
|
|
244
|
-
if(!mirroring && !servers.length && this.mirror){
|
|
245
|
-
servers = await this.fetch({
|
|
246
|
-
method: 'get_servers',
|
|
247
|
-
arg: {account}
|
|
248
|
-
}, this.mirror)
|
|
249
|
-
if (servers.length){
|
|
250
|
-
await this.set_to_repos(addr, JSON.stringify(servers))
|
|
251
|
-
await this.update_servers(account, [...servers])
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return servers
|
|
255
|
-
}
|
|
187
|
+
conf = JSON.parse(conf);
|
|
188
|
+
this.configs[address] = conf;
|
|
256
189
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (servers.find(s=>this.server_match(s, this.server))){
|
|
261
|
-
return
|
|
262
|
-
}
|
|
190
|
+
return conf;
|
|
191
|
+
};
|
|
263
192
|
|
|
264
|
-
|
|
265
|
-
for (let s=0; s< servers.length; s++){
|
|
266
|
-
let server = servers[s]
|
|
193
|
+
dot_config = ".config";
|
|
267
194
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
arg: {account, server: this.server},
|
|
271
|
-
}, server)
|
|
272
|
-
}
|
|
195
|
+
write = async (path, content, options = {}) => {
|
|
196
|
+
let { account, address } = options;
|
|
273
197
|
|
|
274
|
-
|
|
275
|
-
|
|
198
|
+
if (path.endsWith(this.dot_config)) {
|
|
199
|
+
conf = await this.get_config(
|
|
200
|
+
path.slice(0, (this.dot_config.length + 1) * -1)
|
|
201
|
+
);
|
|
276
202
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
let accounts = await this.get_from_repos(addr)
|
|
280
|
-
|
|
281
|
-
accounts = accounts ? JSON.parse(accounts) : []
|
|
282
|
-
|
|
283
|
-
if (!mirroring && !accounts.length && this.mirror){
|
|
284
|
-
accounts = await this.fetch({
|
|
285
|
-
method: 'get_accounts',
|
|
286
|
-
arg: {address}
|
|
287
|
-
}, this.mirror)
|
|
288
|
-
}
|
|
203
|
+
let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
|
|
204
|
+
let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
|
|
289
205
|
|
|
290
|
-
|
|
291
|
-
|
|
206
|
+
await this.set_to_repos(
|
|
207
|
+
`.oracle/${path}`,
|
|
208
|
+
JSON.stringify({
|
|
209
|
+
...conf,
|
|
210
|
+
content,
|
|
211
|
+
repos: reps.map((r) => r.repo._id),
|
|
212
|
+
}),
|
|
213
|
+
{ repos: reps }
|
|
214
|
+
);
|
|
215
|
+
} else {
|
|
216
|
+
conf = await this.get_config(address, true);
|
|
292
217
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (accounts.includes(account)){
|
|
297
|
-
return
|
|
298
|
-
}
|
|
218
|
+
let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
|
|
219
|
+
let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
|
|
299
220
|
|
|
300
|
-
|
|
221
|
+
await this.set_to_repos(`.storage/${path}`, content, { repos: reps });
|
|
222
|
+
}
|
|
223
|
+
};
|
|
301
224
|
|
|
302
|
-
|
|
225
|
+
read = async (path, options = {}) => {
|
|
226
|
+
let { account, address } = options,
|
|
227
|
+
content;
|
|
303
228
|
|
|
304
|
-
|
|
305
|
-
|
|
229
|
+
if (path.endsWith(this.dot_config.length)) {
|
|
230
|
+
content = await this.get_config(
|
|
231
|
+
path.slice(0, (this.dot_config.length + 1) * -1)
|
|
232
|
+
);
|
|
233
|
+
content = content ? content.content : content;
|
|
234
|
+
} else {
|
|
235
|
+
let conf = await this.get_config(address, true);
|
|
236
|
+
let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
|
|
237
|
+
let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
|
|
306
238
|
|
|
307
|
-
await this.
|
|
308
|
-
method: 'update_accounts',
|
|
309
|
-
arg: {address, account},
|
|
310
|
-
}, server)
|
|
239
|
+
content = await this.get_from_repos(`.storage/${path}`, { repos: reps });
|
|
311
240
|
}
|
|
312
241
|
|
|
313
|
-
return
|
|
314
|
-
}
|
|
242
|
+
return content;
|
|
243
|
+
};
|
|
315
244
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
for (let a=0; a< accounts.length; a++){
|
|
321
|
-
let acc = accounts[a]
|
|
322
|
-
let servers_ = await this.get_servers(acc)
|
|
323
|
-
servers_.map(serv=>{
|
|
324
|
-
if(!servers.find(s=>this.server_match(s, serv)))
|
|
325
|
-
servers.push(serv)
|
|
326
|
-
})
|
|
245
|
+
update_server = async (account) => {
|
|
246
|
+
let servers = await this.get_servers(account);
|
|
247
|
+
if (servers.find((serv) => this.server_match(serv, this.server))) {
|
|
248
|
+
return;
|
|
327
249
|
}
|
|
328
250
|
|
|
329
|
-
|
|
330
|
-
|
|
251
|
+
servers.push(this.server);
|
|
252
|
+
let repos = await this.set_to_repos(
|
|
253
|
+
`.oracle/${account}/.config`,
|
|
254
|
+
JSON.stringify(servers)
|
|
255
|
+
);
|
|
256
|
+
this.account_repos[account] = repos;
|
|
331
257
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
258
|
+
for (let s = 0; s < servers.length - 1; s++) {
|
|
259
|
+
let server = servers[s];
|
|
260
|
+
|
|
261
|
+
let reps = await this.fetch(
|
|
262
|
+
{
|
|
263
|
+
method: "update_server",
|
|
264
|
+
args: { account, repos, server: this.server },
|
|
265
|
+
},
|
|
266
|
+
server
|
|
267
|
+
);
|
|
335
268
|
|
|
336
|
-
|
|
337
|
-
|
|
269
|
+
await this.merge_repos(reps, account);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
338
272
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
method: 'sync',
|
|
342
|
-
arg: {manager_server: manager.server}
|
|
343
|
-
}, this.server)
|
|
273
|
+
filter_repos = async (address, repos) => {
|
|
274
|
+
repos = repos || this.repos;
|
|
344
275
|
|
|
345
|
-
|
|
346
|
-
|
|
276
|
+
let filtered_repos = new Array();
|
|
277
|
+
for (let r = 0; r < repos.length; r++) {
|
|
278
|
+
let repo = repos[r];
|
|
347
279
|
|
|
348
|
-
|
|
280
|
+
if (!repo.filter || address.match(repo.filter)) {
|
|
281
|
+
filtered_repos.push(repo);
|
|
349
282
|
}
|
|
350
283
|
}
|
|
351
|
-
}
|
|
352
284
|
|
|
353
|
-
|
|
354
|
-
|
|
285
|
+
return filtered_repos;
|
|
355
286
|
};
|
|
287
|
+
|
|
288
|
+
add_repo = async (repository) => {
|
|
289
|
+
let { filter, repo } = repository;
|
|
290
|
+
if (await this.get_repo(repo.type, repo.options.name)) return;
|
|
291
|
+
|
|
292
|
+
repo = await this.cloth_repo(repo);
|
|
293
|
+
this.repos.push({ filter, repo });
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
sync = async () => {};
|
|
356
297
|
}
|
|
357
298
|
|
|
358
299
|
export default Oracle;
|
package/Objects/Repository.js
CHANGED
|
@@ -1,26 +1,43 @@
|
|
|
1
|
-
class Repository{
|
|
2
|
-
constructor(type){
|
|
1
|
+
class Repository {
|
|
2
|
+
constructor(type) {
|
|
3
3
|
this.type = type;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
objectify = ()=>{
|
|
7
|
-
let obj = {}
|
|
8
|
-
if (this.type ===
|
|
6
|
+
objectify = () => {
|
|
7
|
+
let obj = { type: this.type };
|
|
8
|
+
if (this.type === "fs") {
|
|
9
9
|
obj.name = this.name;
|
|
10
|
-
obj.remote = this.remote
|
|
11
|
-
obj.__dirname = this.__dirname
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
obj.
|
|
10
|
+
obj.remote = this.remote;
|
|
11
|
+
obj.__dirname = this.__dirname;
|
|
12
|
+
obj._id = `${this.__dirname}/${this.name}`;
|
|
13
|
+
} else if (this.type === "github") {
|
|
14
|
+
obj.key = this.auth_token;
|
|
15
|
+
obj.username = this.owner;
|
|
15
16
|
obj.repo = this.repo;
|
|
17
|
+
obj._id = `${this.owner}/${this.repo}`;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
return obj
|
|
19
|
-
}
|
|
20
|
+
return obj;
|
|
21
|
+
};
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
-
|
|
23
|
+
get_id = () => {
|
|
24
|
+
if (this._id) return this._id;
|
|
25
|
+
|
|
26
|
+
switch (this.type) {
|
|
27
|
+
case "fs":
|
|
28
|
+
this._id = `${this.__dirname}/${this.name}`;
|
|
29
|
+
break;
|
|
30
|
+
case "github":
|
|
31
|
+
this._id = `${this.owner}/${this.repo}`;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return this._id;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
match = async () => {
|
|
39
|
+
return false;
|
|
40
|
+
};
|
|
24
41
|
}
|
|
25
42
|
|
|
26
|
-
export default Repository;
|
|
43
|
+
export default Repository;
|
package/package.json
CHANGED
package/utils/create_server.js
CHANGED
|
@@ -1,116 +1,133 @@
|
|
|
1
|
-
import { set_page } from
|
|
2
|
-
import { generate_random_string } from
|
|
1
|
+
import { set_page } from "../html/index.js";
|
|
2
|
+
import { generate_random_string } from "generalised-datastore/utils/functions.js";
|
|
3
3
|
|
|
4
|
-
let PORT = Number(process.env.PORT) || 1408,
|
|
4
|
+
let PORT = Number(process.env.PORT) || 1408,
|
|
5
|
+
account_uids = {};
|
|
5
6
|
|
|
6
7
|
export const handle_routes = async (req, res, manager, app) => {
|
|
7
8
|
try {
|
|
8
9
|
// CORS
|
|
9
|
-
res.setHeader(
|
|
10
|
-
res.setHeader(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
11
|
+
res.setHeader(
|
|
12
|
+
"Access-Control-Allow-Methods",
|
|
13
|
+
"GET, POST, PUT, DELETE, OPTIONS"
|
|
14
|
+
);
|
|
15
|
+
res.setHeader("Access-Control-Allow-Headers", "*");
|
|
16
|
+
|
|
17
|
+
if (req.method === "OPTIONS") {
|
|
14
18
|
res.writeHead(204);
|
|
15
19
|
return res.end();
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
// GET pages
|
|
19
|
-
if (req.method ===
|
|
20
|
-
if (req.url ===
|
|
21
|
-
res.writeHead(200, {
|
|
23
|
+
if (req.method === "GET") {
|
|
24
|
+
if (req.url === "/") {
|
|
25
|
+
res.writeHead(200, { "Content-Type": "text/html" });
|
|
22
26
|
return res.end(set_page(req.url, manager));
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
if (
|
|
26
|
-
|
|
29
|
+
if (
|
|
30
|
+
["/create_account", "/load", "/run", "/parse", "/oracle"].includes(
|
|
31
|
+
req.url
|
|
32
|
+
)
|
|
33
|
+
) {
|
|
34
|
+
res.writeHead(200, { "Content-Type": "text/html" });
|
|
27
35
|
return res.end(set_page(req.url.slice(1), manager));
|
|
28
36
|
}
|
|
29
37
|
|
|
30
38
|
// hand over untouched
|
|
31
|
-
return app ? app(req, res) : res.writeHead(404).end(
|
|
39
|
+
return app ? app(req, res) : res.writeHead(404).end("Not Found");
|
|
32
40
|
}
|
|
33
41
|
|
|
34
42
|
// Only custom POST endpoints handled here
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
if (
|
|
44
|
+
["/create_account", "/load", "/run", "/parse", "/oracle"].includes(
|
|
45
|
+
req.url
|
|
46
|
+
)
|
|
47
|
+
) {
|
|
48
|
+
let body = "";
|
|
49
|
+
req.on("data", (chunk) => {
|
|
50
|
+
body += chunk;
|
|
51
|
+
});
|
|
52
|
+
await new Promise((resolve) => req.on("end", resolve));
|
|
39
53
|
|
|
40
54
|
if (!body) {
|
|
41
|
-
res.writeHead(400, {
|
|
42
|
-
return res.end(
|
|
55
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
56
|
+
return res.end(
|
|
57
|
+
JSON.stringify({ error: "Request body cannot be empty" })
|
|
58
|
+
);
|
|
43
59
|
}
|
|
44
60
|
|
|
45
61
|
const data = JSON.parse(body);
|
|
46
62
|
let result;
|
|
47
63
|
|
|
48
64
|
switch (req.url) {
|
|
49
|
-
case
|
|
65
|
+
case "/create_account": {
|
|
50
66
|
let acc = await manager.get_account(data.name);
|
|
51
67
|
if (acc) {
|
|
52
|
-
if (!await acc.verify(data.password)) {
|
|
53
|
-
result = { message:
|
|
68
|
+
if (!(await acc.verify(data.password))) {
|
|
69
|
+
result = { message: "Incorrect password." };
|
|
54
70
|
} else {
|
|
55
|
-
const uid = generate_random_string(16,
|
|
71
|
+
const uid = generate_random_string(16, "alnum");
|
|
56
72
|
account_uids[uid] = data.name;
|
|
57
|
-
result = { uid, message:
|
|
73
|
+
result = { uid, message: "Account signed-in successfully." };
|
|
58
74
|
}
|
|
59
75
|
} else {
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
|
|
76
|
+
let new_acc = await manager.add_account(data.name, {
|
|
77
|
+
password: data.password,
|
|
78
|
+
});
|
|
79
|
+
if (new_acc) {
|
|
80
|
+
const uid = generate_random_string(16, "alnum");
|
|
63
81
|
account_uids[uid] = data.name;
|
|
64
|
-
result = { uid, message:
|
|
82
|
+
result = { uid, message: "Account created successfully." };
|
|
65
83
|
} else {
|
|
66
|
-
result = { message:
|
|
84
|
+
result = { message: "Account creation failed." };
|
|
67
85
|
}
|
|
68
86
|
}
|
|
69
87
|
break;
|
|
70
88
|
}
|
|
71
89
|
|
|
72
|
-
case
|
|
90
|
+
case "/oracle":
|
|
73
91
|
result = await manager.oracle.call(data);
|
|
74
92
|
break;
|
|
75
93
|
|
|
76
|
-
case
|
|
77
|
-
case
|
|
78
|
-
case
|
|
94
|
+
case "/load":
|
|
95
|
+
case "/run":
|
|
96
|
+
case "/parse": {
|
|
79
97
|
const accountName = account_uids[data.account];
|
|
80
98
|
if (!accountName) {
|
|
81
99
|
result = { message: `Account ${data.account} not found` };
|
|
82
100
|
} else {
|
|
83
101
|
const accountObj = await manager.get_account(accountName);
|
|
84
|
-
if (req.url ===
|
|
85
|
-
if (req.url ===
|
|
86
|
-
if (req.url ===
|
|
102
|
+
if (req.url === "/load") result = await accountObj.load(data);
|
|
103
|
+
if (req.url === "/run") result = await accountObj.run(data);
|
|
104
|
+
if (req.url === "/parse") result = await accountObj.parse(data);
|
|
87
105
|
}
|
|
88
106
|
break;
|
|
89
107
|
}
|
|
90
108
|
}
|
|
91
109
|
|
|
92
|
-
res.writeHead(200, {
|
|
110
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
93
111
|
return res.end(JSON.stringify(result));
|
|
94
112
|
}
|
|
95
113
|
|
|
96
114
|
// Everything else → forward to express app
|
|
97
|
-
return app ? app(req, res) : res.writeHead(404).end(
|
|
98
|
-
|
|
115
|
+
return app ? app(req, res) : res.writeHead(404).end("Not Found");
|
|
99
116
|
} catch (err) {
|
|
100
|
-
console.error(
|
|
101
|
-
res.writeHead(500, {
|
|
117
|
+
console.error("Handler error:", err);
|
|
118
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
102
119
|
return res.end(JSON.stringify({ error: err.message }));
|
|
103
120
|
}
|
|
104
121
|
};
|
|
105
122
|
|
|
106
|
-
|
|
107
123
|
const create_server = (settings) => {
|
|
108
124
|
settings = settings || {};
|
|
109
125
|
let { port, manager } = settings;
|
|
110
126
|
|
|
111
127
|
port = port || PORT;
|
|
112
|
-
|
|
113
|
-
let handler = async (req, res) =>
|
|
128
|
+
|
|
129
|
+
let handler = async (req, res) =>
|
|
130
|
+
await handle_routes(req, res, manager, manager.options.app);
|
|
114
131
|
|
|
115
132
|
return handler;
|
|
116
133
|
};
|