godprotocol 1.2.19 → 1.2.21

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
@@ -3,355 +3,317 @@ 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();
16
+ this.configs = new Object();
14
17
  }
15
18
 
16
- add_repo = async (repository)=>{
17
- let {filter, repo} = repository
18
- if (await this.get_repo(repo.type, repo.options.name))
19
- return;
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) {}
20
30
 
21
- switch(repo.type){
22
- case 'fs':
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})
31
+ return response;
32
+ };
30
33
 
31
- if(!this.manager) return;
32
- if (this.server_match(this.manager.server, this.server)){
33
- for (let r=0; r< this.remotes.length; r++){
34
- let remote = this.remotes[r]
34
+ call = async (payload) => {
35
+ let { method, args } = payload;
35
36
 
36
- await this.fetch({
37
- method: 'add_repo',
38
- arg: {...repo}
39
- }, remote)
40
- }
41
- } else {
42
- await this.fetch({
43
- method: 'add_repo',
44
- arg: {...repo}
45
- }, this.server)
46
- }
47
- }
37
+ switch (method) {
38
+ case "update_servers":
39
+ let { repos, account, server } = args;
48
40
 
49
- call = async(payload)=>{
50
- let {method, arg} = payload, result = {};
51
-
52
- switch(method){
53
- case 'update_servers':
54
- let addr = `.oracle/${arg.account}/.configs`
55
- let servers = await this.get_from_repos(addr)
56
- if (servers){
57
- servers = JSON.parse(servers)
58
- if (servers.find(s=>this.server_match(s, arg.server))){
59
- return
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}
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;
91
53
  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
54
  }
55
+ };
105
56
 
106
- return result
107
- }
57
+ stringify_repos = async () => {
58
+ let rp = [];
59
+ for (let r = 0; r < this.repos.length; r++) {
60
+ let repo = this.repos[r];
108
61
 
109
- push_remote = async(remote)=>{
110
- if (this.remotes.find(rem=>this.server_match(rem, remote))) return;
62
+ rp.push({ repo: await repo.repo.objectify(), filter: repo.filter });
63
+ }
64
+ return rp;
65
+ };
111
66
 
112
- this.remotes.push(remote)
113
- }
67
+ server_match = (s1, s2) => {
68
+ return s1.port === s2.port && s1.hostname === s2.hostname;
69
+ };
114
70
 
115
- string_repos = ()=>{
116
- let repos = new Array();
117
- for(let r=0; r< this.repos.length; r++){
118
- let {filter, repo} = this.repos[r];
119
- repos.push({filter, repo:{type: repo.type, options: repo.objectify()}})
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;
120
79
  }
121
- return repos;
122
- }
123
80
 
124
- get_repo = async(type, name) => {
125
- for (let r=0; r< this.repos.length; r++){
126
- let {repo} = this.repos[r]
81
+ return repo;
82
+ };
127
83
 
128
- if (repo.type === type && repo.name === name)
129
- return repo
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
+ }
130
100
  }
131
- }
132
101
 
133
- fetch = async(payload, server)=>{
134
- let res;
135
- try{
136
- res = await post_request({
137
- options: {
138
- ...server,
139
- path: '/oracle'
140
- },
141
- data: JSON.stringify(payload)
142
- })
143
- }catch(e){
144
- console.log(e)
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;
145
110
  }
146
111
 
147
- return res;
148
- }
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;
149
115
 
150
- set_to_repos = async(address, content, options)=>{
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]
116
+ repo.repo = await this.cloth_repo(repo.repo);
154
117
 
155
- if (!repo.filter || address.match(repo.filter)){
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
- }
118
+ reps.push(repo);
163
119
  }
120
+ return reps;
121
+ };
164
122
 
165
- return res;
166
- }
123
+ get_from_repos = async (path, options = {}) => {
124
+ let repos = await this.parse_option_repos(options),
125
+ content;
167
126
 
168
- get_from_repos = async(address, repo_)=>{
169
- let content
170
- for (let r=0; r< this.repos.length; r++){
171
- let repo = this.repos[r]
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
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;
179
132
  }
180
133
  }
181
- return content
182
- }
134
+ return content;
135
+ };
183
136
 
