godprotocol 1.2.25 → 1.2.27

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 CHANGED
@@ -1,321 +1,108 @@
1
- import Fs from "godprotocol/repos/fs.js";
2
- import Github from "godprotocol/repos/github.js";
3
- import Handlers from "godprotocol/utils/oracle_handlers.js";
4
- import { post_request } from "godprotocol/utils/services.js";
5
-
6
- class Oracle extends Handlers {
7
- constructor(server, mirror) {
8
- super();
1
+ import { post_request } from "../utils/services.js";
9
2
 
3
+ class Oracle_client {
4
+ constructor(server) {
10
5
  this.server = server;
11
- this.mirror = mirror;
12
- this.repos = new Array();
13
- this.remotes = new Array();
14
6
 
15
- this.account_repos = new Object();
16
- this.configs = new Object();
7
+ this.repo_cache = new Object();
17
8
  }
18
9
 
19
- fetch = async (payload, server) => {
20
- let response;
21
- try {
22
- response = await post_request({
23
- options: {
24
- ...server,
25
- path: "/oracle",
26
- },
27
- data: JSON.stringify(payload),
28
- });
29
- } catch (e) {}
30
-
31
- return response;
32
- };
33
-
34
- call = async (payload) => {
35
- let { method, args } = payload;
36
-
37
- switch (method) {
38
- case "update_servers":
39
- let { repos, account, server } = args;
40
-
41
- let servers = await this.get_servers(account);
42
- if (servers.find((serv) => this.server_match(serv, server))) return;
43
- servers.push(server);
44
-
45
- let reps = await this.set_to_repos(
46
- `.oracle/${account}/.config`,
47
- JSON.stringify(servers),
48
- { exclude: repos }
49
- );
50
- await this.merge_repos([...repos, ...reps], account);
51
-
52
- return reps;
53
- break;
54
- }
55
- };
56
-
57
- stringify_repos = async () => {
58
- let rp = [];
59
- for (let r = 0; r < this.repos.length; r++) {
60
- let repo = this.repos[r];
61
-
62
- rp.push({ repo: await repo.repo.objectify(), filter: repo.filter });
63
- }
64
- return rp;
65
- };
66
-
67
- server_match = (s1, s2) => {
68
- return s1.port === s2.port && s1.hostname === s2.hostname;
69
- };
70
-
71
- cloth_repo = async (repo) => {
72
- switch (repo.type) {
73
- case "fs":
74
- repo = new Fs(repo.options || repo);
75
- break;
76
- case "github":
77
- repo = new Github(repo.options || repo);
78
- break;
79
- }
80
-
81
- return repo;
10
+ add_repo = async (repo) => {
11
+ await this.fetch({
12
+ method: "add_repo",
13
+ args: repo,
14
+ });
82
15
  };
83
16
 
84
- repo_object = (repos, account) => {
85
- repos = [...repos];
86
- let acc_repos = this.account_repos[account] || [];
87
- for (let r = 0; r < repos.length; r++) {
88
- let repo = repos[r];
89
-
90
- let fnd =
91
- this.repos.find((r) => r.repo.get_id() === repo) ||
92
- acc_repos.find((r) => (r.repo._id || r.repo.get_id()) === repo);
93
- if (fnd) {
94
- if (fnd.repo.get_id) {
95
- fnd = { ...fnd };
96
- fnd.repo = fnd.repo.objectify();
97
- }
98
- repos[r] = fnd;
99
- }
100
- }
101
-
102
- return repos.filter((r) => !!r.repo);
103
- };
104
-
105
- merge_repos = async (repos, account) => {
106
- let reps = Array.isArray(account) ? account : this.account_repos[account];
107
- if (!reps) {
108
- reps = new Array();
109
- this.account_repos[account] = reps;
110
- }
111
-
112
- for (let r = 0; r < repos.length; r++) {
113
- let repo = repos[r];
114
- if (reps.find((rp) => rp.repo._id === repo.repo._id)) continue;
115
-
116
- repo.repo = await this.cloth_repo(repo.repo);
117
-
118
- reps.push(repo);
119
- }
120
- return reps;
17
+ write = async (path, content) => {
18
+ await this.fetch({
19
+ method: Array.isArray(content) ? "write_bulk" : "write",
20
+ args: { path, content },
21
+ });
121
22
  };
122
23
 
123
- get_from_repos = async (path, options = {}) => {
124
- let repos = await this.parse_option_repos(options),
24
+ read = async (path) => {
25
+ let repo = this.repo_cache[path],
125
26
  content;
126
27
 
127
- for (let r = 0; r < repos.length; r++) {
128
- let repo = repos[r];
129
- if (!repo.filter || path.match(repo.filter)) {
130
- content = await repo.repo.read_file(path);
131
- if (content) break;
28
+ if (repo) {
29
+ content = await repo.read(path);
30
+ }
31
+ if (!content) {
32
+ let response = await this.fetch({
33
+ method: "read",
34
+ args: { path },
35
+ });
36
+ content = response.content;
37
+ if (content) {
38
+ repo = response.repo;
39
+ repo = await this.cloth_repo(repo);
40
+ if (repo) this.repo_cache[path] = response.repo;
132
41
  }
133
42
  }
134
43
  return content;
135
44
  };
136
45
 
137
- parse_option_repos = async (options) => {
138
- let repos = [];
139
- if (options.repos) {
140
- for (let r = 0; r < options.repos.length; r++) {
141
- let rep = options.repos[r];
142
-
143
- repos.push({
144
- filter: rep.filter,
145
- repo: rep.repo.get_id ? rep.repo : await this.cloth_repo(rep.repo),
146
- });
147
- }
148
- } else repos = this.repos;
46
+ server_match = (s1, s2) => s1.hostname === s2.hostname && s1.port === s2.port;
149
47
 
150
- return repos;
151
- };
152
-
153
- set_to_repos = async (path, content, options = {}) => {
154
- let { exclude } = options;
155
- let repos = await this.parse_option_repos(options),
156
- reps = [];
157
-
158
- for (let r = 0; r < repos.length; r++) {
159
- let repo = repos[r];
160
- if (exclude && exclude.find((e) => e._id === repo.repo.get_id())) {
161
- continue;
162
- }
163
- if (!repo.filter || path.match(repo.filter)) {
164
- await repo.repo.write_file(path, content);
48
+ update_servers = async (account) => {
49
+ let addr = `${account}/.servers`;
165
50
 
166
- reps.push({ filter: repo.filter, repo: repo.repo.objectify() });
167
- }
51
+ let servers = await this.read(addr);
52
+ servers = servers ? JSON.parse(servers) : [];
53
+ if (!servers.find((s) => this.server_match(s, this.client))) {
54
+ servers.push(this.client);
55
+ await this.write(addr, JSON.stringify(servers));
168
56
  }
169
- return reps;
170
- };
171
-
172
- get_servers = async (account) => {
173
- let servers = await this.get_from_repos(`.oracle/${account}/.config`);
174
- if (servers) servers = JSON.parse(servers);
175
- else servers = [];
176
-
177
57
  return servers;
178
58
  };
179
59
 
180
- get_config = async (address, cache) => {
181
- let conf;
182
- if (cache) {
183
- conf = this.configs[address];
184
- if (conf) return conf;
185
- }
186
- conf = await this.get_from_repos(`.oracle/${address}/.config`);
187
- if (!conf) return;
188
-
189
- conf = JSON.parse(conf);
190
- this.configs[address] = conf;
191
-
192
- return conf;
193
- };
194
-
195
- dot_config = ".config";
196
-
197
- write = async (path, content, options = {}) => {
198
- let { account, address } = options,
199
- conf;
200
-
201
- if (path.endsWith(this.dot_config)) {
202
- let sli = path.slice(0, (this.dot_config.length + 1) * -1);
203
- conf = await this.get_config(sli);
204
-
205
- let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
206
- let reps = await this.merge_repos(
207
- conf_repos,
208
- await this.filter_repos(`.oracle/${path}`)
209
- );
210
- conf = {
211
- ...conf,
212
- content,
213
- repositories: reps.map((r) => r.repo._id || r.repo.get_id()),
60
+ fetch = async (payload) => {
61
+ let headers;
62
+ if (payload.method !== "authenticate" && !this.auth_token) {
63
+ await this.sync(this.client);
64
+ } else if (payload.method !== "authenticate") {
65
+ headers = {
66
+ authorisation: this.auth_token,
214
67
  };
215
-
216
- this.configs[sli] = conf;
217
-
218
- await this.set_to_repos(`.oracle/${path}`, JSON.stringify(conf), {
219
- repos: reps,
220
- });
221
- } else {
222
- conf = await this.get_config(address, true);
223
-
224
- let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
225
- let reps = await this.merge_repos(
226
- conf_repos,
227
- await this.filter_repos(`.storage/${path}`)
228
- );
229
- await this.set_to_repos(`.storage/${path}`, content, { repos: reps });
230
- }
231
- };
232
-
233
- read = async (path, options = {}) => {
234
- let { account, address } = options,
235
- content;
236
-
237
- if (path.endsWith(this.dot_config)) {
238
- content = await this.get_config(
239
- path.slice(0, (this.dot_config.length + 1) * -1)
240
- );
241
- content = content ? content.content : content;
242
- } else {
243
- let conf = await this.get_config(address, true);
244
- let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
245
- let reps = await this.merge_repos(
246
- conf_repos,
247
- await this.filter_repos(`.storage/${path}`)
248
- );
249
-
250
- content = await this.get_from_repos(`.storage/${path}`, { repos: reps });
251
68
  }
252
69
 
253
- return content;
254
- };
255
-
256
- update_servers = async (account) => {
257
- let servers = await this.get_servers(account);
258
- if (servers.find((serv) => this.server_match(serv, this.server))) {
259
- return;
260
- }
261
-
262
- servers.push(this.server);
263
- let repos = await this.set_to_repos(
264
- `.oracle/${account}/.config`,
265
- JSON.stringify(servers)
266
- );
267
- this.account_repos[account] = repos;
268
-
269
- for (let s = 0; s < servers.length - 1; s++) {
270
- let server = servers[s];
271
-
272
- let reps = await this.fetch(
273
- {
274
- method: "update_servers",
275
- args: { account, repos, server: this.server },
70
+ try {
71
+ let resp = await post_request({
72
+ options: {
73
+ ...this.server,
74
+ path: `/oracle/${payload.method}`,
75
+ headers,
276
76
  },
277
- server
278
- );
77
+ data: JSON.stringify(payload.args),
78
+ });
279
79
 
280
- await this.merge_repos(reps, account);
80
+ return resp || null;
81
+ } catch (e) {
82
+ console.warn("fetch error:", e.message || e);
83
+ return null;
281
84
  }
282
85
  };
283
86
 
284
- filter_repos = async (address, repos) => {
285
- repos = repos || this.repos;
87
+ sync = async (client, Repos) => {
88
+ this.client = client;
89
+ if (Repos) this.Repos = new Repos();
286
90
 
287
- let filtered_repos = new Array();
288
- for (let r = 0; r < repos.length; r++) {
289
- let repo = repos[r];
91
+ let auth = await this.fetch({
92
+ method: "authenticate",
93
+ args: {
94
+ client: this.client,
95
+ },
96
+ });
290
97
 
291
- if (!repo.filter || address.match(repo.filter)) {
292
- filtered_repos.push(repo);
293
- }
294
- }
295
-
296
- return filtered_repos;
297
- };
298
-
299
- get_repo = async (repo) => {
300
- for (let r = 0; r < this.repos.length; r++) {
301
- let rep = this.repos[r];
302
-
303
- if (rep.repo.get_id() === repo.get_id()) return rep.repo;
98
+ if (auth.token) {
99
+ this.auth_token = auth.token;
304
100
  }
305
101
  };
306
102
 
307
- add_repo = async (repository) => {
308
- let { filter, repo } = repository;
309
-
310
- repo = await this.cloth_repo(repo);
311
- if (await this.get_repo(repo)) return;
312
-
313
- this.repos.push({ filter, repo });
314
- };
315
-
316
- sync = async (mgr) => {
317
- this.manager = mgr;
103
+ cloth_repo = async (repo) => {
104
+ return await this.Repos.cloth_repo(repo);
318
105
  };
319
106
  }
320
107
 
321
- export default Oracle;
108
+ export default Oracle_client;