godprotocol 1.2.24 → 1.2.26
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/Callable.js +197 -176
- package/Objects/Oracle.js +61 -294
- package/Objects/Virtual_machine.js +303 -263
- package/package.json +22 -15
- package/utils/vm_utils.js +162 -147
- package/repos/fs.js +0 -122
- package/repos/github.js +0 -185
- package/repos/ram.js +0 -47
package/Objects/Oracle.js
CHANGED
|
@@ -1,321 +1,88 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
15
|
-
this.account_repos = new Object();
|
|
16
|
-
this.configs = new Object();
|
|
17
6
|
}
|
|
18
7
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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;
|
|
82
|
-
};
|
|
83
|
-
|
|
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;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
get_from_repos = async (path, options = {}) => {
|
|
124
|
-
let repos = await this.parse_option_repos(options),
|
|
125
|
-
content;
|
|
126
|
-
|
|
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;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return content;
|
|
135
|
-
};
|
|
136
|
-
|
|
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;
|
|
149
|
-
|
|
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);
|
|
165
|
-
|
|
166
|
-
reps.push({ filter: repo.filter, repo: repo.repo.objectify() });
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return reps;
|
|
8
|
+
add_repo = async (repo, filter) => {
|
|
9
|
+
await this.fetch({
|
|
10
|
+
method: "add_repo",
|
|
11
|
+
args: {
|
|
12
|
+
repo,
|
|
13
|
+
filter,
|
|
14
|
+
},
|
|
15
|
+
});
|
|
170
16
|
};
|
|
171
17
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return servers;
|
|
18
|
+
write = async (path, content) => {
|
|
19
|
+
await this.fetch({
|
|
20
|
+
method: Array.isArray(content) ? "write_bulk" : "write",
|
|
21
|
+
args: { path, content },
|
|
22
|
+
});
|
|
178
23
|
};
|
|
179
24
|
|
|
180
|
-
|
|
181
|
-
let
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
if (conf) return conf;
|
|
25
|
+
read = async (path) => {
|
|
26
|
+
let repo = this.repo_cache[path];
|
|
27
|
+
if (repo) {
|
|
28
|
+
content = await repo.read(path);
|
|
185
29
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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()),
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
this.configs[sli] = conf;
|
|
217
|
-
|
|
218
|
-
await this.set_to_repos(`.oracle/${path}`, JSON.stringify(conf), {
|
|
219
|
-
repos: reps,
|
|
30
|
+
if (!content) {
|
|
31
|
+
let response = await this.fetch({
|
|
32
|
+
method: "read",
|
|
33
|
+
args: { path },
|
|
220
34
|
});
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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 });
|
|
35
|
+
content = response.content;
|
|
36
|
+
if (content) {
|
|
37
|
+
repo = response.repo;
|
|
38
|
+
repo = await this.cloth_repo(repo);
|
|
39
|
+
if (repo) this.repo_cache[path] = response.repo;
|
|
40
|
+
}
|
|
251
41
|
}
|
|
252
|
-
|
|
253
42
|
return content;
|
|
254
43
|
};
|
|
255
44
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return;
|
|
45
|
+
fetch = async (payload) => {
|
|
46
|
+
if (payload.method !== "authenticate" && !this.auth_token) {
|
|
47
|
+
await this.sync(this.client);
|
|
260
48
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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 },
|
|
49
|
+
try {
|
|
50
|
+
let resp = await post_request({
|
|
51
|
+
options: {
|
|
52
|
+
...this.server,
|
|
53
|
+
path: `/oracle/${payload.method}`,
|
|
54
|
+
headers: {
|
|
55
|
+
authorisation: this.auth_token,
|
|
56
|
+
},
|
|
276
57
|
},
|
|
277
|
-
|
|
278
|
-
);
|
|
279
|
-
|
|
280
|
-
|
|
58
|
+
data: JSON.stringify(payload.args),
|
|
59
|
+
});
|
|
60
|
+
return resp || null;
|
|
61
|
+
} catch (e) {
|
|
62
|
+
console.warn("fetch error:", e.message || e);
|
|
63
|
+
return null;
|
|
281
64
|
}
|
|
282
65
|
};
|
|
283
66
|
|
|
284
|
-
|
|
285
|
-
|
|
67
|
+
sync = async (client, Repos) => {
|
|
68
|
+
this.client = client;
|
|
69
|
+
if (Repos) this.Repos = new Repos();
|
|
286
70
|
|
|
287
|
-
let
|
|
288
|
-
|
|
289
|
-
|
|
71
|
+
let auth = await this.fetch({
|
|
72
|
+
method: "authenticate",
|
|
73
|
+
args: {
|
|
74
|
+
client: this.client,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
290
77
|
|
|
291
|
-
|
|
292
|
-
|
|
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;
|
|
78
|
+
if (auth.token) {
|
|
79
|
+
this.auth_token = auth.token;
|
|
304
80
|
}
|
|
305
81
|
};
|
|
306
82
|
|
|
307
|
-
|
|
308
|
-
|
|
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;
|
|
83
|
+
cloth_repo = async (repo) => {
|
|
84
|
+
return await this.Repos.cloth_repo(repo);
|
|
318
85
|
};
|
|
319
86
|
}
|
|
320
87
|
|
|
321
|
-
export default
|
|
88
|
+
export default Oracle_client;
|