184
- read = async(path, options={})=>{
185
- let {address, account, prefix} = options;
186
- prefix = prefix || '.storage'
187
-
188
- let has = (await this.get_accounts(address)).includes(account)
189
-
190
- if(!has) return;
191
-
192
- let servers = await this.get_servers(account)
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;
204
- }
205
- }
206
- for (let s=0; s< servers.length; s++){
207
- let server = servers[s]
208
- if (this.server_match(server, this.server)){
209
- continue
210
- }
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];
211
142
 
212
- let res = await this.fetch({
213
- method: 'read',
214
- arg: {path, prefix}
215
- }, server)
216
- if(res && res.content){
217
- return res.content
143
+ repos.push({
144
+ filter: rep.filter,
145
+ repo: rep.repo.get_id ? rep.repo : await this.cloth_repo(rep.repo),
146
+ });
218
147
  }
219
- }
220
- }
221
-
222
- write = async(path, content, options={})=>{
223
- let {account, address, prefix} = options
224
- prefix = prefix || '.storage'
148
+ } else repos = this.repos;
225
149
 
226
- await this.update_accounts(address, account)
227
- let servers = await this.get_servers(account)
150
+ return repos;
151
+ };
228
152
 
229
- for (let s=0; s< servers.length; s++){
230
- let server = servers[s]
153
+ set_to_repos = async (path, content, options = {}) => {
154
+ let { exclude } = options;
155
+ let repos = await this.parse_option_repos(options),
156
+ reps = [];
231
157
 
232
- await this.fetch({
233
- method: 'write',
234
- arg: {path, content, prefix}
235
- }, server)
236
- }
237
- }
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);
238
165
 
239
- get_servers = async(account, mirroring)=>{
240
- let addr = `.oracle/${account}/.configs`
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])
166
+ reps.push({ filter: repo.filter, repo: repo.repo.objectify() });
252
167
  }
253
168
  }
254
- return servers
255
- }
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 = [];
256
176
 
257
- update_servers = async(account, servers)=>{
258
- servers = servers || await this.get_servers(account)
259
-
260
- if (servers.find(s=>this.server_match(s, this.server))){
261
- return
177
+ return servers;
178
+ };
179
+
180
+ get_config = async (address, cache) => {
181
+ let conf;
182
+ if (cache) {
183
+ conf = this.configs[address];
184
+ if (conf) return conf;
262
185
  }
186
+ conf = await this.get_from_repos(`.oracle/${address}/.config`);
187
+ if (!conf) return;
263
188
 
264
- servers.push(this.server)
265
- for (let s=0; s< servers.length; s++){
266
- let server = servers[s]
189
+ conf = JSON.parse(conf);
190
+ this.configs[address] = conf;
267
191
 
268
- await this.fetch({
269
- method: 'update_servers',
270
- arg: {account, server: this.server},
271
- }, server)
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(path)
209
+ );
210
+
211
+ await this.set_to_repos(
212
+ `.oracle/${path}`,
213
+ JSON.stringify({
214
+ ...conf,
215
+ content,
216
+ repositories: reps.map((r) => r.repo._id || r.repo.get_id()),
217
+ }),
218
+ { repos: reps }
219
+ );
220
+ } else {
221
+ conf = await this.get_config(address, true);
222
+
223
+ let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
224
+ let reps = await this.merge_repos(
225
+ conf_repos,
226
+ await this.filter_repos(path)
227
+ );
228
+ await this.set_to_repos(`.storage/${path}`, content, { repos: reps });
272
229
  }
230
+ };
273
231
 
274
- return servers
275
- }
232
+ read = async (path, options = {}) => {
233
+ let { account, address } = options,
234
+ content;
276
235
 
277
- get_accounts = async(address, mirroring)=>{
278
- let addr = `.oracle/${address}/.configs`
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)
236
+ if (path.endsWith(this.dot_config)) {
237
+ content = await this.get_config(
238
+ path.slice(0, (this.dot_config.length + 1) * -1)
239
+ );
240
+ content = content ? content.content : content;
241
+ } else {
242
+ let conf = await this.get_config(address, true);
243
+ let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
244
+ let reps = await this.merge_repos(
245
+ conf_repos,
246
+ await this.filter_repos(path)
247
+ );
248
+
249
+ content = await this.get_from_repos(`.storage/${path}`, { repos: reps });
288
250
  }
289
251
 
290
- return accounts
291
- }
252
+ return content;
253
+ };
292
254
 
293
- update_accounts = async(address, account)=>{
294
- let accounts = await this.get_accounts(address)
295
-
296
- if (accounts.includes(account)){
297
- return
255
+ update_servers = async (account) => {
256
+ let servers = await this.get_servers(account);
257
+ if (servers.find((serv) => this.server_match(serv, this.server))) {
258
+ return;
298
259
  }
299
260
 
300
- accounts.push(account)
261
+ servers.push(this.server);
262
+ let repos = await this.set_to_repos(
263
+ `.oracle/${account}/.config`,
264
+ JSON.stringify(servers)
265
+ );
266
+ this.account_repos[account] = repos;
301
267
 
302
- let servers = await this.get_servers_from_address(address, accounts)
268
+ for (let s = 0; s < servers.length - 1; s++) {
269
+ let server = servers[s];
303
270
 
304
- for (let s=0; s< servers.length; s++){
305
- let server = servers[s]
271
+ let reps = await this.fetch(
272
+ {
273
+ method: "update_servers",
274
+ args: { account, repos, server: this.server },
275
+ },
276
+ server
277
+ );
306
278
 
307
- await this.fetch({
308
- method: 'update_accounts',
309
- arg: {address, account},
310
- }, server)
279
+ await this.merge_repos(reps, account);
311
280
  }
281
+ };
312
282
 
313
- return accounts
314
- }
283
+ filter_repos = async (address, repos) => {
284
+ repos = repos || this.repos;
285
+
286
+ let filtered_repos = new Array();
287
+ for (let r = 0; r < repos.length; r++) {
288
+ let repo = repos[r];
315
289
 
316
- get_servers_from_address = async(address, accounts)=>{
317
- accounts = accounts || await this.get_accounts(address)
318
-
319
- let servers = []
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
- })
290
+ if (!repo.filter || address.match(repo.filter)) {
291
+ filtered_repos.push(repo);
292
+ }
327
293
  }
328
294
 
329
- return servers
330
- }
295
+ return filtered_repos;
296
+ };
331
297
 
332
- server_match = (s1, s2)=>{
333
- return s1.hostname === s2.hostname && s1.port === s2.port
334
- }
298
+ get_repo = async (repo) => {
299
+ for (let r = 0; r < this.repos.length; r++) {
300
+ let rep = this.repos[r];
335
301
 
336
- set_manager = async(manager)=>{
337
- console.log(`Manager:${manager.name} Oracle running at ${this.server.hostname}:${this.server.port}`)
302
+ if (rep.repo.get_id() === repo.get_id()) return rep.repo;
303
+ }
304
+ };
338
305
 
339
- if (!this.server_match(this.server, manager.server)){
340
- let response = await this.fetch({
341
- method: 'sync',
342
- arg: {manager_server: manager.server}
343
- }, this.server)
306
+ add_repo = async (repository) => {
307
+ let { filter, repo } = repository;
344
308
 
345
- for (let r=0; r< response.repos.length; r++){
346
- let repository = response.repos[r]
309
+ repo = await this.cloth_repo(repo);
310
+ if (await this.get_repo(repo)) return;
347
311
 
348
- await this.add_repo(repository)
349
- }
350
- }
351
- }
312
+ this.repos.push({ filter, repo });
313
+ };
352
314
 
353
- sync = async () => {
354
-
315
+ sync = async (mgr) => {
316
+ this.manager = mgr;
355
317
  };
356
318
  }
357
319
 
@@ -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 === 'fs'){
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
- }else if (this.type === 'github'){
13
- obj.key = this.auth_token
14
- obj.username = this.owner
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
- match = async()=>{
22
- return false
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.2.19",
3
+ "version": "1.2.21",
4
4
  "description": "A distributed computing environment",
5
5
  "main": "Index.js",
6
6
  "scripts": {
@@ -28,12 +28,9 @@
28
28
  "type": "module",
29
29
  "homepage": "https://github.com/immanuel-savvy/GodProtocol#readme",
30
30
  "dependencies": {
31
- "@octokit/rest": "^21.0.2",
32
31
  "crypto": "^1.0.1",
33
- "elliptic": "^6.6.0",
34
32
  "fs": "^0.0.1-security",
35
33
  "generalised-datastore": "latest",
36
- "js-base64": "^3.7.7",
37
34
  "aircode4": "latest"
38
35
  },
39
36
  "devDependencies": {